0

I have recently found that Eigen limits the size of static matrices with EIGEN_STACK_ALLOCATION_LIMIT (to 128kB).

What are the reasons for this limit?

jjcasmar
  • 1,387
  • 1
  • 18
  • 30
  • related: https://stackoverflow.com/q/13636684/4117728. The stack is limited, thats not eigens fault, though 128kB is rather little – 463035818_is_not_an_ai Jul 19 '22 at 09:41
  • You can pre-define that macro before including Eigen (ideally using a command-line argument) if you like to have larger matrices on the stack. Be aware that this is the limit per matrix, i.e., if you have lots of large matrices simultaneously, you could still exceed your stack capacity. – chtz Jul 19 '22 at 10:48

1 Answers1

2

A few reasons come to mind:

  1. Eigen is often used in multithreaded code. Threads other than the main thread usually have a fixed stack size. 2-8 MiB are common limits. In addition to that, some Eigen, BLAS and LAPACK routines use alloca internally. All of that combined doesn't leave much room until the application crashes with such large matrices

  2. There is very little benefit. Allocation cost will be dwarfed by whatever you do with it at such a size.

  3. There are potential hidden costs. Just like std::array, constant time move construction / move assignment are not possible. Consider this simple example:

using Matrix = Eigen::Matrix<double, 128, 128>;
Matrix fill();

void foo()
{
    Matrix x;
    x = fill();
}

You might think that you assign directly to x and thanks to copy-elision, there is no extra cost. But in reality, the compiler allocates stack space of a temporary matrix. Then fill() stores its result in there. Then that result is copied to x. Copy-elision cannot work in such a case because the return value needs to be alias-free.

With a dynamic matrix, we would simply swap some pointers and be done with it.

Homer512
  • 9,144
  • 2
  • 8
  • 25
  • The use case for this was trying to compute some derivatives using an autodiff library. Since the Scalar type must carry all the derivative for the autodiff to work, a simple 6x6 matrix was hitting the limit due to the big size of the Scalar type. – jjcasmar Jul 19 '22 at 13:17
  • @jjcasmar Doesn't really change the fact that you are handling an amount of data where stack allocation is not suitable. – Homer512 Jul 19 '22 at 13:27