Difference between static and dynamic schedule in OpenMP in C
Asked Answered
O

2

13

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

Ob answered 23/11, 2010 at 21:21 Comment(1)
Could you detail why the second program crashes? Has the pointer, g, been corrupted? And, when it crashed? You could printf the value of i. I need more information to understand this problem. You may print out some variables for each iteration and each thread.Sunderance
W
14

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

Waldner answered 25/1, 2011 at 2:33 Comment(0)
I
16

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.

Illinium answered 3/5, 2011 at 3:52 Comment(0)
W
14

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

Waldner answered 25/1, 2011 at 2:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.