Error using large Eigen matrix: " OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG"

I have been using Eigen matrices to test a new code I wrote, and I just ran into this issue for the first time. I just started reading about "Fixed vs. Dynamic size" in Eigen matrices and I thought I was using "dynamic" matrices for large sizes, but when I try using larger number of grids I get the error:
 
 static assertion failed: OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG


Example code:
1
2
3
4
5
6
7
8
9
10
11
12
static const int nx = 128;
static const int ny = 128; 

using namespace std;
using namespace Eigen;

int main(){
Eigen::Matrix<double, (ny+1), nx> X; //ERROR HERE
X.setZero();
//other similar initializations 

}

This code is running fine for smaller sizes of nx; ny; but not the case I am showing. Ideally, I would like to run something as large as nx=1024; and ny=1024;
Is this not possible using Eigen matrices? Thanks.
Last edited on
I don't have Eigen installed but from the docs it seems like you can do:
 
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> X(ny + 1, nx);
or
 
Eigen::MatrixXd X(ny + 1, nx);

https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
Last edited on
Yes, it is possible. Objects that are too big for the stack needs to be allocated on the heap:
1
2
3
4
5
6
7
8
9
10
11
12
static const int nx = 128;
static const int ny = 128; 

using namespace std;
using namespace Eigen;

int main(){
Eigen::Matrix<double, (ny+1), nx>* X = new Eigen::Matrix<double, (ny+1), nx>();
X->setZero();
//other similar initializations 

}
or better yet using smart pointer:
1
2
3
4
5
6
7
8
9
10
11
12
static const int nx = 128;
static const int ny = 128; 

using namespace std;
using namespace Eigen;

int main(){
 std::unique_ptr<Eigen::Matrix<double, (ny+1), nx>> X{new Eigen::Matrix<double, (ny+1), nx>()};
X->setZero();
//other similar initializations 

}
Try making X static. static variables are not allocated on the stack.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

static const int nx = 128;
static const int ny = 128; 

int main()
{
   Matrix<double,Dynamic,Dynamic> X(ny+1,nx);       // <==== Use Dynamic
   MatrixXd Y(ny+1,nx);                             // <==== This works as well (X: Dynamic, d: double)
   X.setZero();
   Y.setZero();
}


I think I would just use MatrixXd, for simplicity. It's a predefined typedef for the longer version.

http://eigen.tuxfamily.org/dox/group__QuickRefPage.html


Actually, Eigen was easier to install than I realised - just unzip the templated headers into a directory, then adjust the include path. No Windows settings to fiddle with, or code to build. Works a treat from the command line.


Can't help feeling all this matrix stuff is easier in Fortran, though:
program main
   use iso_fortran_env
   implicit none
   integer, parameter :: nx = 128, ny = 128
   real(kind=real64), allocatable :: X(:,:)

   allocate( X(ny+1,nx) )
   X = 0
end program


Last edited on
Thanks everyone!! Using Matrix<double,Dynamic,Dynamic> X(ny+1,nx);
or Eigen::MatrixXd X((ny+1),nx); fixed my problem!
Last edited on
Topic archived. No new replies allowed.