C语言 C中OpenMP中静态和动态调度的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4261087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between static and dynamic schedule in OpenMP in C
提问by Tomek Tarczynski
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 pand assigning actualNumberOfChromosomesto it and changing the loop so that iwas equal to p.
问题出在 中actualNumberOfChromosomes,但我想了解原因,而不仅仅是解决这个问题。我可以通过创建加法变量p并分配actualNumberOfChromosomes给它并更改循环来解决这个问题,使其i等于p.
回答by anshu
The difference between staticschedule type and dynamicschedule type is that with staticthe chunks can be pre-computed as well as decided how to scheduled to threads during compilation itself, whereas with dynamicthe same thing is done during run-time.
static调度类型和dynamic调度类型之间的区别在于static,块可以预先计算并决定如何在编译期间调度到线程,而dynamic在运行时完成相同的事情。
With the usage of dynamic, it involves some complex mechanisms like deadlock handling mechanism, load handling, etc.
随着 的使用dynamic,它涉及到一些复杂的机制,如死锁处理机制、负载处理等。
You can get some more info at: http://openmp.blogspot.com.
您可以在以下网址获得更多信息:http: //openmp.blogspot.com。
回答by ejd
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:
问题是此代码不符合 OpenMP 标准,并且不符合标准的程序具有“未指定”的行为。如果您查看 OpenMP API V3.0 规范,第 2.5.1 节 Loop Construct,则在其描述下指出:
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).
静态和动态调度类型之间的最大区别在于,对于静态,块可以在编译期间进行某种程度的计算并调度到线程,而对于动态,它是在运行时完成的(需要更多的锁定)。

