# SYNC IMAGES

Statement: Performs a synchronization of the image with each of the other images in the image set.

The SYNC IMAGES statement takes the following form:

SYNC IMAGES (image-set[, STAT=stat-var][, ERRMSG=err-var])

image-set | Is an integer expression or *. If it is an integer expression, it must be scalar or of rank one. If it is an array expression, the value of each element must be positive and not greater than the number of images; there must be no repeated values. If it is a scalar expression, the value must be positive and not greater than the number of images. If * is specified, it indicates all images.
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

When SYNC IMAGES statements are executed on images C and G, they correspond if the number of times image C has executed a SYNC IMAGES statement in the current team with G in its image set is the same as the number of times image G has executed a SYNC IMAGES statement with C in its image set in this team. The segments that executed before the SYNC IMAGES statement on either image, precede the segments that execute after the corresponding SYNC IMAGES statement on the other image.

In a program that uses SYNC ALL as its only synchronization mechanism, every SYNC ALL statement can be replaced by a SYNC IMAGES (*) statement. However, SYNC ALL may provide better performance. SYNC IMAGES statements are not required to specify the entire image set, or even the same image set, on all images that participate in the synchronization.

## Example

In the following example, image 5 waits for each of the other images to complete using the data. The other images wait for image 5 to set up the data, but do not wait for any other image:

    IF (THIS_IMAGE() == 5) then
       SYNC IMAGES(*)       ! Sets up coarray data for other images
    ELSE
       SYNC IMAGES(5)       ! Other images use the data set up by image 5
    END IF

In the following example, each image synchronizes with its neighbor:

    INTEGER :: THIS_STEP, TOTAL_STEPS, TOTAL_IMAGES, MY_IMAGE
    MY_IMAGE = THIS_IMAGE ()
    TOTAL_IMAGES = NUM_IMAGES ()
       ! Set up calculation
    SYNC ALL

    DO THIS_STEP = 1, TOTAL_STEPS
       IF (MY_IMAGE > 1) SYNC IMAGES (MY_IMAGE - 1)
          ! Do the calculations
       IF (MY_IMAGE < TOTAL_IMAGES) SYNC IMAGES (MY_IMAGE + 1)
    END DO
    SYNC ALL

The calculation starts on image 1 since all the others will be waiting on SYNC IMAGES (TOTAL_IMAGES-1). When this is done, image 2 starts and image 1 performs its second calculation. This continues until they are all executing different steps at the same time. Eventually, image 1 finishes and then the others finish one by one.

Parent topic: S

## See Also

Image Control Statements
Coarrays
Using Coarrays