OpenMP和C99数据(OpenMP and C99 data)

编程入门 行业动态 更新时间:2024-10-23 17:25:16
OpenMP和C99数据(OpenMP and C99 data)

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.

更多推荐

本文发布于:2023-07-05 06:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   OpenMP   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!