Intel® Fortran Compiler

Developer Guide and Reference

ID 767251
Date 3/31/2025
Public
Document Table of Contents

INTERCHANGE Directive for OpenMP

OpenMP* Fortran Compiler Directive: Causes a nest of loops to be reordered.

Syntax

!$OMP INTERCHANGE [ clause ]

loop-nest

[!$OMP END INTERCHANGE]

clause

Is PERMUTATION (loop-list). It may appear no more than once on the directive.

loop-list is a list of n compile time constant integer expressions (1, 2, …, n), where n is at least 2 and less than or equal to the number of loops in the loop-nest.

loop-nest

Is a perfectly nested set of 2 or more DO loops. The loops need not have unit stride. All loops in the nest must be rectangular, meaning the loop bounds of an inner loop cannot depend on a loop control variable of an outer loop.

Description

INTERCHANGE is a pure directive, so it can appear in a Fortran PURE procedure.

Interchanging loops can have an impact on performance. Rearranging loops so that memory access patterns have unit stride give better performance in vectorized code. In some cases it is desirable to have the longest iteration count on the innermost loop, and the shortest on the outermost loop.

If no PERMUTATION clause is specified, the behavior is as if PERMUTATION (2, 1) appeared.

The INTERCHANGE construct is associated with the n outer most loops, where n is the number of list items in loop-list. If the loop-list contains the sequence p1, p2, …pn, the associated loops are l1, l2, …ln with being the outermost loop, then the effect of the INTERCHANGE construct is to reorder the loops in the order lP1, lp2, …lpn .

Example

The following shows the transformation when the outer three loops in a nest of four loops are reordered in an INTERCHANGE construct. The loops all have unit stride. The PARALLEL DO construct is applied to the transformed loop nest.

  INTEGER :: arr (16, 20, 24, 10)
  INTEGER :: i, j, k, l
  INTEGER :: i_max, j_max, k_max, l_max
  INTEGER :: i_min, j_min, k_min, l_min
  
  . . . 
  !$OMP PARALLEL DO
  !$OMPX INTERCHANGE PERMUTATION (2, 3, 1) 
  DO i = i_min, i_max
    DO j = j_min, j_max
      DO k = k_min, k_max
        DO l = l_max, l_min
          arr(i,j,k,l) = arr(i,j,k,l)*10
        END DO
      END DO
    END DO
  END DO 
  . . . 
 

The resulting transformed code is:

  INTEGER :: arr (16, 20, 24, 10)
  INTEGER :: i, j, k, l
  INTEGER :: i_max, j_max, k_max, l_max
  INTEGER :: i_min, j_min, k_min, l_min
  . . . 
  !$OMP PARALLEL DO
  DO j = j_min, j_max
    DO k = k_min, k_max
      DO i = i_min, i_max
       DO l = l_max, l_min
          arr(i,j,k,l) = arr(i,j,k,l)*10
       END DO
      END DO
    END DO
  END DO 
  . . .