First of all: This problem was adressed in other questions like how can i keep variables private in subroutines which are called in a parallel section of openmp? and Are local variables in procedures automatically private when using OpenMP?, but my problem remains as follows.
I call a subroutine from inside an OpenMP parallel region and want the local variables be private for every thread, so that changes to local variables in one thread will not affect the other threads. However, I can't make it work in this example:
program omp_private
use omp_lib
implicit none
call test() !initalize temp = 0
!$omp parallel
call test()
!$omp end parallel
contains
subroutine test()
integer :: temp = 0
if (omp_get_thread_num()==1) temp = 1
if (omp_get_thread_num()==0) write(*,*) "In thread ",omp_get_thread_num()," temp is ",temp
end subroutine test
end program omp_private
My output looks usually like this (depending on thread speed)
In thread 0 temp is 0
In thread 0 temp is 1
Adding recursive
nor save
changed anything. I cannot explicitly declare temp
as private since temp
is not defined outside the subroutine.
How could I make it work?