【问题标题】:OpenMP reduction with Eigen::VectorXd使用 Eigen::VectorXd 减少 OpenMP
【发布时间】:2017-03-22 13:43:03
【问题描述】:

我正在尝试通过减少 OpenMP 来并行化以下循环;

#define EIGEN_DONT_PARALLELIZE
#include <iostream>
#include <cmath>
#include <string>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Eigenvalues>
#include <omp.h>

using namespace Eigen;
using namespace std;

VectorXd integrand(double E)
{
    VectorXd answer(500000);
    double f = 5.*E + 32.*E*E*E*E;
    for (int j = 0; j !=50; j++)
        answer[j] =j*f;
    return answer;
}

int main()
{
    omp_set_num_threads(4);
    double start = 0.;
    double end = 1.;
    int n = 100;
    double h = (end - start)/(2.*n);

    VectorXd result(500000);
    result.fill(0.);
    double E = start;
    result = integrand(E);
    #pragma omp parallel
    { 
    #pragma omp for nowait
    for (int j = 1; j <= n; j++){
        E = start + (2*j - 1.)*h;
        result = result + 4.*integrand(E);
        if (j != n){
            E = start + 2*j*h;
            result = result + 2.*integrand(E);
        }
    }
    }
    for (int i=0; i <50 ; ++i)
        cout<< i+1 << " , "<< result[i] << endl;

    return 0;
}

并行肯定比不并行要快,但是对于所有 4 个线程,结果变化很大。当线程数设置为 1 时,输出正确。 如果有人能帮助我解决这个问题,我将不胜感激......

我正在使用带有编译标志的 clang 编译器;

clang++-3.8 energy_integration.cpp -fopenmp=libiomp5

如果这是失败,那么我将不得不学习实现Boost::threadstd::thread...

【问题讨论】:

  • firstprivate(params) reduction(+:result_int) 添加到您的parallel 指令中,删除critical 并重试...
  • @Gilles 感谢您的回复。我已经编辑了我的代码,以便第一个 #pragma 语句读取 #pragma omp parallel firstprivate(params) reduction(+:result_int),第二个 #pragma 语句保持原样,所有后续 @987654332 @ 语句被删除。然后程序会产生运行时错误:....const Eigen::Matrix&lt;double, -1, 1, 0, -1, 1&gt; &gt;]: Assertion aLhs.rows() == aRhs.rows() &amp;&amp; aLhs.cols() == aRhs.cols()' failed. Aborted - 我可以确保 kspace 和 result_int 具有相同数量的元素和维度
  • 你能把你的例子完善成一个完整的minimal reproducible example吗?另外,串行版本是否按预期工作?
  • @AviGinsburg 谢谢,请参见上文,现已编辑。是的,串行版本按预期工作
  • 这不完整。没有integrand(...) 的定义,您的代码也不能编译。如果串行版本返回正确的结果,您也没有回答。

标签: c++ openmp eigen clang++ eigen3


【解决方案1】:

您的代码没有为 OpenMP 定义自定义归约来归约 Eigen 对象。我不确定 clang 是否支持用户定义的缩减(参见OpenMP 4 spec,第 180 页)。如果是这样,您可以声明减少并将reduction(+:result) 添加到#pragma omp for 行。如果没有,你可以自己修改代码,如下:

VectorXd result(500000); // This is the final result, not used by the threads
result.fill(0.);
double E = start;
result = integrand(E);
#pragma omp parallel
{
    // This is a private copy per thread. This resolves race conditions between threads
    VectorXd resultPrivate(500000);
    resultPrivate.fill(0.);
#pragma omp for nowait// reduction(+:result) // Assuming user-defined reductions aren't allowed
    for (int j = 1; j <= n; j++) {
        E = start + (2 * j - 1.)*h;
        resultPrivate = resultPrivate + 4.*integrand(E);
        if (j != n) {
            E = start + 2 * j*h;
            resultPrivate = resultPrivate + 2.*integrand(E);
        }
    }
#pragma omp critical
    {
        // Here we sum the results of each thread one at a time
        result += resultPrivate;
    }
}

您遇到的错误 (in your comment) 似乎是由于大小不匹配造成的。虽然您的代码本身没有一个微不足道的问题,但不要忘记,当 OpenMP 启动每个线程时,它必须为每个线程初始化一个私有 VectorXd。如果没有提供,默认为VectorXd()(大小为零)。使用此对象时,会发生大小不匹配。 omp declare reduction 的“正确”用法将包括初始化程序部分:

#pragma omp declare reduction (+: VectorXd: omp_out=omp_out+omp_in)\
     initializer(omp_priv=VectorXd::Zero(omp_orig.size()))

omp_priv 是私有变量的名称。它由VectorXd::Zero(...) 初始化。使用omp_orig 指定大小。标准 (第 182 页,第 25-27 行)将其定义为:

特殊标识符omp_orig也可以出现在initializer-clause中,它指的是要减少的原始变量的存储。

在我们的例子中(参见下面的完整示例),这是result。所以result.size() 是 500000 并且私有变量被初始化为正确的大小。

#include <iostream>
#include <string>
#include <Eigen/Core>
#include <omp.h>

using namespace Eigen;
using namespace std;

VectorXd integrand(double E)
{
    VectorXd answer(500000);
    double f = 5.*E + 32.*E*E*E*E;
    for (int j = 0; j != 50; j++)   answer[j] = j*f;
    return answer;
}

#pragma omp declare reduction (+: Eigen::VectorXd: omp_out=omp_out+omp_in)\
     initializer(omp_priv=VectorXd::Zero(omp_orig.size()))

int main()
{
    omp_set_num_threads(4);
    double start = 0.;
    double end = 1.;
    int n = 100;
    double h = (end - start) / (2.*n);

    VectorXd result(500000);
    result.fill(0.);
    double E = start;
    result = integrand(E);

#pragma omp parallel for reduction(+:result)
    for (int j = 1; j <= n; j++) {
        E = start + (2 * j - 1.)*h;
        result += (4.*integrand(E)).eval();
        if (j != n) {
            E = start + 2 * j*h;
            result += (2.*integrand(E)).eval();
        }
    }
    for (int i = 0; i < 50; ++i)
        cout << i + 1 << " , " << result[i] << endl;

    return 0;
}

【讨论】:

  • 太好了,谢谢。这让我在运行 4 个线程时速度提高了 2.17 倍。用户定义的缩减对我不起作用,但运行时错误让我想知道它是否与 Eigen 相关,而不是 Clang。编辑刚刚用 g++ 试过这个,它甚至不能编译 ‘result’ has invalid type for ‘reduction’
  • 发生了什么运行时错误?用什么代码?也可能是 Eigen 类型不能很好地与 omp 配合使用,我没有尝试过。
  • 我认为您对 Eigen 和 omp 的看法是正确的。我已经在使用您的用户定义缩减发布的代码上对此进行了测试。即使只设置了一个线程,运行时错误也是一样的,摘录显示为输出太长:[BinaryOp = Eigen::internal::scalar_sum_op&lt;double&gt;, Lhs = const Eigen::Matrix&lt;double, -1, 1, 0, -1, 1&gt;, Rhs = const Eigen::CwiseUnaryOp&lt;Eigen::internal::scalar_multiple_op&lt;doub‌​le&gt;, const Eigen::Matrix&lt;double, -1, 1, 0, -1, 1&gt; &gt;]: Assertion `aLhs.rows() == aRhs.rows() &amp;&amp; aLhs.cols() == aRhs.cols()' failed. Aborted我赶紧补充一下,你概述的替代方法,很管用。
  • 感谢您提供的附录,我将从您链接的手册中进一步阅读 OpenMP。我已经尝试过您使用omp declare reduction 发布的代码,但出现编译错误。 Clang 产生 error: expected an OpenMP directive #pragma omp declare reduction(+: VectorXd: omp_out=omp_out+omp_in)\ 和 g++ 产生 In function ‘int main()’: energy_integral_test.cpp:36:45: error: ‘result’ has invalid type for ‘reduction’ #pragma omp parallel for reduction(+:result) 。不过,您已经找到了适合我的解决方案
  • 你添加了初始化部分吗? '\' 只是为了防止行太长。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-29
  • 2020-09-16
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
相关资源
最近更新 更多