Difference between static and dynamic schedule in OpenMP in C
I've got two similar codes.
First
#pragma omp parallel for shared(g) private(i) schedule(dynamic, 1)
for(i = (*g).actualNumberOfChromosomes; i < (*g).maxNumberOfChromosomes; i++)
{
AddCrossoverChromosome(g, i); // it doesnt change actualNumberOfChromosomes
#pragma omp atomic
(*g).actualNumberOfChromosomes++;
}
Second
#pragma omp parallel for shared(g) private(i) schedule(static, 1)
开发者_如何学Cfor(i = (*g).actualNumberOfChromosomes; i < (*g).maxNumberOfChromosomes; i++)
{
AddCrossoverChromosome(g, i); // it doesnt change actualNumberOfChromosomes
#pragma omp atomic
(*g).actualNumberOfChromosomes++;
}
The only difference is in the first line. First code works fine, but the second one crashes. Why?
Problem is somewhere in actualNumberOfChromosomes
, but I would like to understand why, and not just solve this. I could solve this by creating addition variable p
and assigning actualNumberOfChromosomes
to it and changing the loop so that i
was equal to p
.
The difference between static
schedule type and dynamic
schedule type is that with static
the chunks can be pre-computed as well as decided how to scheduled to threads during compilation itself, whereas with dynamic
the same thing is done during run-time.
With the usage of dynamic
, it involves some complex mechanisms like deadlock handling mechanism, load handling, etc.
You can get some more info at: http://openmp.blogspot.com.
The problem is that this code is not OpenMP compliant and non-compliant programs have "unspecified" behavior. If you look at the OpenMP API V3.0 spec, section 2.5.1 Loop Construct, under the description it states:
The iteration count for each associated loop is computed before entry to the outermost loop. If execution of any associated loop changes any of the values used to compute any of the iteration counts then the behavior is unspecified.
The big difference between a schedule type of static and dynamic, is that with static, the chunks can be somewhat computed and scheduled to threads during compilation, while with dynamic it is done during run-time (requiring more locking).
精彩评论