Summary of OpenMP Directives in Fortran 95
| Directive Format (Fixed) |
C$OMP directive
optional_clauses...
!$OMP directive optional_clauses...
*$OMP directive optional_clauses...
Must start in column one; continuation lines must have a non-blank or
non-zero character in column 6 |
|
|
|
| Directive Format (Free) |
!$OMP directiveoptional_clauses...
May appear anywhere, preceded by whitespace; continuation lines are
identified with an ampersand: !$OMP& |
|
| Conditional Compilation |
Source lines beginning with !$, C$,
or *$ in columns 1 and 2 (fixed format), or !$ preceded
by white space (free format) are compiled only when compiler option -openmp,
or -mp=openmp is specified. |
| PARALLEL
Directive |
!$OMP PARALLEL [clause[[,]
clause]...]
block of Fortran statements with no
transfer in or out of block
!$OMP END PARALLEL
Defines a parallel region: a block of code that is to be executed
by multiple threads in parallel. clause
can be one of the following: PRIVATE(list),
SHARED(list),
DEFAULT(option),
FIRSTPRIVATE(list),
REDUCTION(list),
IF(expression),
COPYIN(list). |
| DO Directive |
!$OMP DO [clause[[,]
clause]...]
do_loop statements block
[!$OMP END DO [NOWAIT]]
The DO directive specifies that the iterations of the DO loop
that immediately follows must be executed in parallel. This directive must
appear within a parallel region. clause
can be one of the following: PRIVATE(list),
FIRSTPRIVATE(list),
LASTPRIVATE(list),
REDUCTION(list),
SCHEDULE(type),
ORDERED. |
| SECTIONS Directive |
!$OMP SECTIONS [clause[[,]
clause]...]
[!$OMP SECTION]
block of statements with no transfer
in or out
[!$OMP SECTION
optional block of statements
]
...
!$OMP END SECTIONS [NOWAIT]
Encloses a non-iterative section of code to be divided among threads
in the team. Each section is executed once by a thread in the team. clause
can be one of the following: PRIVATE(list),
FIRSTPRIVATE(list),
LASTPRIVATE(list),
REDUCTION(list).
Each section is preceded by a SECTION directive, which is optional
for the first section. |
| SINGLE Directive |
!$OMP SINGLE [clause[[,]
clause]...]
block of statements with no transfer
in or out
!$OMP END SINGLE [NOWAIT]
The statements enclosed by SINGLE is to be executed by only
one thread in the team. Threads in the team that are not executing the
SINGLE
block of statements wait at the END SINGLE directive
unless NOWAIT is specified. clause
can be one of: PRIVATE(list),
FIRSTPRIVATE(list). |
| PARALLEL DO Directive |
!$OMP PARALLEL DO [clause[[,]
clause]...]
do_loop statements block
[!$OMP END PARALLEL DO [NOWAIT]]
Shortcut for specifying a parallel region that contains a single DO
loop: a PARALLEL directive followed immediately by a DO
directive. clause can be any
of the clauses accepted by the PARALLEL and DO directives. |
| PARALLEL SECTIONS Directive |
!$OMP PARALLEL SECTIONS [clause[[,]
clause]...]
[!$OMP SECTION]
block of statements with no transfer
in or out
[!$OMP SECTION
optional block of statements
]
...
!$OMP END PARALLEL SECTIONS [NOWAIT]
Shortcut for specifying a parallel region that contains a single SECTIONS
directive: a PARALLEL directive followed by a SECTIONS
directive. clause can be any
of the clauses accepted by the PARALLEL and SECTIONS
directives. |
| Synchronization Directives |
| MASTER Directive |
!$OMP MASTER
block of statements with no transfers
in or out
!$OMP END MASTER
The block of statements enclosed by these directives is executed only
by the master thread of the team. The other threads skip this block and
continue. There is no implied barrier on entry to or exit from the master
section. |
| CRITICAL Directive |
!$OMP CRITICAL [(name)]
block of statements with no transfers
in or out
!$OMP END CRITICAL [(name)]
Restrict access to the statement block enclosed by these directives
to only one thread at a time. The optional name
argument identifies the critical region. All unnamed CRITICAL
directives map to the same name. Critical section names are global entities
of the program. If a name conflicts with any other entity, the behavior
of the program is undefined. If name
appears on the CRITICAL directive, it must also appear on the
END
CRITICAL directive. |
| BARRIER Directive |
!$OMP BARRIER
Synchronizes all the threads in a team. Each thread waits until all
the others in the team have reached this point. |
| ATOMIC Directive |
!$OMP ATOMIC
Ensures that a specific memory location is to be updated atomically,
rather than exposing it to the possibility of multiple, simultaneous writing
threads.
The directive applies only to the immediately following statement, which
must be one of these forms:
x = x operator expression
x = expression operator x
x = intrinsic(x, expression)
x = intrinsic(expression, x)
where:
· x is a scalar of
intrinsic type
· expression is a
scalar expression that does not reference x
· intrinsic is one
of MAX, MIN, IAND, IOR, or IEOR.
· operator is one
of + - * / .AND. .OR. .EQV. .NEQV. |
| FLUSH Directive |
!$OMP FLUSH [(list)]
Thread-visible variables are written back to memory at the point at
which this directive appears. This includes global variables (common blocks
and modules), local variables (without the SAVE attribute) passed to a
subprogram or declared SHARED in a parallel region in the subprogram, dummy
arguments, and all pointer dereferences. The optional list
consists of a comma-separated list of variables that need to be flushed. |
| ORDERED Directive |
!$OMP ORDERED
block of statements with no transfers
in or out
!$OMP END ORDERED
The enclosed block of statements are executed in the order that iterations
would be executed in a sequential execution of the loop. It can appear
only in the dynamic extent of a DO or PARALLEL DO directive.
The ORDERED clause must be specified on the closest DO
directive enclosing the block. |
| Data Environment Directives |
| THREADPRIVATE Directive |
!$OMP THREADPRIVATE (/cb/[,/cb/]...)
Makes the named common blocks private to a thread but global within
the thread. cb is the name of
the common block. To make a common block THREADPRIVATE, this directive
must appear after every COMMON declaration of that block.
|
| Data Scoping Clauses |
| Several directives noted above accept
clauses to control the scope attributes of variables enclosed by the directive.
If no data scope clause is specified for a directive, the default scope
for variables affected by the directive is SHARED. list
is a comma-separated list of named variables or common blocks that are
accessible in the scoping unit. Common block names must appear within slashes
(for example, /ABLOCK/) |
| PRIVATE Clause |
PRIVATE(list)
Declares the variables in the comma separated list
to be private to each thread in a team. |
| SHARED Clause |
SHARED(list)
All the threads in the team share the variables that appear in list,
and access the same storage area. |
| DEFAULT Clause |
DEFAULT(PRIVATE | SHARED | NONE)
Specify scoping attribute for all variables within a parallel region.
THREADPRIVATE
variables are not affected by this clause. If not specified, DEFAULT(SHARED)
is assumed. |
| FIRSTPRIVATE Clause |
FIRSTPRIVATE(list)
Variables on list are PRIVATE. In addition, private copies
of the variables are initialized from the original object existing before
the construct. |
| LASTPRIVATE Clause |
LASTPRIVATE(list)
Variables on the list are PRIVATE. In addition, when the LASTPRIVATE
clause appears on a DO directive, the thread that executes the sequentially
last iteration updates the version of the variable before the construct.
On a SECTIONS directive, the thread that executes the lexically
last SECTION updates the version of the object it had before the
construct. |
| REDUCTION Clause |
REDUCTION({operator|intrinsic}:list)
operator is one of: +
* - .AND. .OR. .EQV. .NEQV.
intrinsic is one of: MAX
MIN IAND IOR IEOR
Variables in list must be
named scalar variables of intrinsic type.
The REDUCTION clause is intended to be used on a region in
which the reduction variable is used only in reduction statements of the
form shown previously for the ATOMIC directive. Variables on list
must be SHARED in the enclosing context. A private copy of each
variable is created for each thread as if it were PRIVATE. At
the end of the reduction, the shared variable is updated by combining the
original value with the final value of each of the private copies. |
| COPYIN Clause |
COPYIN(list)
The COPYIN clause applies only to common blocks that are declared
as THREADPRIVATE. In a parallel region, COPYIN specifies
that the data in the master thread of the team be copied to the thread
private copies of the common block at the beginning of the parallel region. |
| Scheduling Clauses on DOand
PARALLEL DO Directives |
| SCHEDULE Clause |
SCHEDULE(type
[,chunk])
Specifies how iterations of the DO loop are divided among the threads
of the team. type can be one
of the following. In the absence of a SCHEDULE clause, STATIC
scheduling is used. |
| STATIC Scheduling |
SCHEDULE(STATIC, chunk)
Iterations are divided into pieces of a size specified by chunk.
The pieces are statically assigned to threads in the team in a round-robin
fashion in the order of the thread number. chunk
must be a scalar integer expression. |
| DYNAMIC Scheduling |
SCHEDULE(DYNAMIC, chunk)
Iterations are broken into pieces of a size specified by chunk.
As each thread finishes a piece of the iteration space, it dynamically
obtains the next set of iterations. |
| GUIDED Scheduling |
SCHEDULE(GUIDED, chunk)
With GUIDED, the chunk
size is reduced in an exponentially decreasing manner with each dispatched
piece of the iterations. chunk
specifies the minimum number of iterations to dispatch each time. |
| RUNTIME Scheduling |
SCHEDULE(RUNTIME)
Scheduling is deferred until runtime. Schedule type
and chunk size will be determined
from the setting of the OMP_SCHEDULE environment variable. |
Summary of Fortran 95 OpenMP Library Routines
| Execution Environment Routines |
| OMP_SET_NUM_THREADS Subroutine |
|
SUBROUTINE OMP_SET_NUM_THREADS(int_expr)
Sets the number of threads to use for the next parallel region. |
| OMP_GET_NUM_THREADS Function |
|
INTEGER FUNCTION OMP_GET_NUM_THREADS()
Returns the number of threads currently in the team executing the parallel
region from which it is called. |
| OMP_GET_MAX_THREADS Function |
|
INTEGER FUNCTION OMP_GET_MAX_THREADS()
Returns the maximum value that can be returned by calls to the OMP_GET_NUM_THREADS
function. |
| OMP_GET_THREAD_NUM Function |
|
INTEGER FUNCTION OMP_GET_THREAD_NUM()
Returns the thread number within the team. This is a number between
0 and OMP_GET_NUM_THREADS()-1.The master thread is thread 0. |
| OMP_GET_NUM_PROCS Function |
|
INTEGER FUNCTION OMP_GET_NUM_PROCS()
Returns the number of processors that are available to the program. |
| OMP_IN_PARALLEL Function |
|
LOGICAL FUNCTION OMP_IN_PARALLEL()
Returns .TRUE. if called from within the dynamic extent of
a region executing in parallel, and .FALSE. otherwise. |
| OMP_SET_DYNAMIC Subroutine |
|
SUBROUTINE OMP_SET_DYNAMIC(logical_expr)
Enables or disables dynamic adjustment of the number of threads available
for parallel execution of programs. |
| OMP_GET_DYNAMIC Function |
|
LOGICAL FUNCTION OMP_GET_DYNAMIC()
Returns .TRUE. if dynamic thread adjustment is enabled and
returns .FALSE. otherwise. |
| OMP_SET_NESTED Subroutine |
|
SUBROUTINE OMP_SET_NESTED(logical_expr)
Enables or disables nested parallelism. |
| OMP_GET_NESTED Function |
|
FUNCTION OMP_GET_NESTED()
Returns .TRUE. if nested parallelism is enabled, .FALSE.
otherwise. |
| Lock Routines
The lock variable var must be
accessed only through these routines. var
should be of type integer and of a KIND large enough to hold an
address. For example, on a 64-bit system, var
may be declared as INTEGER(KIND=8) |
| OMP_INIT_LOCK Subroutine |
|
SUBROUTINE OMP_INIT_LOCK(var)
Initializes a lock associated with lock variable var
for use in subsequent calls. The initial state is unlocked. |
| OMP_DESTROY_LOCK Subroutine |
|
SUBROUTINE OMP_DESTROY_LOCK(var)
Disassociates the given lock variable var
from any locks. |
| OMP_SET_LOCK Subroutine |
|
SUBROUTINE OMP_SET_LOCK(var)
Forces the executing thread to wait until the specified lock is available.
The thread is granted ownership of the lock when it is available. |
| OMP_UNSET_LOCK Subroutine |
|
SUBROUTINE OMP_UNSET_LOCK(var)
Releases the executing thread from ownership of the lock. Behavior
is undefined if the thread does not own that lock. |
| OMP_TEST_LOCK Function |
|
LOGICAL FUNCTION OMP_TEST_LOCK(var)
Attempts to set the lock associated with lock variable var.
Returns .TRUE. if the lock was set successfully, .FALSE.
otherwise. |