#pragma unroll, #pragma nounroll
Purpose
Controls loop unrolling, for improved performance.
Syntax
>>-#--pragma--+-nounroll------------+-------------------------->< '-unroll--+---------+-' '-(--n--)-'
Parameters
- n
- Instructs the compiler to unroll loops by a factor of n. In other words, the body of a loop is replicated to create n copies (including the original) and the number of iterations is reduced by a factor of 1/n. The value of n must be a positive integer.
- Specifying #pragma unroll(1) disables loop unrolling, and is equivalent to specifying #pragma nounroll.
Usage
Only one pragma can be specified on a loop.
The pragma affects only the loop that follows it. An inner nested loop requires a #pragma unroll directive to precede it if the wanted loop unrolling strategy is different from that of the -funroll-loops (-qunroll) option.
The #pragma unroll and #pragma nounroll directives can only be used on for loops. They cannot be applied to do while and while loops.
The loop structure
must meet the following conditions:
- There must be only one loop counter variable, one increment point for that variable, and one termination variable. These cannot be altered at any point in the loop nest.
- Loops cannot have multiple entry and exit points. The loop termination must be the only means to exit the loop.
- Dependencies in the loop must not be "backwards-looking". For example, a statement such as A[i][j] = A[i -1][j + 1] + 4 must not appear within the loop.
Examples
In
the following example, the #pragma unroll(3) directive on the
first for loop requires the compiler to replicate
the body of the loop three times. The #pragma unroll on the
second for loop allows the compiler to decide whether
to perform unrolling.
#pragma unroll(3)
for( i=0;i < n; i++)
{
a[i] = b[i] * c[i];
}
#pragma unroll
for( j=0;j < n; j++)
{
a[j] = b[j] * c[j];
}
In this example, the first #pragma unroll(3) directive
results in: 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];
}
}