Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

[OpenMP - Fortran] Scope of COMMON block variables

Edgardo_Doerner
1,052 Views

Dear all,

Although the answer of the question in the title is, in principle, quite clear, I am confused about the scope (shared or private) of variables declared in COMMON blocks inside functions that are not the main function. For example, I am trying to parallelize a MC Code of particle transport on matter using OpenMP, the main part of this program is like the following code:

PROGRAM TUTOR2
IMPLICIT NONE
[Variable initialization, some COMMON blocks, program initialization routines, etc...]

C$OMP PARALLEL NUM_THREADS (4)
C$OMP SINGLE
      WRITE(*,*) "Number of OpenMP threads: ", OMP_GET_NUM_THREADS()
      DO I=1,NCASE
C$OMP TASK FIRSTPRIVATE(ESCORE)
        CALL SHOWER(IQIN,EIN,XIN,YIN,ZIN,UIN,VIN,WIN,IRIN,WTIN)
C$OMP END TASK
      END DO
C$OMP END SINGLE NOWAIT
C$OMP END PARALLEL

So I have a function call SHOWER() that realizes the simulation of the particle cascade. This function call another functions (RANDOM, PHOTON, ELECTR, etc) that realize the transport calculations. SHOWER and this functions declare some COMMON blocks, but they are not present in the main function of the program. For example, SHOWER could have this structure:

SUBROUTINE SHOWER
COMMON/STACK/ E(15),X(15),Y(15),Z(15),U(15),V(15),W(15),DNEAR(15),
     *WT(15),IQ(15),IR(15),LATCH(15), LATCHI,NP,NPold
[...]
IF (( IQ(NP) .EQ. 0 )) THEN
          CALL PHOTON(ircode)
ELSE
          CALL ELECTR(ircode)
END IF

The STACK COMMON block is not declared in the main part of the program. Then, PHOTON an ELECTR make use of this block, i.e.

SUBROUTINE PHOTON(IRCODE)
COMMON/STACK/ E(15),X(15),Y(15),Z(15),U(15),V(15),W(15),DNEAR(15),
     *WT(15),IQ(15),IR(15),LATCH(15), LATCHI,NP,NPold
[...]

SUBROUTINE ELECTR(IRCODE)
COMMON/STACK/ E(15),X(15),Y(15),Z(15),U(15),V(15),W(15),DNEAR(15),
     *WT(15),IQ(15),IR(15),LATCH(15), LATCHI,NP,NPold
[...]

So, the COMMON block STACK has shared or global scope? (i.e. all the created OpenMP threads see this variables, or have each thread a copy of this block?). Thanks for your help!

0 Kudos
2 Replies
TimP
Honored Contributor III
1,052 Views

common variables are treated as shared, unless you have made a threadprivate copy of the common.  Then you must copy data in and out in order to communicate outside the scope of the threadprivate.

If the common is declared inside procedures invoked only in the parallel region, it's conceivable on some implementation that it could be private in effect, but I don't know any such implementation.

This can get ugly enough to support the recommendations of avoiding common in new software design. 

A problem frequently seen in applications using common, where OpenMP has been added subsequently, is non-local data on a NUMA platform, due to not taking advantage of the first-touch facilities built into OpenMP libraries

You'd probably get more visibility to experts on this subject by asking in the appropriate Fortran forum.

0 Kudos
Edgardo_Doerner
1,052 Views

Thanks for your quick reply, Tim!...

Well, I had the doubt about asking on this forum or the Fortran one, anyway your answer dissipated my doubts about this issue... thanks for your answer!...

By the way, I have read many times the recommendations about avoiding common, but unfortunately I am adapting a quite old code (actually one programs first on MORTRAN and then is pre-compiled to Fortran). Maybe I should do a contribution and adapt the codes to newer standards, hehehe.

 

 

0 Kudos
Reply