Hope this is the right place to post questions from newbies.
I was wondering if there is an elegant way to initialize private variables when parallelizing a loop.
To illustrate the question with a simple example :
This loop which fills the value 10, 9, 8, etc. in an array of 10 elements, and I want to use the -- operator in the loop to do so.
- Code: Select all
int a[10];
int i;
int val=10;
for (i=0; i<10; ++i) {
a[i] = val;
val--;
}
I was thinking of using openmp this way :
- Code: Select all
#pragma omp parallel for firstprivate(val) private(i) schedule(static)
for (i=0; i<10; ++i) {
a[i] = val;
val--;
}
but of course val is initialized to 10 for each thread and this not going to work.
I thought of initialising val in the for statement
- Code: Select all
for(i=0,val=10-i; ... )
I know we could rewrite the code easily to make it work (a[i]=val-i) , but this is a general question on private variables initialization.
Is there a simple / recommanded way to initialize variables used by each thread ?
Thanks for any advice
