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

Usage of THREADPRIVATE directives with COMMON blocks

webhart
Beginner
1,413 Views

I have a multithreaded program where threads are created from c++. The fortran subroutines use common blocks. I wish that the data of the common blocks is local to the respective thread. The solution below fails. I am not a fortran programmer but I read the documentation about the THREADPRIVATE directive. The code below demonstrates my problem. After running a while printing dots the "Trouble" output appears. Thank youfor any help.

spawn_threads() is written in C++ and spawns two threads that start MYSUB1().

gettid(int) is written in C++ and returns a tid using pthread_self()

The objects were linked using: ifort -openmp *.o -lstdc++

!$OMP THREADPRIVATE ( /mycommon/)
!$OMP PARALLEL DEFAULT(PRIVATE) COPYIN(/mycommon/)
SUBROUTINE MYSUB1()
implicit none

INTEGER tid, tid2
COMMON /mycommon/ tid

call gettid(tid)
call gettid(tid2)

do while (.TRUE.)
call mysub2(tid2)
enddo

end subroutine

subroutine MYSUB2(tid2)
implicit none

INTEGER tid, tid2
COMMON /mycommon/ tid

if (tid2.ne.tid) then
write(0,*) "Trouble: ", tid, " != " , tid2
else
write(0,*) "."
endif

end subroutine

program testthreads

call spawn_threads()

end program

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
1,413 Views
Quoting - webhart

I have a multithreaded program where threads are created from c++. The fortran subroutines use common blocks. I wish that the data of the common blocks is local to the respective thread. The solution below fails. I am not a fortran programmer but I read the documentation about the THREADPRIVATE directive. The code below demonstrates my problem. After running a while printing dots the "Trouble" output appears. Thank youfor any help.

spawn_threads() is written in C++ and spawns two threads that start MYSUB1().

gettid(int) is written in C++ and returns a tid using pthread_self()

The objects were linked using: ifort -openmp *.o -lstdc++

!$OMP THREADPRIVATE ( /mycommon/)
!$OMP PARALLEL DEFAULT(PRIVATE) COPYIN(/mycommon/)
SUBROUTINE MYSUB1()
implicit none

INTEGER tid, tid2
COMMON /mycommon/ tid

call gettid(tid)
call gettid(tid2)

do while (.TRUE.)
call mysub2(tid2)
enddo

end subroutine

subroutine MYSUB2(tid2)
implicit none

INTEGER tid, tid2
COMMON /mycommon/ tid

if (tid2.ne.tid) then
write(0,*) "Trouble: ", tid, " != " , tid2
else
write(0,*) "."
endif

end subroutine

program testthreads

call spawn_threads()

end program

The threads created and managed by your C++ code are not from the same pool of threads created by the Fortran OpenMP code. Additionally, the technique underwhich thread private data is accessed may differ between C++ and Fortran depending on implementation.

At the very least your problem is spawning multiple threads in C++ then calling your Fortran initialization code (that populates the thread private data) only to have the Fortran code create an additional thread pool and initialize those threadsthread private data. Then later in your code calling a fortran routine from the C++ created thread and examine the un-initialize fortran portion of your thread private data.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,413 Views

If you use OpenMP in the C++ code and build the Fortran with "-openmp-lib compat", then Fortran and C++ will be using the same thread pool and you should be able to do what you want. Note that this will be the default in version 11.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,413 Views

If you use OpenMP in the C++ code and build the Fortran with "-openmp-lib compat", then Fortran and C++ will be using the same thread pool and you should be able to do what you want. Note that this will be the default in version 11.


That is great to know. What happens if you use MS VC++ and IVF? And does it matter if the application is a Fortran PROGRAM or C++ main/winmain?

Jim Dempsey

0 Kudos
TimP
Honored Contributor III
1,413 Views


That is great to know. What happens if you use MS VC++ and IVF? And does it matter if the application is a Fortran PROGRAM or C++ main/winmain?

Jim Dempsey

We're on the linux forum here, but VC9 works interchangeably with ICL, together with Intel OpenMP compatibility library, under Windows. The examples I posted on the threading forum show this (with Fortran main).

0 Kudos
Reply