OMP for 循环条件不是简单的关系比较
我的程序由于错误而无法编译:OpenMP for 循环的条件必须是关系比较('<'、'<='、'>'、'>=' 或循环变量'i'的'!=')
,参考for (size_t i = 2; i * i <= n; i++)
。如何修改和修复它而不影响性能?这是由于 OpenMP 版本旧而导致的问题吗? (因为我记得以前在另一台旧版本的计算机上遇到过不同的问题,现在已经解决了。)
#include <iostream>
#include <cstdio>
int main(int argc, char **argv)
{
if (argc != 2)
return EXIT_FAILURE;
size_t n;
if (sscanf(argv[1], "%zu", &n) == 0)
return EXIT_FAILURE;
auto *prime = new size_t[n + 1];
for (size_t i = 2; i <= n; i++)
prime[i] = i;
#pragma omp parallel for
for (size_t i = 2; i * i <= n; i++)
for (size_t j = i * i; j <= n; j += i)
prime[j] = 0;
size_t N = 0;
for (size_t i = 2; i <= n; i++)
if (prime[i] != 0)
N++;
std::cout << N << '\n';
}
I have this program that isn't compiling due to error: condition of OpenMP for loop must be a relational comparison ('<', '<=', '>', '>=', or '!=') of loop variable 'i'
, referring to for (size_t i = 2; i * i <= n; i++)
. How can I modify and fix it without affecting performance? Is this an issue due to having an old OpenMP version? (Because I remember having a different issue before on another computer with an older version that is resolved now.)
#include <iostream>
#include <cstdio>
int main(int argc, char **argv)
{
if (argc != 2)
return EXIT_FAILURE;
size_t n;
if (sscanf(argv[1], "%zu", &n) == 0)
return EXIT_FAILURE;
auto *prime = new size_t[n + 1];
for (size_t i = 2; i <= n; i++)
prime[i] = i;
#pragma omp parallel for
for (size_t i = 2; i * i <= n; i++)
for (size_t j = i * i; j <= n; j += i)
prime[j] = 0;
size_t N = 0;
for (size_t i = 2; i <= n; i++)
if (prime[i] != 0)
N++;
std::cout << N << '\n';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#pragma omp parallel for
之后的循环必须是 规范形式待确认。在您的情况下,问题在于测试表达式,它应该是以下之一:因此,您必须使用@Yakk建议的内容:计算
n
的sqrt
> 并将其与i
进行比较:Loop after the
#pragma omp parallel for
have to be a canonical form to be confirming. In your case the problem is with the test expression, which should be one of the following:So, you have to use what was suggested by @Yakk: calculate the
sqrt
ofn
and compare it toi
: