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

Eigen Block operations - copy or reference?

Tags: None
(comma "," separated)
cheuer
Registered Member
Posts
2
Karma
0
Hi all,

I was wondering whether one can definately predict the bahaviour of block operations.
For example:
Code: Select all
MatrixXd Z = MatrixXd::Random(1000,1000);
double a,b=5;

for(int i=0;i<Z.cols();i++){

a = Z.col(i)*b;

}


In terms of speed and memory usage this is equal to:

Code: Select all
typedef Eigen::Map<Eigen::VectorXd> MapVectorXd;
.
.
MatrixXd Z = MatrixXd::Random(1000,1000);
MapVectorXd vec(&Z(0,0),1000);

double a,b=5;

for(int i=0;i<Z.cols();i++){

new (&vec) = MapVectorXd(&Z(0,i),1000);
a = vec*b;

}


In the second example I am explictely using a Map-Object and therefore a pointer to a data-array.
This definately does not result in copying anything.

But in the first example I am only hoping, that the bahaviour is equal to the second example, but I
am never sure about it.
The reason for my question is, that I observed a copy-behaviour when using the specialized block-operation
for vectors, namely: .segment() . When I was using that particular block operation, it resulted in extremely poor performs
compared to constructing a Map<> to the data and using the pointer. Therefore I am assuming that .segment() results
in copying data. But .segment() and .col() and so on are all part of the Block<> family.

In my application I never write to blocks, it is just about getting a pointer to a subset of the data.
Is there a way of safely predicting the behaviour of block-operations?
What happens when using block operations like .col() on sparse-matrices?

Thanks in advance!
User avatar
benoitsteiner
Registered Member
Posts
13
Karma
0
The best way to find out if an Eigen allocates any temporary memory while evaluating an expression is to call Eigen::set_is_malloc_allowed(false) just above the expression you want to check. If Eigen attempts to allocate any memory you should see the following assertion failure:
"heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)"

Note that this mechanism is not available by default. You need to make sure that EIGEN_RUNTIME_NO_MALLOC has been #defined when you compile Eigen to make use of it.


User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Submatrices expressions never trigger a copy of the data. If you observed a slowdown by using .segment(), then please show us the complete expression because this can only comes from other operations, not segment.
cheuer
Registered Member
Posts
2
Karma
0
@benoitsteiner: thank you for the suggestion. That is a good way for debugging some crucial parts!

@ggael: I can't remeber what I did with .segment(), but I did some tests and of course you are right: the copying must have been caused by something else! Thank you


Bookmarks



Who is online

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