#pragma unroll

Applies to C Applies to C++

Description

The #pragma unroll directive is used to unroll the innermost or outermost loops in your program, which can help improve program performance.

Syntax

>>-#--pragma--+-nounroll------------+--------------------------><
              '-unroll--(--+---+--)-'
                           '-n-'
 
 

where n is the loop unrolling factor. In C programs, the value of n is a positive integral constant expression. In C++ programs, the value of n is a positive scalar integer or compile-time constant initialization expression. An unroll factor of 1 disables unrolling. If n is not specified and if -qhot, -qsmp, or -O4 or higher is specified, the optimizer determines an appropriate unrolling factor for each nested loop.

Notes

The #pragma unroll and #pragma nounroll directives must appear immediately before the loop to be affected.

Only one of these directives can be specified for a given loop. The loop structure must meet the following conditions:

Specifying #pragma nounroll for a loop instructs the compiler to not unroll that loop. Specifying #pragma unroll(1) has the same effect.

To see if the unroll option improves performance of a particular application, you should first compile the program with usual options, then run it with a representative workload. You should then recompile with command line -qunroll option and/or the unroll pragmas enabled, then rerun the program under the same conditions to see if performance improves.

Examples

  1. In the following example, loop control is not modified:
    #pragma unroll(2)
    while (*s != 0)
    {
      *p++ = *s++;
    }
    
    Unrolling this by a factor of 2 gives:
    while (*s)
    {
      *p++ = *s++;
      if (*s == 0) break;
      *p++ = *s++;
    }
    
  2. In this example, loop control is modified:
    #pragma unroll(3)
    for (i=0; i<n; i++) {
      a[i]=b[i] * c[i]; 
    }
    
    Unrolling by 3 gives:
    i=0;
    if (i>n-2) goto remainder;
    for (; i<n-2; i+=3) { 
      a[i]=b[i] * c[i];
      a[i+1]=b[i+1] * c[i+1]; 
      a[i+2]=b[i+2] * c[i+2]; 
    } 
    if (i<n) { 
      remainder: 
      for (; i<n; i++) { 
        a[i]=b[i] * c[i]; 
      } 
    }
    

Related references

IBM Copyright 2003