XL Fortran for AIX 8.1

Language Reference

PARALLEL / END PARALLEL

Purpose

The PARALLEL construct enables you to define a block of code that can be executed by a team of threads concurrently. The PARALLEL construct includes a PARALLEL directive that is followed by one or more blocks of code, and ends with an END PARALLEL directive.

The PARALLEL and END PARALLEL directives only take effect if you specify the -qsmp compiler option.

Format



             .----------------------------.
             V                            |
>>-PARALLEL----+------------------------+-+--------------------><
               '-+---+--parallel_clause-'
                 '-,-'
 
 
>>-block-------------------------------------------------------><
 
 
>>-END PARALLEL------------------------------------------------><
 
 

where parallel_clause is:



>>-+-copyin_clause-----------------+---------------------------><
   +-default_clause----------------+
   +-firstprivate_clause-----------+
   +-IF--(--scalar_logical_expr--)-+
   +-num_threads_clause------------+
   +-private_clause----------------+
   +-reduction_clause--------------+
   '-shared_clause-----------------'
 
 

copyin_clause
See -- COPYIN

default_clause
See -- DEFAULT

firstprivate_clause
See -- FIRSTPRIVATE.

IF(scalar_logical_expr)

If you specify the IF clause, the run-time environment performs a test to determine whether to run the block in serial or parallel. If scalar_logical_expr is true, then the block is run in parallel; if not, then the block is run in serial.

num_threads_clause
See -- NUM_THREADS.

private_clause
See -- PRIVATE.

reduction_clause
See -- REDUCTION

shared_clause
See -- SHARED

Rules

It is illegal to branch into or out of a PARALLEL construct.

The IF and DEFAULT clauses can appear at most once in a PARALLEL directive.

An IF expression is evaluated outside of the context of the parallel construct. Any function reference in the IF expression must not have side effects.

A variable that appears in the REDUCTION clause of an enclosing PARALLEL construct cannot appear in the FIRSTPRIVATE, LASTPRIVATE, or PRIVATE clause of an enclosing PARALLEL construct, and cannot be made PRIVATE by using the DEFAULT clause in the inner PARALLEL construct.

You should be careful when you perform input/output operations in a parallel region. If multiple threads execute a Fortran I/O statement on the same unit, you should make sure that the threads are synchronized. If you do not, the behavior is undefined. Also note that although in the XL Fortran implementation each thread has exclusive access to the I/O unit, the OpenMP specification does not require exclusive access.

Directives that bind to a parallel region will bind to that parallel region even if it is serialized.

The END PARALLEL directive implies the FLUSH directive.

Examples

Example 1: An example of an inner PARALLEL directive with the PRIVATE clause enclosing the PARALLEL construct. Note: The SHARED clause is present on the inner PARALLEL construct.

!$OMP PARALLEL PRIVATE(X)
!$OMP DO
          DO I = 1, 10
            X(I) = I
!$OMP PARALLEL SHARED (X,Y)
!$OMP DO
      DO K = 1, 10
         Y(K,I)= K * X(I)
      END DO
!$OMP END DO
!$OMP END PARALLEL
      END DO
!$OMP END DO
!$OMP END PARALLEL

Example 2: An example showing that a variable cannot appear in both a PRIVATE, and SHARED clause.

!$OMP PARALLEL
PRIVATE(A), SHARED(A)
!$OMP DO
      DO I = 1, 1000
         A(I) = I * I
      END DO
!$OMP END DO
!$OMP END PARALLEL

Example 3: This example demonstrates the use of the COPYIN clause. Each thread created by the PARALLEL directive has its own copy of the common block BLOCK. The COPYIN clause causes the initial value of FCTR to be copied into the threads that execute iterations of the DO loop.

      PROGRAM TT
      COMMON /BLOCK/ FCTR
      INTEGER :: I, FCTR
!$OMP THREADPRIVATE(/BLOCK/)
      INTEGER :: A(100)
 
      FCTR = -1
      A = 0
 
!$OMP PARALLEL COPYIN(FCTR)
!$OMP DO
      DO I=1, 100
         FCTR = FCTR + I
         CALL SUB(A(I), I)
      ENDDO
!$OMP END PARALLEL
 
      PRINT *, A
      END PROGRAM
 
      SUBROUTINE SUB(AA, J)
      INTEGER :: FCTR, AA, J
      COMMON /BLOCK/ FCTR
!$OMP THREADPRIVATE(/BLOCK/)    ! EACH THREAD GETS ITS OWN COPY
                                ! OF BLOCK.
      AA = FCTR
      FCTR = FCTR - J
      END SUBROUTINE SUB

The expected output is:

0 1 2 3 ... 96 97 98 99

Related Information


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]