问题描述
#pragma omp parallel
{
int x; // private to each thread ?
}
#pragma omp parallel for
for (int i = 0; i < 1000; ++i)
{
int x; // private to each thread ?
}
谢谢!
附言如果局部变量是自动私有的,使用 private 子句有什么意义?
P.S. If local variables are automatically private, what is the point of using private clause?
推荐答案
是的,局部变量是自动私有的.
Yes, local variables are automatically private.
存在 private
子句的原因是 您不必更改代码.
The reason for the existence of the private
clause is so that you don't have to change your code.
没有private 子句的唯一并行化以下代码的方法
The only way to parallelize the following code without the private clause
int i,j;
#pragma omp parallel for private(j)
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
//do something
}
}
是更改代码.例如像这样:
is to change the code. For example like this:
int i
#pragma omp parallel for
for(i = 0; i < n; i++) {
int j;
for(j = 0; j < n; j++) {
//do something
}
}
这是完全有效的 C89/C90 代码,但 OpenMP 的目标之一是不必更改您的代码,除非添加可以在编译时启用或禁用的 pragma
语句.
That's perfectly valid C89/C90 code but one of the goals of OpenMP is not have to change your code except to add pragma
statements which can be enabled or disabled at compile time.
这篇关于OpenMP:局部变量是否自动私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!