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

Temporary allocation on Matrix-Matrix dense product

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

Why does the following code allocate temporary memory in Eigen?

Code: Select all
/* some initialization of matrices m1 and m2 */
Eigen::MatrixXcd m1(4,4);
Eigen::MatrixXcd m2(4,4);
for (int r=0; r<4; ++r) { for (int c=0; c<4; ++c) { m1(r,c) = (double)r*c; m2(r,c) = (double)r+c; } }

Eigen::MatrixXcd U(4,4);

/* matrix-matrix product */
Eigen::internal::set_is_malloc_allowed(false);
U = m1 * m2; // this causes an allocation assert !!!


But this works without temporary allocation:
Code: Select all
(m1 * m2).evalTo(U);


Does this mean that I should avoid assignments at all cost when writing code?

Thanks!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
All the details are here: http://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html

Basically, dense matrix (and vector) products are the only two exceptions where we assume aliasing by default. Solution:

U.noalias() = m1 * m2;


Bookmarks



Who is online

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