altera-unroll-loops¶
查找未展开的内部循环,以及具有未知循环界限或大量迭代的完全展开循环。
展开内部循环可以提高 OpenCL 内核的性能。但是,如果它们具有未知循环界限或大量迭代,则无法完全展开,应该部分展开。
注意
此检查无法确定
while
或do..while
循环中的迭代次数;因此,如果此类循环完全展开,则会发出一个建议用户改为部分展开的说明。在
for
循环中,我们的检查仅适用于简单的算术增量(+
、-
、*
、/
)。对于所有其他增量,建议部分展开。根据退出条件,确定迭代次数是否很大的计算可能会有 1 的偏差。这应该不是问题,因为截止值通常是任意的。
基于 Altera SDK for OpenCL: 最佳实践指南。
for (int i = 0; i < 10; i++) { // ok: outer loops should not be unrolled
int j = 0;
do { // warning: this inner do..while loop should be unrolled
j++;
} while (j < 15);
int k = 0;
#pragma unroll
while (k < 20) { // ok: this inner loop is already unrolled
k++;
}
}
int A[1000];
#pragma unroll
// warning: this loop is large and should be partially unrolled
for (int a : A) {
printf("%d", a);
}
#pragma unroll 5
// ok: this loop is large, but is partially unrolled
for (int a : A) {
printf("%d", a);
}
#pragma unroll
// warning: this loop is large and should be partially unrolled
for (int i = 0; i < 1000; ++i) {
printf("%d", i);
}
#pragma unroll 5
// ok: this loop is large, but is partially unrolled
for (int i = 0; i < 1000; ++i) {
printf("%d", i);
}
#pragma unroll
// warning: << operator not supported, recommend partial unrolling
for (int i = 0; i < 1000; i<<1) {
printf("%d", i);
}
std::vector<int> someVector (100, 0);
int i = 0;
#pragma unroll
// note: loop may be large, recommend partial unrolling
while (i < someVector.size()) {
someVector[i]++;
}
#pragma unroll
// note: loop may be large, recommend partial unrolling
while (true) {
printf("In loop");
}
#pragma unroll 5
// ok: loop may be large, but is partially unrolled
while (i < someVector.size()) {
someVector[i]++;
}
选项¶
- MaxLoopIterations¶
定义完全展开循环可以具有的最大迭代次数。默认情况下,它设置为 100。
在实践中,这指的是循环语句条件表达式中上限的整数值。