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?
The COMMON statement defines a block of main memory storage so that different program units can share the same data without using arguments.
The THREADPRIVATE directive allows you to specify named common blocks and named variables as private to a thread but global within that thread. Once you declare a common block or variable THREADPRIVATE, each thread in the team maintains a separate copy of that common block or variable.
Example. In the early forms of Fortran the only mechanism for creating global variable store visible from subroutines and functions is to use the COMMON block mechanism. This permitted sequences of variables to be names and shared in common. In addition to named common blocks there may also be a blank (unnamed) common block.
In the early forms of Fortran the only mechanism for creating global variable store visible from subroutines and functions is to use the COMMON block mechanism. This permitted sequences of variables to be names and shared in common. In addition to named common blocks there may also be a blank (unnamed) common block.
If a threadprivate variable or a threadprivate common block is declared with the BIND attribute, the corresponding C entities must also be specified in a threadprivate directive in the C program. A blank common block cannot appear in a threadprivate directive.
OpenMP Directives: THREADPRIVATE Directive 1 Purpose:. The THREADPRIVATE directive is used to make global file scope variables (C/C++) or common blocks (Fortran)... 2 Format:. 3 Notes:. The directive must appear after the declaration of listed variables/common blocks. Each thread then gets its own... 4 Example:. More ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With