使用Eigen :: VectorXd减少OpenMP

时间:2016-11-08 19:48:42

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

我试图将以下循环与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 ......

1 个答案:

答案 0 :(得分:3)

您的代码未定义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()(大小为零)。使用此对象时,会发生大小不匹配。 A&#34;正确&#34; 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子句中,它将引用要减少的原始变量的存储。

在我们的案例中(参见下面的完整示例),这是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;
}