Re: [eigen] Use Eigen Map in OpenMP reduction

[ Thread Index | Date Index | More Archives ]


I did not realize that you were using a custom reduction. I’ve never been able to make this kind of think works and I believe that it is not implemented correctly in most compilers.

But even if it were, there is no “tmp +=“ in your code, so it is impossible for OpenMP to generate the right code. The compiler has no idea that your 2 inner loops is a “tmp +=“. So you should first rewrite your code this way.

Then hope to have a descent compiler which has implemented custom reduction in OpenMP. Then, even if it is the case, you might get a lot of malloc for intermediate results. I am quite sure you won’t manage to get something interesting with OpenMP at this time. To get a confirmation, I would post to an OpenMP mailing list.

François Fayard

On 10 Jan 2019, at 17:12, Ghislain Durif <> wrote:

#pragma omp declare reduction (+: Eigen::MatrixXd: omp_out=omp_out+omp_in)\
    initializer(omp_priv=MatrixXd::Zero(omp_orig.rows(), omp_orig.cols()))

void myclass::do_something() {
    Eigen::MatrixXd tmp = Eigen::MatrixXd::Zero(m_n, m_p); // temporary matrix
#pragma omp parallel for reduction(+:tmp)
    for(int i=0; i<m_n;i++) {
        for(int l=0; l<m_n; l++) {
            for(int j=0; j<m_p; j++) {
                tmp(l,j) += 10;
    m_mat = tmp;

Mail converted by MHonArc 2.6.19+