简体   繁体   English

为什么 Eigen 限制堆栈的大小?

[英]Why Eigen limits size on the stack?

I have recently found that Eigen limits the size of static matrices with EIGEN_STACK_ALLOCATION_LIMIT (to 128kB).我最近发现 Eigen 使用 EIGEN_STACK_ALLOCATION_LIMIT 限制静态矩阵的大小(到 128kB)。

What are the reasons for this limit?这个限制的原因是什么?

A few reasons come to mind:想到几个原因:

  1. Eigen is often used in multithreaded code. Eigen 常用于多线程代码中。 Threads other than the main thread usually have a fixed stack size.主线程以外的线程通常具有固定的堆栈大小。 2-8 MiB are common limits. 2-8 MiB 是常见的限制。 In addition to that, some Eigen, BLAS and LAPACK routines use alloca internally.除此之外,一些 Eigen、BLAS 和 LAPACK 例程在内部使用alloca 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.就像std::array一样,恒定时间移动构造/移动分配是不可能的。 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.您可能认为您直接分配给x并且由于复制省略,没有额外的成本。 But in reality, the compiler allocates stack space of a temporary matrix.但实际上,编译器会分配一个临时矩阵的堆栈空间。 Then fill() stores its result in there.然后fill()将其结果存储在那里。 Then that result is copied to x .然后将该结果复制到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.使用动态矩阵,我们只需交换一些指针并完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM