# SYNC MEMORY

Statement: Ends one image segment and begins another. Each segment can then be ordered in some way with respect to segments on other images.

The SYNC MEMORY statement takes the following form:

SYNC MEMORY [([STAT=stat-var][, ERRMSG=err-var])]

stat-var | Is a scalar integer variable in which the status of the synchronization is stored.
err-var | Is a scalar default character variable in which an error condition is stored if such a condition occurs.

STAT= and ERRMSG= can appear in either order, but only once in a SYNC IMAGES statement.

## Description

Unlike the other image control statements, this statement does not have any built-in synchronization effect.

The action regarding X on image Q precedes the action regarding Y on image Q if, by execution of statements on image P, the following is true:

- A variable X on image Q is defined, referenced, becomes undefined, or has its allocation status, pointer association status, array bounds, dynamic type, or type parameters changed or inquired about by execution of a statement

- That statement precedes a successful execution of a SYNC MEMORY statement

- A variable Y on image Q is defined, referenced, becomes undefined, or has its allocation status, pointer association status, array bounds, dynamic type, or type parameters changed or inquired about by execution of a statement that succeeds execution of that SYNC MEMORY statement,

User-defined ordering of segment Pi on image P to precede segment Qj on image Q occurs when the following happens:

- Image P executes an image control statement that ends segment Pi, and then executes statements that initiate a cooperative synchronization between images P and Q

- Image Q executes statements that complete the cooperative synchronization between images P and Q and then executes an image control statement that begins segment Qj

Execution of the cooperative synchronization between images P and Q must include a dependency that forces execution on image P of the statements that initiate the synchronization to precede the execution on image Q of the statements that complete the synchronization. The mechanisms available for creating such a dependency are processor dependent.

### Note

SYNC MEMORY usually suppresses compiler optimizations that may reorder memory operations across the segment boundary defined by the SYNC MEMORY statement. It ensures that all memory operations initiated in the preceding segments in its image complete before any memory operations in the subsequent segment in its image are started.

## Example

The following example should be run on two images:

    use, intrinsic :: iso_fortran_env
    logical (atomic_logical_kind), save :: locked[*] = .true.
    logical val
    integer :: iam

    iam = this_image()

    if (iam == 1) then
       sync memory
       call atomic_define(locked[2], .false.)
    else if (iam == 2) then
       val = .true.
       do while (val)
          call atomic_ref(val, locked)
       end do
       sync memory
    end if

    print *, 'success'
    end

Parent topic: S

## See Also

Image Control Statements
Coarrays
Using Coarrays