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?
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?
A few reasons come to mind:
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
There is very little benefit. Allocation cost will be dwarfed by whatever you do with it at such a size.
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.