OpenMP嵌套循环索引依赖项

时间:2014-11-02 03:01:55

标签: c++ for-loop parallel-processing openmp

我是这个特殊堆栈交换的新手。我不知道让事情变得漂亮的语法,抱歉。

我正在努力为娱乐项目最大化我的代码。我在C ++中编码,我使用OpenMP进行并行化。问题如下,这是实际代码的过度简化版本,比如我有:

 //Lots of other stuff

 #pragma omp parallel for ordered num_threads(4) schedule(dynamic, 100) private(a,b)
for(int a=0; a<230000; a++)
{
    for(int b=a+1; b<230000; b++)
    {

        //code increments b at times, example
        while(stored[b][0]==stored[a][0])
        {
                    b++;
        }

        //increment counter
        #pragma omp atomic
        ++counter;

        //counting part
        #pragma omp ordered
        if(stored[a][0]!=stored[b][0])
        {
           if(stored[a][1]!=stored[b][1])
           {
                //print counter to file
           }
         }
    }
}

基本上,我想计算代码运行的次数。但是,依赖性&#34; b = a + 1&#34;似乎使代码关闭。它给了我相同的数量,好像它只是&#34; b = 0&#34;,这大约是100亿。

非常感谢任何帮助。

编辑1:我应该提到循环内部的所有内容都与a无关。也就是说,它可以在a的任意方向上运行,也可以在a的任意随机值上运行,但必须在b中按顺序运行。整点是在b测量增量的某些约束下跳过尽可能多的b值,为此算法需要b中的顺序。

1 个答案:

答案 0 :(得分:1)

我对你的代码感到有点困惑。如果你试图使代码并行,你为什么要在pragma中订购?有序将使它连续运行。如果您使用ordered获得了错误的答案,那么代码的其他部分可能是错误的。

我发现的一个问题是你在pragma语句之前声明了a和b。您可能不应该这样做,因此您的代码不会混淆。 a和b应该在for循环的范围内。

以下是我认为您正在寻找的代码。

// do not declare a or b in the code before or use different variables instead of a or b
int counter = 0;
#pragma omp parallel for num_threads(4) schedule(dynamic, 100)
for(int a=0; a<230000; a++)
{
    int tmpCounter = 0;
    for(int b=a+1; b<230000; b++)
    {

        //code increments b at times, example
        while(stored[b][0]==stored[a][0])
        {
                    b++;
        }

        //increment counter code
        ++tmpCounter;

        //counting part
       // idk if you need this code to be ordered?
          if(stored[a][0]!=stored[b][0])
          {
            if(stored[a][1]!=stored[b][1])
            {
                //print counter to file
            }
          }

    }
    #pragma omp critical
    counter += tmpCounter;
}

如果这不是您要找的内容,请在澄清之后评论我的帖子。我会尝试解决它。​​

相关问题