【问题标题】:c++ openmp for loop max reduction value during interim of loop executionc ++ openmp for loop在循环执行期间的最大减少值
【发布时间】:2014-10-30 22:56:59
【问题描述】:

在并行 for 循环中计算最大减少量时,在循环执行期间的中间时间,最大减少量变量的值是多少?它是仅针对特定线程的最大值还是所有线程的最大值?

我问的原因是我想使用循环内的当前最大值来执行计算,并且我希望它是所有线程的当前最大值,而不仅仅是正在执行循环的线程。

例如:

#include <stdio.h>
#include <omp.h>

int main(int argc, char *argv[]) {

    double randomarray[10];
    //initialize the random array

    double outputarray[10];
    double currentmax = 0;

    #pragma omp parallel for reduction(max:currentmax)
    for( i=0;i<10; i++) {

        if(randomarray[i] > currentmax)
        {
            currentmax = randomarray[i];   
        }

        output[i]=randomarray[i]/currentmax;
        // is this current max for the currently 
        // executing thread or all threads?
    }

}

【问题讨论】:

  • 你当前的代码太简单了,内存带宽受限。
  • @Zboson 我有一个特定的问题,我正在尝试解决在计算全局最大值之前我需要知道当前最大值的地方;因此,您建议做两遍与我要解决的问题无关。如果您知道不受“内存带宽限制”的更好解决方案,请分享。

标签: c++ multithreading max openmp reduction


【解决方案1】:

减少变量的值在使用reduction 关闭的构造中未定义,并且在线程之间有所不同。每个线程都有变量的私有副本。您将不得不重新考虑并行化。

来自 OpenMP 4 规范:

对于并行和工作共享结构,每个列表的私有副本 项目被创建,每个隐含任务一个,就像私有子句一样 已被使用。 ...然后按指定初始化私有副本 更多。在减少条款所针对的区域的末尾 指定时,原始列表项通过合并其原始列表项来更新 值与每个私有副本的最终值,使用 指定归约标识符的组合器。

【讨论】:

    【解决方案2】:

    它是仅针对特定线程的最大值还是所有线程的最大值?

    这是 OpenMP 并行区域中的“每个线程”私有值。

    下面的代码sn-p可能实现了你想做的,但是感觉没那么有意义。

    #pragma omp parallel for
    for( i=0;i<10; i++) {
      double local_max;
    
      #pragma omp critical
      {
        if(randomarray[i] > currentmax)
        {
          currentmax = randomarray[i];   
        }
        local_max = currentmax;
      }
    
      output[i]=randomarray[i]/local_max;
    }
    

    【讨论】:

      【解决方案3】:

      其他回复清楚地表明,减少 openmp max 不能提供我需要的功能。所以我找到了另一种通过下面定义的原子最大操作来实现它的方法:

      inline double __sync_fetch_and_max_double(double* address, double val) {
        int64_t* address_as_int64 = (int64_t*)address;
        int64_t old = *address_as_int64, assumed;
        do {
          assumed = old;
          double assumed_double = *((double*)&assumed);
          double min = (val > assumed_double) ? val : assumed_double;
          old = __sync_val_compare_and_swap(address_as_int64, assumed,
                                            *((int64_t*)&min));
        } while (assumed != old);
        return *((double *)&old);
      }
      

      然后修改问题中的代码以计算最大值并存储在所有线程之间共享的单个变量中。由于函数是原子函数,因此变量的值始终具有唯一值,即所有线程的当前最大值。

      修改后的代码如下:

      #include <stdio.h>
      #include <omp.h>
      
      int main(int argc, char *argv[]) {
      
          double randomarray[10];
          //initialize the random array
      
          double outputarray[10];
          double currentmax = 0;
      
          #pragma omp parallel for
          for( i=0;i<10; i++) {
      
              __sync_fetch_and_max_double(&currentmax,randomarray[i]);   
      
              output[i]=randomarray[i]/currentmax; //max among all threads
      
          }
      
      }
      

      【讨论】:

      • 有很多不同的方法可以做到这一点。例如。您不必使用原子或关键。您可以进行第一遍以找到每个线程的最大值,然后将这些值传达给每个线程以进行更正,然后再进行第二遍。这就是我对前缀总和所做的。但是你目前正在做的是内存带宽限制(前缀和也是如此)所以它不是很有趣。你最终想要达到什么目标?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 2019-10-07
      • 2011-02-05
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      相关资源
      最近更新 更多