The #pragma unroll directive is used to unroll the innermost or outermost for loops in your program, which can help improve program performance.
>>-#--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.
The #pragma unroll and #pragma nounroll directives can only be used on for loops or a block_loop directive. It cannot be applied to do while and while loops.
The #pragma unroll and #pragma nounroll directives must appear immediately before the loop or the block_loop directive 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.
#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 information