开发者

OpenMP and C99 data

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开发者_StackOverflow 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).


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.


C always allowed for the declaration of x inside the for block. so there is no difference at all, and you should have done that before.

The difference is in fact in the declaration of i. This in C89 you would have to have declare before the for loop. OMP then "knows" by itself that the loop variable must be local for each thread.

int i;
#pragma omp parallel for
for (i=0; i<n; i++) {
   int x=i;
}

Here C99 really eases your life and clarifies your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜