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:局部变量是否自动私有?


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01