Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP and C99 data

Tags:

c

c99

openmp

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).

like image 571
Brian Hawkins Avatar asked Dec 17 '25 16:12

Brian Hawkins


1 Answers

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.

like image 138
Jonathan Dursi Avatar answered Dec 19 '25 05:12

Jonathan Dursi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!