C++ OpenMP:局部变量是否自动私有?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6358375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenMP: are local variables automatically private?
提问by pic11
#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 ?
}
Thank you!
谢谢!
P.S. If local variables are automatically private, what is the point of using privateclause?
PS 如果局部变量是自动私有的,那么使用私有子句有什么意义呢?
回答by Z boson
Yes, local variables are automatically 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
}
}
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.
这是完全有效的 C89/C90 代码,但 OpenMP 的目标之一是不必更改代码,除非添加pragma
可以在编译时启用或禁用的语句。
回答by ysrini
The data within a parallel region is private to each thread.
并行区域内的数据对每个线程都是私有的。
Kindly refer http://en.wikipedia.org/wiki/OpenMP#Data_sharing_attribute_clauses[Data sharing attribute clauses]
请参考http://en.wikipedia.org/wiki/OpenMP#Data_sharing_attribute_clauses[数据共享属性条款]