Do COMMON blocks in Fortran have to be declared threadprivate in every subroutine for OpenMP?

364 Views Asked by At

I am modifying some old, old Fortran code to run with OpenMP directives, and it makes heavy use of COMMON block. I have found multiple sources that say that using OMP directives to declare COMMON blocks as THREADPRIVATE solves the issue of COMMON blocks residing in global scope by giving each OpenMP thread its own copy. What I'm unsure of though, is whether the THREADPRIVATE directive needs to be after declaration in every single subroutine, or whether having it in the main (and only) PROGRAM is enough?

1

There are 1 best solutions below

1
On BEST ANSWER

Yes, it must be at every occurrence. Quoting from the OpenMP 5.0 standard

If a threadprivate directive that specifies a common block name appears in one program unit, then such a directive must also appear in every other program unit that contains a COMMON statement that specifies the same name. It must appear after the last such COMMON statement in the program unit.

As a comment putting OpenMP into a program full of global variables is likely to lead to a life of pain. I would at least give some thought to "do I want to start from here" before I begin such an endeavour - modernisation of the code before you add OpenMP might turn out to be an easier and cheaper option, especially in the long run.