How to declare OpenMP private common block variables?

911 views Asked by At

I am new to Parallel computing and I've been trying to parallelize a sequential Fortran program which I have worked on before, using OpenMP, My main code is similar to this example :

    program main
use omp_lib
implicit none
integer :: i
double precision :: y , test , a(5)
common/y/y
external :: test
a=[1,2,3,4,5]
do i=1,5
    y=a(i)
    print*,test(2.d0)
end do
end program main

double precision function test(x)
   implicit none
   double precision :: y , x
   common/y/y
   test=y*x
   return
end function

I am trying to use !$OMP PARALLEL DO , but I have problem with the variable "y" which is declared global and used in the function "test" but it is changed within each thread. How can I parallelize such a code without declaring "y" as the argument of the function "test"?

Also, is there any way that each thread can carry the value of iteration counter "i" privately, throughout the whole program (including through the functions)?

1

There are 1 answers

0
Russell Jurek On

OMP uses a DEFAULT() type for all variables that exist prior to the start of the OMP construct. The default value of DEFAULT() is shared. That means your common block variables are shared variables by default. Functions inside OMP constructs however use their local scope to create private versions of function local variables. The y variable that you're passing to the function through a COMMON block isn't function local though. That means it retains the shared type, and is common across all threads.

If this was just another variable, then you could resolve this by declaring y as a PRIVATE() variable in the OMP construct. Instead, because this is inside a COMMON block you need to declare y as THREADPRIVATE in every location that references the common block. If you miss it in even one place it'll default to being a shared variable across all of your threads. Assuming it even compiles.