In my VS2010 C code I am successfully using the pragma directive here:
void doSomething(void)
{
n = doSomethingElse();
j = doOnceMore();
k = n + j;
}
#pragma omp parallel for
for (i = 0; i < 5; ++i)
{
doSomething();
}
But I cannot get it to work if I move the work of “doSomething()” inline:
#pragma omp parallel for
for (int i = 0; i < 5; ++i)
{
n = doSomethingElse();
j = doOnceMore();
k = n + j;
}
I always assumed that the pragma directive would take the stuff inside the brackets and assign it a unique thread. Am I dead wrong about that, or is there some other omp syntax I should use?
n,j,k are by default thread shared, hence it does not work. Every thread is writing on n,j,k at the same time, currently.
It depends what you want to do whether they have to be private or shared though. If they are local for one loop pass, you can declare them as thread private and it should work fine (the loop counter, here
i, is by default thread private).Since openmp cannot guess what purpose variables have, it is your job to tell the pragma how to handle them. You find more information about Clauses and Variables here. There is also a very good talk on the openmp webpage about data structures and constructing proper parallel regions.