OpenMP: are local variables automatically private?(OpenMP:局部变量是否自动私有?)
问题描述
#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:局部变量是否自动私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenMP:局部变量是否自动私有?


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01