XL Fortran for AIX 8.1

Language Reference

ATOMIC

Purpose

You can use the ATOMIC directive to update a specific memory location safely within a parallel region. When you use ATOMIC, you ensure that only one thread is writing to the memory location at a time, avoiding errors which might occur from simultaneous writes to the same memory location.

Normally, you would protect a shared variable within a CRITICAL construct if it is being updated by more than one thread at a time. However, certain platforms support atomic operations for updating variables. For example, some platforms might support a hardware instruction that reads from a memory location, calculates something and writes back to the location all in one atomic action. The ATOMIC directive instructs the compiler to use an atomic operation whenever possible. Otherwise, the compiler will use some other mechanism to perform an atomic update.

The ATOMIC directive only takes effect if you specify the -qsmp compiler option.

Format



>>-ATOMIC------------------------------------------------------><
 
 
>>-atomic_statement--------------------------------------------><
 
 

where atomic_statement is:



>>-+-update_variable--=--update_variable--operator--expression-----------+-><
   +-update_variable--=--expression--operator--update_variable-----------+
   +-update_variable--=--intrinsic--(--update_variable--,--expression--)-+
   '-update_variable--=--intrinsic--(--expression--,--update_variable--)-'
 
 

update_variable
is a scalar variable of intrinsic type.

intrinsic
is one of max, min, iand, ior or ieor.

operator
is one of +, -, *, /, .AND., .OR., .EQV., .NEQV. or .XOR.

expression
is a scalar expression that does not reference update_variable.

Rules

The ATOMIC directive applies only to the statement which immediately follows it.

The expression in an atomic_statement is not evaluated atomically. You must ensure that no race conditions exist in the calculation.

All references to the same update_variable that follow an ATOMIC directive must have the same data type and type parameters.

The function intrinsic, the operator operator, and the assignment must be the intrinsic function, operator and assignment and not a redefined intrinsic function, defined operator or defined assignment.

Examples

Example 1: In the following example, multiple threads are updating a counter. ATOMIC is used to ensure that no updates are lost.

      PROGRAM P
        R = 0.0
!$OMP   PARALLEL DO SHARED(R)
        DO I=1, 10
!$OMP     ATOMIC
          R = R + 1.0
        END DO
        PRINT *,R
      END PROGRAM P
 

Expected output:

10.0

Example 2:In the following example, an ATOMIC directive is required, because it is uncertain which element of array Y will be updated in each iteration.

      PROGRAM P
        INTEGER, DIMENSION(10) :: Y, INDEX
        INTEGER B
        Y = 5
        READ(*,*) INDEX, B
!$OMP   PARALLEL DO SHARED(Y)
        DO I = 1, 10
!$OMP     ATOMIC
          Y(INDEX(I)) = MIN(Y(INDEX(I)),B)
        END DO
        PRINT *, Y
      END PROGRAM P
 

Input data:

10 10 8 8 6 6 4 4 2 2   4
 

Expected output:

5 4 5 4 5 4 5 4 5 4
 

Example 3: The following example is invalid, because you cannot use an ATOMIC operation to reference an array.

      PROGRAM P
           REAL ARRAY(10)
           ARRAY = 0.0
     !$OMP PARALLEL DO SHARED(ARRAY)
           DO I = 1, 10
     !$OMP   ATOMIC
             ARRAY = ARRAY + 1.0
           END DO
           PRINT *, ARRAY
           END PROGRAM P 

Example 4: In the following invalid example, the ATOMIC operation attempts to reference R twice, which is illegal. The expression and update_variable cannot be identical.

PROGRAM P
           R = 0.0
     !$OMP PARALLEL DO SHARED(R)
           DO I = 1, 10
     !$OMP   ATOMIC
             R = R + R
           END DO
           PRINT *, R
           END PROGRAM P
 

Related Information


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