Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Question about OpenMP

Zhanghong_T_
Novice
628 Views

Dear all,

I am studying to coding with OpenMP. I have a simple Fortran code which uses a module and some variable was defined in the module. I wish the module is common for all subroutines which use the module but it is dependent for threads, how should I do?

Thanks,
Zhanghong Tang

The Fortran code:

module omp
integer::ithread
end module

program test2
use omp
implicit none
include 'omp_lib.h'
!$omp parallel
ithread = OMP_GET_NUM_THREADS()
!call sleep(1)
write(*,*)ithread
!$omp end parallel
! Variables

! Body of test2
print *, 'Hello World'

end program test2

0 Kudos
5 Replies
Steve_Nuchia
New Contributor I
628 Views


I am finding your question difficult to understand. In particular, I do not understand what you mean by "dependent for [on] threads". While this is an English language forum, it is possible that another participant could help you express yourself more clearly if you also posted a description of the core issue in your native language.

Also, you do not say explicitly what your example is intended to do, nor in what way it falls short of your expectations.

One problem I see, if I understand your intentions correctly, is that each thread is printing the same total number of threads rather than its own OMP thread number. Try OMP_GET_THREAD_NUM instead.

-swn

0 Kudos
Steve_Nuchia
New Contributor I
628 Views


You might also find THREADPRIVATE relevant to your task.

0 Kudos
Zhanghong_T_
Novice
628 Views
Quoting - Steve Nuchia


You might also find THREADPRIVATE relevant to your task.

Dear Steve Nuchia,

Thank you very much for your kindly reply.

THe code should be like this:

module omp
integer::ithread
end module

program test2
use omp
implicit none
include 'omp_lib.h'
!$omp parallel
ithread = OMP_GET_THREAD_NUM()
call sleep(1)
write(*,*)ithread
!$omp end parallel
! Variables

! Body of test2
print *, 'Hello World'

end program test2

Now the problem is that how to let the "ithread" be a common variable for the subroutines which use the module "omp" but is private for every thread.

Thanks,
Zhanghong Tang

0 Kudos
Zhanghong_T_
Novice
628 Views
Quoting - Steve Nuchia


You might also find THREADPRIVATE relevant to your task.

Oh, your THREADPRIVATE is just what I looked for.

Thanks,

Zhanghong Tang

0 Kudos
Zhanghong_T_
Novice
628 Views
Quoting - Steve Nuchia


You might also find THREADPRIVATE relevant to your task.

Dear Steve Nuchia,

Thank you very much for your kindly reply. Now I want to let the C code to call the Fortran code and the OpenMP applies on C level. The Fortran code is compiled as a static library and the library is linked into the C main program. The whole solution was attached. Could you please help me to look at it?

I wish the following output:

ithread = 0

ithread = 2

ithread = 1

ithread = 3

0 3

2 3

1 3

3 3

But the result is as follows:

ithread = 0

ithread = 2

ithread = 1

ithread = 3

3 3

3 3

3 3

3 3

Thanks,

Zhanghong Tang

0 Kudos
Reply