This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen and OpenMP reductions

Tags: None
(comma "," separated)
linello
Registered Member
Posts
56
Karma
0
OS

Eigen and OpenMP reductions

Mon May 21, 2012 8:01 am
Hi you all, I would like to try OpenMP sum reduction on a Eigen Vector3d.

The code is the following:
Code: Select all
Vector3d someFunction()
{
return Vector3d(1.0,2.0,3.0);
}

Vector3d val = Vector3d::Zero();
for (int i=0 ; i<8; i++)
{
    val+=someFunction();
}


A parallel OpenMP implementation should be
Code: Select all
Vector3d val = Vector3d::Zero();
#pragma omp parallel for  reduction(+:val)
for (int i=0 ; i<8; i++)
{
    val+=someFunction();
}

but I get the following g++ error (g++-4.5)

Code: Select all
error: ‘x’ has invalid type for ‘reduction’


This seems logic to me, but I want to ask you if a OpenMP reduction can in someway be applied to Eigen types and if it makes sense both in terms of speed and code readability.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen and OpenMP reductions

Wed May 23, 2012 11:42 am
Indeed, openmp supports only native types. You could emulate this by using a Matrix<double,3,Dynamic> vals(nb_threads), each thread accumulate into vals.col(threadid), and then you do val = vals.rowsize().sum().

It might be worth it only if you have to accumulate a large number of Vector3d.
As an alternative, you might simply fill a big Matrix<double,3,Dynamic, RowMajor>, and let Eigen does the reduction for you.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell