OpenMP外循环私有或共享 [英] OpenMP outer loop private or shared
问题描述
我对OpenMP有疑问.我是否在外部循环中将i
声明为私有或共享有什么不同?
I have a question about OpenMP. Does it make any difference whether I declare i
in the outer loop as private or shared?
int i,j;
#pragma omp parallel for private(j)
for (i=0; i<n; i++) {
for(j=0; j<n; j++) {
//do something
}
}
推荐答案
从技术上讲,这没有什么区别,因为该标准以 canonical循环形式明确声明了循环变量,始终是隐式私有的.
Technically it does not make a difference, because the standard explicitly states the loop variable in the canonical loop form is always implicitly private.
如果以其他方式共享此变量,则在循环结构中将其隐式设置为私有.
If this variable would otherwise be shared, it is implicitly made private in the loop construct.
[在 OpenMP API 4.5中的2.6 ]
我建议不要声明它是共享的,这对尝试阅读该程序的任何人来说都非常混乱.您可能会争辩说,可以在循环外部共享变量,而在内部共享私有变量,但是我想不到这种情况有意义的情况.
I would advise against declaring it shared, it would be very confusing to to anyone trying to read the program. You might argue that you could have the variable being shared outside of the loop and private inside, but I can't think of a case where this would make sense.
我希望编译器在这种情况下会向您发出警告,但至少GCC不会.
I'd hoped the compiler would give you a warning in that case, but at least GCC does not.
这篇关于OpenMP外循环私有或共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!