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

Getting data pointer from mapped external array

Tags: None
(comma "," separated)
deng
Registered Member
Posts
10
Karma
0
Hey guys,

I'm currently evaluating Eigen and am very impressed. However, there's one thing that is currently more or less a showstopper for me.

I'm having an external matrix in *ptr with dimensions in m,n. I'm mapping this matrix by using

Code: Select all
Map<MatrixXd> mat(ptr,m,n);


I know I can pass this matrix to a function without any temporary by using

Code: Select all
template<typename T>
void myfunc(const MatrixBase<T> &A)
{
cout << A << endl;
}


However, in this function 'myfunc', I'd like to call an external C routine which expects a raw pointer to the data, i.e., the original 'ptr'. If I simply use 'A.data()', the compiler rightfully tells me that this member function does not exist, since 'A' is now an expression and not a matrix. Now my question is: how do I get the raw data, without any copying involved? I'm hoping there's some kind of const_cast I can use, but I'm not sure. Actually, it'd be even nicer if I could wrap an external array without the need to create an expression. :-)

Cheers,
David
deng
Registered Member
Posts
10
Karma
0
Well, I can just

Code: Select all
&A(0)


of course. What was I thinking? Sorry for the noise.

Still, I'm wondering why mapping an external array really requires an expression. I would be so nice if I could just call a special constructor for MatrixXd like

Code: Select all
MatrixXd mat(ptr,m,n)


without using Map at all. This is what e.g. the Armadillo library provides, but Eigen was faster in my first tests, so I guess there must be a good reason. ;-)
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
.derived().data() will do the job, though we should probably add it in "DenseCoeffsBase<Derived, DirectAccessors>" such that is readily available.

Regarding your second question:
- what would happen if someone want to resize such a matrix that maps external memory?
- our Matrix<> object can deal with data allocated on the stack. Such object does not have any pointer attribute, and so we would still need the Map expression for such small fixed size objects.
- Matrix<> always guarantees the elements are sequentially stored while Map can deal with strides.
deng
Registered Member
Posts
10
Karma
0
Thank you very much ggael for your reply.

.derived().data() was exactly what I was looking for!

Regarding your remarks:

I think such a matrix wrapping external memory should simply be const. I think it's a pretty usual case that you get a raw matrix from some external environment (say, MATLAB) and it's meant to be read-only anyway.

As for stack memory and strided access: I'm not saying that Map is useless, quite the opposite, it's a very powerful tool. I just think that it'd be nice if I could use the normal matrix constructor to wrap external memory in a read-only way, so that I still deal with matrices and not immediately with matrix expressions.
Hauke
Registered Member
Posts
109
Karma
3
OS
deng wrote:I just think that it'd be nice if I could use the normal matrix constructor to wrap external memory in a read-only way, so that I still deal with matrices and not immediately with matrix expressions.


The problem is that we would be required to add special flags to the Matrix class which would be needed to decide whether or not to delete the hosted memory. There are many more side-effects as e.g. implementing proper const correctness, matrix-matrix assignment, matrix swapping, etc.

@Gael, I like your proposal of moving .data() to "DenseCoeffsBase<Derived, DirectAccessors>". You probably saw what I discussed with Jitse yesterday.

- Hauke
deng
Registered Member
Posts
10
Karma
0
I see. That surely sounds like a lot of added complexity, indeed. Well, it's not that big of an issue, so I will just learn to live with it. :) Thanks for your explanations.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]