SHARED

Purpose

All sections use the same copy of the variables and common blocks you specify in data_scope_entity_list.

The SHARED clause specifies variables that must be available to all threads. If you specify a variable as SHARED, you are stating that all threads can safely share a single copy of the variable.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-SHARED--(--data_scope_entity_list--)------------------------><

data_scope_entity
Read syntax diagramSkip visual syntax diagram
   .-,---------------------------.   
   V                             |   
>>---+-named_variable----------+-+-----------------------------><
     '-⁄--common_block_name--⁄-'     

named_variable
is a named variable that is accessible in the directive construct
common_block_name
is a common block name that is accessible in the directive construct

Rules

A variable in the SHARED clause must not be either:
  • A pointee
  • A runtime size array, which can be an assumed-shape array, an allocatable array, an array pointer, or an explicitly array with runtime bounds
  • A THREADLOCAL common block.
  • A THREADPRIVATE common block or its members.
  • A THREADPRIVATE variable.
If a SHARED variable, a subobject of a SHARED variable, or an object associated with a SHARED variable or subobject of a SHARED variable appears as an actual argument in a reference to a non-intrinsic procedure and:
  • The actual argument is an array section with a vector subscript; or
  • The actual argument is
    • An array section,
    • An assumed-shape array, or,
    • A pointer array
    and the associated dummy argument is an explicit-shape or assumed-size array;
then any references to or definitions of the shared storage that is associated with the dummy argument by any other thread must be synchronized with the procedure reference. In other words, you must structure your code in such a way that if a thread encounters a procedure reference, then the procedure call by that thread and any reference to or definition of the shared storage by any other thread will always occur in the same sequence. You can do this, for example, by placing the procedure reference after a BARRIER.

Example for OpenMP

In the following example, the procedure reference with an array section actual argument is required to be synchronized with references to the dummy argument by placing the procedure reference in a critical section, because the associated dummy argument is an explicit-shape array.
INTEGER :: abc(10)

i = 2
j = 5

!$OMP PARALLEL DEFAULT(NONE), SHARED(abc, i, j)
!$OMP CRITICAL
! Actual argument is an array section. 
! The procedure reference must be in a critical section.
CALL sub1(abc(i: j))
!$OMP END CRITICAL
!$OMP END PARALLEL
  
  CONTAINS
    SUBROUTINE sub1(arr)
      INTEGER:: arr(1: 4)
      DO i = 1, 4
        arr(i) = i
      END DO
    END SUBROUTINE
END


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us