Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 305
Frank Avatar asked Jun 11 '21 01:06

Frank


People also ask

What is a common block in Fortran?

The COMMON statement defines a block of main memory storage so that different program units can share the same data without using arguments.

What is thread private?

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.

What is a common block in Fortran?

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.

How to create global variables from subroutines in Fortran?

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.

Can a blank threadprivate variable appear in a threadprivate directive?

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.

What is the purpose of the threadprivate directive in OpenMP?

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 ...


1 Answers

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.

like image 134
Ian Bush Avatar answered Dec 29 '22 09:12

Ian Bush