Community
cancel
Showing results for 
Search instead for 
Did you mean: 
maria
Beginner
115 Views

Modules marked as PRIVATE in openMP


I have a question regarding using MODULE in openMP.

Our program can be simplied by the following example. The parallel region iswithin subroutine SUB1. Variables A & B are from VAR1_ARRAYS modules. To remove the data dependence,we madeA & B as private. However, we found that A & B values are the same from different thread even though they should be different. Clearly A & B values are shared among the threads.

Within subroutine SUB1, variables A & B were not used even though they were marked asPRIVATE at he beginning of the parallel region. I am wondering if private will not work for this kinds of MODULES? Dowe have to pass A & B parameters in CALL SUB2(A,B) and CALL SUB3(A,B) instead ofusing MODULE?Or we have a bug somewhere?


MAIN program

call SUB1


.......
STOP
END

SUBROUTINE SUB1
USE VAR1_ARRAYS, ONLY: A, B
......
!$OMP parallel do private(A,B)
DO I = 1, II
CALL SUB2
CALL SUB3
ENDDO
RETURN
END

SUBROUTINE SUB2
USE VAR1_ARRAYS, ONLY: A, B
...
A=...
B=...
RETURN
END

SUBROUTINE SUB3
USE VAR1_ARRAYS, ONLY: A, B
...

C=A*B
...
RETURN
END


SUBROUTINEMODULES

MODULE VAR1_ARRAYS
REAL A, B
END MODULE

RETURN
END

0 Kudos
5 Replies
TimP
Black Belt
115 Views

Unfortunately, this question isn't covered by references on OpenMP which I have available. It looks reasonable to expect your code to produce private copies of the variables, or at least a warning from the compiler that it can't do so, or that you need threadprivate, if that is the case. Of course, if private works, you would need firstprivate and lastprivate in order to communicate values in and out of the parallel region; without such usage, it doesn't make sense to use module variables.
0 Kudos
jimdempseyatthecove
Black Belt
115 Views

Steve,

I would expect that

REAL :: A, B
COMMON /FOO/ A, B
...
!$OMP PARALLEL DO PRIVATE(A,B)


Should provide for seperate storage for A and B
*** within the scope of the parallel region
*** and subsequently as dummy args when calling subroutine from parallel region
*** but not as common when calling subroutine within parallel region

The same behavior should apply to module data

What the user wants from the code sketch would be best served using THREADPRIVATE data (as you suggested).

*** cauton about thread private data ***

Learn how to use thread private data, it is your friend provided you use it correctly

1) Each thread has a seperateinstance of thread private data.
2) As contradictory as it may seem, use SHARED(xxx) for thread private data.
In this context SHARED means the parallel region is to use the variables
as-was expressed in the serial section (or parallel nested region prior to entering
next nested parallel region containing SHARED clause). Due to the fact that
the serial code has seperate instances of the thread private data
the as-was placement of the data was private per thread.
Marking as PRIVATE would make another instance of this data.
(potentially on the stack and uninitialized)
3) If you exit a parallel region and enter another parallel region
there is no assurance that the omp_get_thread_num() team members
of one team are the same threads of the same team member numbers
running in the other parallel region(s). Additionally, the two (multiple)
regions may have a different number of threads.
i.e. make sure allocations and initializations, if required,have beendone.
This may require insertion of
IF(.NOT. ALLOCATED(...
IF(.NOT. YourOnceOnlyCodeFlagInThreadPrivate) CALL YourOnceOnlyInit()

into your code

Once you familiarize your self with thread private data, it is relatively easy to use.

Jim Dempsey
0 Kudos
maria
Beginner
115 Views

Threadprivate works for this case. When declaring the module, I added threadprivate. That's the only change I make. Now the program works. variables A and B are temporary variables that are only
assigned inside the parallel region.

Just want to confirm one thing, I don't need to declare threadprivate inside parallel regionafter USE VAR1_ARRAYS from every subroutine, right?


*************
MODULE VAR1_ARRAYS
REAL A,B
!$OMP threadprivate(a,b)
END MODULE
****************
0 Kudos
TimP
Black Belt
115 Views

This makes sense to me, but I can't find references to back it up.
0 Kudos
jimdempseyatthecove
Black Belt
115 Views

I suggest you run a test to illustrate

subroutine foo
use var1_arrays
write(*,*) 'A located at', LOC(A)
!$OMP PARALLEL DEFAULT(PRIVATE)
!$OMP CRITICAL
write(*,*) 'A located at', LOC(A), OMP_GET_THREAD_NUM()
!$OMP END CRITICAL
!$OMP END PARALLEL
end subroutine foo

It is not clear as to if DEFAULT(PRIVATE) applies to threadprivate variables
I would assume that I will assume that it does

!$OMP PARALLEL DEFAULT(PRIVATE) SHARED(A,B)

would explicitly state that A and B were the variables as-were outside the scope of the parallel region.

Jim Dempsey
0 Kudos