OpenMP如何处理并行部分内声明的数据? 在C99之前,我将使用private()子句来指定线程局部数据,例如
int i, x; #pragma omp parallel for private(x) for (i=0; i<n; i++) { x=i; }现在C99允许混合数据和代码,我更喜欢在使用它们之前声明变量。 在循环范围内声明数据是否保证它是线程私有的? 例如,以下是否有效?
#pragma omp parallel for for (int i=0; i<n; i++) { int x=i; }我尝试添加private(x)以防万一,但我的编译器对象(可能因为x尚未声明)。
How does OpenMP deal with data declared inside a parallel section? Before C99 I would use the private() clause to specify thread-local data, e.g.
int i, x; #pragma omp parallel for private(x) for (i=0; i<n; i++) { x=i; }Now that C99 allows mixing of data and code, I prefer to declare variables just before using them. Does declaring data within the scope of the loop guarantee it is thread-private? For example, is the following valid?
#pragma omp parallel for for (int i=0; i<n; i++) { int x=i; }I tried adding private(x) just in case, but my compiler objects (probably since x isn't declared yet).
最满意答案
某些变量(包括在并行构造中声明的变量)具有预定的数据共享属性(例如,您不能将它们声明为shared或private )。 这些在OMP3标准的2.9.1.1节中定义。
在这种情况下, OpenMP Standard 3.0,2.9.1.1 :( p78,第12行)“在构造内的范围内声明的具有自动存储持续时间的变量是私有的。” 我很确定在OpenMP中一直都是这样。 所以是的,在你的C99例子中,i和x是私有的; 另一方面,我理解相同的部分,如果x被声明为静态,它将被共享。 我认为在这方面,它或多或少都符合你的期望。
Some variables, including those declared within a parallel construct, have data-sharing attributes that are predetermined (eg, you can't declare them shared or private). Those are defined in section 2.9.1.1 in the OMP3 standard.
In this case, OpenMP Standard 3.0, 2.9.1.1: (p78, line 12) "Variables with automatic storage duration that are declared in a scope inside the construct are private." I'm pretty sure it's always been this way in OpenMP. So yes, in your C99 example, i and x are private; on the other hand, I understand that same section to say that if x was declared static, it'd be shared. I think in this respect, it more or less does what you'd expect.
更多推荐
发布评论