我正在try 编译以下代码:

#pragma omp parallel shared (j)
{
   #pragma omp for schedule(dynamic)
   for(i = 0; i != j; i++)
   {
      // do something
   }
}

但我得到了以下错误:error: invalid controlling predicate.

OpenMP standard声明对于parallel for构造函数,它"只"允许以下运算符之一:<<=>>=.

我不明白为什么不允许i != j人.对于static schedule,我可以理解,因为编译器需要预先计算分配给每个线程的迭代次数.但我不明白为什么在这种情况下会有这种限制.有线索吗?


即使我赚了for(i = 0; i != 100; i++)分,我也可以写上"&lt;"或"&lt;=".

推荐答案

.

我向OpenMP开发者发送了一封关于这个主题的邮箱,我得到的答案是:

For signed int, the wrap around behavior is undefined. If we allow !=, programmers may get unexpected tripcount. The problem is whether the compiler can generate code to compute a trip count for the loop.

对于一个简单的循环,例如:

for( i = 0; i < n; ++i )

the compiler can determine that there are 'n' iterations, if n>=0, and zero iterations if n < 0.

对于如下循环:

for( i = 0; i != n; ++i ) 

again, a compiler should be able to determine that there are 'n' iterations, if n >= 0; if n < 0, we don't know how many iterations it has.

对于如下循环:

for( i = 0; i < n; i += 2 )

the compiler can generate code to compute the trip count (loop iteration count) as floor((n+1)/2) if n >= 0, and 0 if n < 0.

对于如下循环:

for( i = 0; i != n; i += 2 )

the compiler can't determine whether 'i' will ever hit 'n'. What if 'n' is an odd number?

对于如下循环:

for( i = 0; i < n; i += k )

the compiler can generate code to compute the trip count as floor((n+k-1)/k) if n >= 0, and 0 if n < 0, because the compiler knows that the loop must count up; in this case, if k < 0, it's not a legal OpenMP program.

对于如下循环:

for( i = 0; i != n; i += k )

the compiler doesn't even know if i is counting up or down. It doesn't know if 'i' will ever hit 'n'. It may be an infinite loop.

Credits:OpenMP ARB

C++相关问答推荐

CC crate 示例不会与C函数链接

在函数中使用复合文字来初始化C语言中的变量

为什么此共享库没有预期的依赖项?

如何将另一个数组添加到集合中,特别是字符串?

通过描述符查找文件路径时出现问题

链接器脚本和C程序使用相同的头文件,这可能吗?

为什么我的二叉树删除删除整个左部分的树?

如何在C宏定义中包含双引号?

Fscanf打印除退出C代码为1的程序外的所有内容

为什么会出现此错误?二进制表达式的操作数无效

存储和访问指向 struct 的指针数组

&stdbool.h&q;在嵌入式系统中的使用

try 判断长整数是否为素数

在C中使用字符串时是否不需要内存分配?

共享内存未授予父进程权限

如何使用 VLA 语法使用 const 指针声明函数

添加/删除链表中的第一个元素

malloc:损坏的顶部大小无法找出问题

在带中断的循环缓冲区中使用 易失性

C23 中是否有 __attribute__((nonnull)) 的等效项?