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

How to access the underlying array of a MatrixBase<Derived>

Tags: None
(comma "," separated)
XapaJIaMnu
Registered Member
Posts
2
Karma
0
I need to access the array that contains the data of a MatrixBase Eigen matrix without making a deep copy. Cross post from StackOverflow.

The Eigen library has the data() method which returns a pointer to an array, however it is only accessible from a Matrix type. The MatrixBase doesn't have a similar method, even though the MatrixBase class is supposed to act as a template and the actual type should be just a Matrix. If I try to access MatrixBase.data() I get a compile time error:

Code: Select all
template <typename ScalarA, typename Index, typename DerivedB, typename DerivedC>
void uscgemv(float alpha,
     const USCMatrix<ScalarA,Index> &a,
     const MatrixBase<DerivedB> &b,
     const MatrixBase<DerivedC> &c_const)
{
    //...some code
    float * bMat = b.data();
    ///more code
}


Error:

Code: Select all
error: ‘const class Eigen::MatrixBase<Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>, Eigen::Matrix<float, -1, 1> > >’ has no member named ‘data’
 float * bMat = b.data();

The underlying matrices that are of type Eigen:MatrixBase are always Eigen::Matrix<float, Dynamic, Dynamic>, so all of the objects whose data() I am trying to access definitely have it as a method.

I am currently solving my problem using this method:
Code: Select all
float * bMat;
int bRows = b.rows();
int bCols = b.cols();
mallocPinnedMemory(&bMat, bRows*bCols*sizeof(float));
Eigen::Map<Matrix<float, Dynamic, Dynamic> > bmat_temp(bMat, bRows, bCols);
bmat_temp = b;  //THis is SLOW, we should avoid it.


However this adds an additional copy, which is the single slowest point in my code.

A solution using Ref<> was proposed, but I think this also adds an additional copy, even though the types should be matching.

I can't use Eigen Magma for this situation and also I'd rather not change the types of the function, as it would require changing A LOT of code.

Any ideas?

EDIT:

Thanks for the explanation. It doesn't solve my problem, but it explains why I can't get a pointer to a data array (and that such array doesn't exist, so I have to look for a different solution).

Last edited by XapaJIaMnu on Tue Aug 05, 2014 11:13 am, edited 1 time in total.
jitseniesen
Registered Member
Posts
204
Karma
2
The error suggests that you when you call uscgemv, the argument b is not a Matrix but an expression of the form scalar * Matrix. Such an expression does not have a .data() member because the scalar product is not yet evaluated.
XapaJIaMnu
Registered Member
Posts
2
Karma
0
jitseniesen wrote:The error suggests that you when you call uscgemv, the argument b is not a Matrix but an expression of the form scalar * Matrix. Such an expression does not have a .data() member because the scalar product is not yet evaluated.



Does this mean that the expression is evaluated the first time you access it? Because I can do b(0,0) and get the result inside?
jitseniesen
Registered Member
Posts
204
Karma
2
XapaJIaMnu wrote:Does this mean that the expression is evaluated the first time you access it? Because I can do b(0,0) and get the result inside?


Correct. If you do b(0,0) then that entry of the matrix is computed (lazy evaluation).


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]