UNROLL

Purpose

The UNROLL directive instructs the compiler to attempt loop unrolling where applicable. Loop unrolling replicates the body of the DO loop to reduce the number of iterations required to complete the loop.

You can control loop unrolling for an entire file using the -qunroll compiler option. Specifying the directive for a particular DO loop always overrides the compiler option.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-+-UNROLL--+---------------------+-+-------------------------><
   |         '-(--unroll_factor--)-' |   
   '-NOUNROLL------------------------'   

unroll_factor
The unroll_factor must be a positive scalar integer constant expression. An unroll_factor of 1 disables loop unrolling. If you do not specify an unroll_factor, loop unrolling is compiler determined.

Rules

You must specify one of the following compiler options to enable loop unrolling:
  • –O3 or higher optimization level
  • -qhot compiler option
  • -qsmp compiler option

The UNROLL directive must immediately precede a DO loop.

You must not specify the UNROLL directive more than once, or combine the directive with NOUNROLL, STREAM_UNROLL, UNROLL_AND_FUSE, or NOUNROLL_AND_FUSE directives for the same DO construct.

You must not specify the UNROLL directive for a DO WHILE loop or an infinite DO loop.

Examples

Example 1: In this example, the UNROLL(2) directive is used to tell the compiler that the body of the loop can be replicated so that the work of two iterations is performed in a single iteration. Instead of performing 1000 iterations, if the compiler unrolls the loop, it will only perform 500 iterations.
!IBM* UNROLL(2)
      DO I = 1, 1000
         A(I) = I
      END DO
If the compiler chooses to unroll the previous loop, the compiler translates the loop so that it is essentially equivalent to the following:
     DO I = 1, 1000, 2
        A(I) = I
        A(I+1) = I + 1
     END DO
Example 2: In the first DO loop, UNROLL(3) is used. If unrolling is performed, the compiler will unroll the loop so that the work of three iterations is done in a single iteration. In the second DO loop, the compiler determines how to unroll the loop for maximum performance.
      PROGRAM GOODUNROLL

      INTEGER I, X(1000)
      REAL A, B, C, TEMP, Y(1000)

!IBM* UNROLL(3)
      DO I = 1, 1000
         X(I) = X(I) + 1
      END DO

!IBM* UNROLL
      DO I = 1, 1000
         A = -I
         B = I + 1
         C = I + 2
         TEMP = SQRT(B*B - 4*A*C)
         Y(I) = (-B + TEMP) / (2*A)
      END DO
      END PROGRAM GOODUNROLL

Related information