Fortran User's Guide HomeContentsPreviousNextIndex


Appendix E

Fortran Directives Summary

This appendix summarizes the directives recognized by the f77 and f95 Fortran compilers:

General Fortran Directives

General directives accepted by both f77 and f95 are described in Chapter 2.

TABLE E-1   Summary of General Fortran Directives  
Format
C$PRAGMA keyword ( a [ , a ] ... ) [ , keyword ( a [ , a ] ... ) ] ,... 
C$PRAGMA SUN
keyword ( a [ , a ] ... ) [ , keyword ( a [ , a ] ... ) ] ,...
C$PRAGMA SPARC
keyword ( a [ , a ] ... ) [ , keyword ( a [ , a ] ... ) ] ,...

Comment-indicator in column 1 may be c, C, !, or *. (We use C in these examples. f95 free-format must use !.)
C Directive C$PRAGMA C(list)

Declares a list of names of external functions as C language routines.
UNROLL Directive C$PRAGMA SUN UNROLL=n

Advises the compiler that the following loop can be unrolled to a length n.
WEAK Directive C$PRAGMA WEAK(name[=name2])

Declares name to be a weak symbol, or an alias for name2.
OPT Directive C$PRAGMA SUN OPT=n

Set optimization level for a subprogram to n.
PIPELOOP Directive C$PRAGMA SUN PIPELOOP=n

Assert dependency in loop between iterations n apart.
PREFETCH Directives C$PRAGMA SPARC_PREFETCH_READ_ONCE (name)
C$PRAGMA SPARC_PREFETCH_READ_MANY (
name)
C$PRAGMA SPARC_PREFETCH_WRITE_ONCE (
name)
C$PRAGMA SPARC_PREFETCH_WRITE_MANY (
name)

Request compiler generate prefetch instructions for references to name. (Requires -xprefetch option.)


Special Fortran 95 Directives

The following directives are only available with f95.

TABLE E-2   Special Fortran 95 Directives
Format !DIR$ directive              : initial line
!DIR$&
...                       : continuation line

With fixed-format source, C is also accepted as a directive-indicator: CDIR$ directive... ; the line must start in column 1.
WIth free-format source, the line may be preceded by blanks.
FIXED/FREE Directives !DIR$ FREE
!DIR$ FIXED

These directives specify the source format of the lines following the directive. They apply to the rest of the source file in which they appear, up to the next FREE or FIXED directive.


See Appendix C for details.

Sun Parallelization Directives

Sun-style parallelization directives are the default (-mp=sun compiler option), and are detailed in the chapter on parallelization in the Fortran Programming Guide.

TABLE E-3   Sun-Style Parallelization Directives Summary
Format C$PAR directive [optional_qualifiers] : initial line
C$PAR& [more_qualifiers]           : continuation line

Fixed format, the directive-indicator may be C (as shown), c, *, or !.
Separate multiple qualifiers with commas. Characters beyond column 72 ignored unless -e compiler option specified.
TASKCOMMON Directive C$PAR TASKCOMMON block_name

Declares variables in common block block_name as thread-private: private to a thread, but global within the thread. Declaring a common block TASKCOMMON requires that this directive appear after every common declaration of that block.
DOALL Directive C$PAR DOALL [qualifiers]

Parallelize DO loop that follows. Qualifiers are:
  PRIVATE(list)      declare names on list PRIVATE
  SHARED(list)             declare names on list SHARED
  MAXCPUS(n)     use no more than n threads
  READONLY(list)        listed variables not modified in loop
  SAVELAST               save last value of all private variables
  STOREBACK(list)    save last value of listed variables
  REDUCTION(list)     listed variables are reduction variables
  SCHEDTYPE(type)    use scheduling type: (default is STATIC)
                                         STATIC
                                         SELF(nchunk)
                                         FACTORING[(m)]
                                            GSS[(m)]
DOSERIAL Directive C$PAR DOSERIAL

Disables parallelization of the loop that follows.


Cray Parallelization Directives

Cray-style parallelization directives are detailed in the chapter on parallelization in the Fortran Programming Guide. Requires -mp=cray compiler option.

TABLE E-4   Cray Parallelization Directives Summary  
Format CMIC$ directive qualifiers              : initial line
CMIC$& [more_qualifiers]                   : continuation line

Fixed format. Directive-indicator may be C (as shown here), c, *, or !. With f95 free-format, leading blanks can appear before !MIC$.
DOALL Directive CMIC$ DOALL SHARED(list), PRIVATE(list) [, more_qualifiers]

Parallelize loop that follows. Qualifiers are:

Scoping qualifiers are required (unless list is empty)--all variables in the loop must appear in a PRIVATE or SHARED clause:
PRIVATE(list)    declare names on list PRIVATE
   SHARED(list)   declare names on list SHARED
  AUTOSCOPE   automatically determine scope of variables

The following are optional:
   MAXCPUS(n)          use no more than n threads
  SAVELAST           save last value of all private variables

Only one scheduling qualifier may appear:
   GUIDED equivalent to Sun-style GSS(64)
   SINGLE equivalent to Sun-style SELF(1)
   CHUNKSIZE(n) equivalent to Sun-style SELF(n)
   NUMCHUNKS(m) equivalent to Sun-style SELF(n/m)
The default scheduling is Sun-style STATIC, for which there is no Cray-style equivalent. Interpretations of these scheduling qualifiers differ between Sun and Cray style. Check the Fortran Programming Guide for details.
TASKCOMMON Directive CMIC$ TASKCOMMON block_name

Declares variables in the named common block as thread-private-- private to a thread, but global within the thread. Declaring a common block TASKCOMMON requires that this directive appear immediately before or after every common declaration of that block.
DOSERIAL Directive CMIC$ DOSERIAL

Disables parallelization of the loop that follows.
DOSERIAL* Directive CMIC$ DOSERIAL*

Disables parallelization of the loop nest that follows.


Fortran 95 OpenMP Directives

The Sun Fortran 95 compiler supports the OpenMP version 1.1 Fortran API. The -mp=openmp and -openmp f95 compiler flags enable these directives.

This section lists the OpenMP directives, library routines, and environment variables supported by f95. For details on OpenMP, see the OpenMP Fortran specification at http://www.openmp.org/.

The following table summarizes the OpenMP directives supported by f95. Items enclosed in square brackets ([...]) are optional.

TABLE E-5   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 directive optional_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 DO and 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.


OpenMP Library Routines

OpenMP Fortran API library routines are external procedures. In the following summary, int_expr is a default scalar integer expression, and logical_expr is a default scalar logical expression. The return values of these routines are also of default kind. For details see the OpenMP specifications.

TABLE E-6   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.


OpenMP Environment Variables

The following table summarizes the OpenMP Fortran API environment variables that control the execution of OpenMP programs.

TABLE E-7   Summary of OpenMP Fortran Environment Variables
OMP_SCHEDULE
Sets schedule type for DO and PARALLEL DO directives specified with schedule type RUNTIME. Example: setenv OMP_SCHEDULE "GUIDED,4". If not defined, a default value of STATIC is used.
OMP_NUM_THREADS
Sets the number of threads to use during execution, unless set by a call to OMP_SET_NUM_THREADS() subroutine. Example: setenv OMP_NUM_THREADS 16
If not set, a default of 1 is used.
OMP_DYNAMIC
Enables or disables dynamic adjustment of the number of threads available for execution of parallel regions. Example: setenv OMP_DYNAMIC FALSE
If not set, a default value of TRUE is used.
OMP_NESTED
Enables or disables nested parallelism. Example: setenv OMP_NESTED TRUE



Sun Microsystems, Inc.
Copyright information. All rights reserved.
Feedback
Library   |   Contents   |   Previous   |   Next   |   Index