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 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.
-
It depends on what they are being used for, though. If there's a common block which contains only variables which describe the problem parameters which are not updated inside the parallel region, that should be shared. Similarly if the common block holds large arrays but inside the parallel region each thread operates on a completely disjoint subset of each array, that can also be shared. The big problem is when a common block is used to avoid passing multiple arguments to a function/subroutine by packing them into the common. Then you'll need the common block to be private (or firstprivate) Jun 14, 2021 at 7:50