OpenMP - 为什么firstprivate 会导致错误?
为什么我会收到此错误,我应该做什么?
error: firstprivate variable 'j' is private in outer context
void foo() {
int i;
int j = 10;
#pragma omp for firstprivate(j)
for (i = 0; i < 10; i++)
printf("%d\n", j);
}
Why am I getting this error, and what should I do?
error: firstprivate variable 'j' is private in outer context
void foo() {
int i;
int j = 10;
#pragma omp for firstprivate(j)
for (i = 0; i < 10; i++)
printf("%d\n", j);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 pragma,它就可以工作。
请注意,
omp for
和omp parallel for
不是同一件事:后者是omp for
的简写> 在omp 并行
内。It works if you use the pragma
Note that
omp for
andomp parallel for
aren't the same thing: the latter is shorthand for anomp for
inside anomp parallel
.我删除了我的第一个答案,因为我遗漏了一些内容并且它是不正确的。由于 OpenMP V3.0 规范(及之前版本)第 2.9.3.4 节firstprivate 子句,限制项目符号 2 中的限制,该错误是正确的:
问题在于它不知道在执行工作共享区域的线程中使用哪个私有值。如果它是一个新的并行区域,则每个线程将创建一个新区域,并且firstprivate 是从创建该区域的线程的私有副本中复制的。
I deleted my first answer because I missed something and it was incorrect. The error is correct because of a restriction in the OpenMP V3.0 spec (and previous versions), section 2.9.3.4 firstprivate clause, Restrictions bullet 2:
The problem is that it doesn't know which private value to use among the threads that are to execute the worksharing region. If it is a new parallel region, then each thread will create a new region and the firstprivate is copied from the private copy of the thread creating the region.