问题描述
我有两个类似codeS。
I've got two similar codes.
首先
#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++;
}
二
#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++;
}
唯一的区别是在第一行。首先code正常工作,但第二个崩溃。
问题是为什么?问题是在某处actualNumberOfChromosomes,但我想知道为什么,而不是仅仅解决这个问题。我可以通过创建另外变量p,并把它分配actualNumberOfChromosomes和改变的循环,所以我等于P,在这种情况下解决这个既codeS做工精细。
The only difference is in first line. First code works fine, but the second one crashes.The question is 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 i was equal to p, in such case both codes work fine.
推荐答案
的问题是,这个code未OpenMP的符合和不符合的程序有未指定行为。如果你看一下OpenMP API的V3.0规范,部分2.5.1循环结构,说明其下规定:
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 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).
这篇关于在OpenMP中使用C静态和动态调度的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!