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

Eigen::Ref and c pointers

Tags: None
(comma "," separated)
kencoken
Registered Member
Posts
3
Karma
0

Eigen::Ref and c pointers

Thu Mar 27, 2014 2:06 pm
Hi,

I'm getting my head around Eigen::Refs and hoped that someone might be able to help me with a small sticking point.

I am attempting to call an external library function which writes directly to a preallocated c-array, and pass the contents of my Eigen::Ref to this:

Code: Select all
void makeMatrix(Eigen::Ref<Eigen::MatrixXf> mat) {
    mat = Eigen::MatrixXf::Zero(10,1);
    libraryFunc(<PTR_TO_MAT_DATA_HERE>);
}

void libraryFunc(float* dataToWrite) { .. }



Now, as Eigen::Ref can possibly contain Eigen::Block, the usual approach of calling my_mat.data() is not possible.

Is there any other way of getting access to the address of the first element in the block?

I'm aware that there is a reason why this is not supported 'out of the box', as clearly the block may have different strides/isn't guaranteed to be contiguous, but would be good to be able to call libraryFunc without making a copy and I know that in my case the data will be contiguous.

Thanks for any help you might be able to give to clear up this confusion!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen::Ref and c pointers  Topic is solved

Thu Mar 27, 2014 3:51 pm
You can still use &mat(0,0) but the data(), outerStride() and innerStride() method should be available. If not, then that's a bug we have to fix.
kencoken
Registered Member
Posts
3
Karma
0

Re: Eigen::Ref and c pointers

Thu Mar 27, 2014 4:26 pm
Aha! That's a very useful trick! Thanks for pointing me in the right direction 8) 8) 8)
kencoken
Registered Member
Posts
3
Karma
0

Re: Eigen::Ref and c pointers

Thu Mar 27, 2014 4:35 pm
Oh, I just realised that you stated that the data() method should also still be available.

Having just checked, indeed it is! It seems that I had some other issue which was causing my code to fail compilation before. That's very useful to know!

And of course, it makes perfect sense that innerStride() and outerStride() can be used to check that I'm accessing the data appropriately as well – I'm understanding things much better now.

Thanks again for your help!


Bookmarks



Who is online

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