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

Addition assignment of matrix product

Tags: None
(comma "," separated)
Henrik
Registered Member
Posts
5
Karma
0

Addition assignment of matrix product

Fri Nov 07, 2014 12:11 pm
I have a vector of dynamic Eigen matrices and a column vector that I want to add to one of them, but with large dimensions the compiler gives me an error about too big stack allocation. I didn't even know that stack allocation was going on. I tried putting noalias() in various places but it did not help. Can I do this in Eigen?
Code: Select all
using matrix = Eigen::Matrix<parameter_scalar, Eigen::Dynamic, Eigen::Dynamic>;
std::vector<matrix> Sigma(N, matrix::Identity(parameter_dimension, parameter_dimension).eval() );

using map_vector = Eigen::Map<Eigen::Matrix<parameter_scalar, parameter_dimension, 1> >;
map_vector z(z_v.data());

Sigma[index] += z * z.transpose();

The error message of the last line is
error: static assertion failed: OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
If parameter_dimension is too large, use Eigen::Dynamic for the template parameter of map_vector to avoid stack allocation:
Code: Select all
using map_vector = Eigen::Map<Eigen::Matrix<parameter_scalar, parameter_dimension>16?Eigen::Dynamic:parameter_dimension, 1> >;
map_vector z(z_v.data(),parameter_dimension);
Henrik
Registered Member
Posts
5
Karma
0
Why is anything being allocated? Can't the addition be done in place? The target is already dynamically allocated, so why is a (temporary?) created?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This is the result of very tricky internal implementation details which is gone in the devel branch. Basically, in your case a non initialized object of type Matrix<scalar,dim,dim> is declared in the body of the product expression. In your case this member is not used at all, so if dim=Dynamic, this really costs nothing, but if dim is a compile time constant, then we request static allocation on the stack. Since the variable is not used, in practice the compiler removes it and again there is zero overhead, but in Eigen we guard against too large stack allocation and produce a compilation error on purpose. You can control this limit with EIGEN_STACK_ALLOCATION_LIMIT (http://eigen.tuxfamily.org/dox/TopicPre ... tives.html). This member is needed when the product expression is nested within a more complicated expression for which a temporary is needed. Again, all this trick is not needed anymore in the devel branch.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I pushed a workaround in the 3.2 branch: https://bitbucket.org/eigen/eigen/commits/879ca94e7363/
Henrik
Registered Member
Posts
5
Karma
0
That sounds great! Thank you!

- Henrik


Bookmarks



Who is online

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