Eigen在使用高维矩阵时栈溢出,报错OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG,解决方法及高维矩阵运算

  本篇内容主要解决Eigen大型矩阵的构建、赋值、运算过程中,内存溢出的问题,报错:OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG!

1.问题陈述

  在使用Eigen中,创建高维矩阵和运算的时候,一直报一个错误,大致内容如下:

In file included from /usr/include/eigen3/Eigen/Core:297:0,
                 from /home/mm/Cpptest/untitled3/main.cpp:3:
/usr/include/eigen3/Eigen/src/Core/DenseStorage.h: In instantiation of ‘void Eigen::internal::check_static_allocation_size() [with T = double; int Size = 1562500]’:
/usr/include/eigen3/Eigen/src/Core/DenseStorage.h:110:41:   required from ‘Eigen::internal::plain_array<T, Size, MatrixOrArrayOptions, 16>::plain_array() [with T = double; int Size = 1562500; int MatrixOrArrayOptions = 0]’
/usr/include/eigen3/Eigen/src/Core/DenseStorage.h:187:38:   required from ‘Eigen::DenseStorage<T, Size, _Rows, _Cols, _Options>::DenseStorage() [with T = double; int Size = 1562500; int _Rows = 1250; int _Cols = 1250; int _Options = 0]’
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:457:55:   required from ‘Eigen::PlainObjectBase<Derived>::PlainObjectBase() [with Derived = Eigen::Matrix<double, 1250, 1250>]’
/usr/include/eigen3/Eigen/src/Core/Matrix.h:259:41:   required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix() [with _Scalar = double; int _Rows = 1250; int _Cols = 1250; int _Options = 0; int _MaxRows = 1250; int _MaxCols = 1250]’
/home/mm/Cpptest/untitled3/main.cpp:11:53:   required from here
/usr/include/eigen3/Eigen/src/Core/util/StaticAssert.h:32:40: error: static assertion failed: OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG
     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
                                        ^
/usr/include/eigen3/Eigen/src/Core/DenseStorage.h:33:3: note: in expansion of macro ‘EIGEN_STATIC_ASSERT’
   EIGEN_STATIC_ASSERT(Size * sizeof(T) <= EIGEN_STACK_ALLOCATION_LIMIT, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG);

这里面最重要的一个信息就是,下面这一个错误提示:

error: static assertion failed: OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG
     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);

  主要原因就是:栈溢出

2.解决办法:

  要想解决这个问题,我们只需要将创建的静态矩阵,改成动态矩阵。
例如,先前我们创建的如下面形式的矩阵:

Eigen::Matrix<double, 500, 500> matrix_NN;

将上一行代码改成下面形式:

Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_NN;

  这里的Eigen::Dynamic表示的是矩阵的维数是动态的,在赋值或者运算的时候,自动产生对应的维数大小。
注意:因为我们定义的矩阵是动态维数,所以有时候可能必须要先“固定(有别于静态矩阵的固定)”维数,那么可以通过赋值的方式,下面介绍几个赋值方式使其产生“固定”维数的方法:
  1).用随机值矩阵初始化

matrix_NN = Eigen::MatrixXd::Random(500, 500);

  2).用单位矩阵初始化

matrix_NN = Eigen::MatrixXd::Identity(500, 500);

  3).用零矩阵初始化

matrix_NN = Eigen::MatrixXd::Zero(500, 500);

  有一点需要强调: 虽然我们可以使用任何维度的矩阵去赋值或者初始化matrix_NN,但是始终没有改变它是动态矩阵的性质,也就是维数运行过程始终可以变化,这一点儿不同于静态矩阵,静态矩阵编译时就确定了维度,运行时不能变。

补充:静态矩阵和动态矩阵的区别:
  动态矩阵和静态矩阵:动态矩阵是指其大小在运行时确定,静态矩阵是指其大小在编译时确定。
  MatrixXd:表示任意大小的元素类型为double的矩阵变量,其大小只有在运行时被赋值之后才能知道。
  Matrix3d:表示元素类型为double大小为3*3的矩阵变量,其大小在编译时就知道。

3.题外话

  矩阵使用了动态矩阵之后,就可以做大型矩阵的运算了,我将一个小例子放在这里与大家分享,我将一个400维的矩阵,进行方程求解,在我的已经用了好多年的美帝良心机(联想)笔记本上,分别使用直接求逆的方法和QR分解的方法,运行时间如下:

Direct guidance uses times: 315363ms
QR uses times: 832.552ms

代码如下,如果你有兴趣可以在你的电脑上试一下:

#include <iostream>
#include <ctime>
#include <Eigen/Core>
#include <Eigen/Dense>

using namespace std;
#define MATRIX_SIZE 400

int main() {
    //This codes mainly use to solve the equation, matrix_NN *x = v_Nd
    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_NN;
    matrix_NN = Eigen::MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    Eigen::Matrix<double, MATRIX_SIZE, 1> v_Nd;
    v_Nd = Eigen::MatrixXd::Random(MATRIX_SIZE, 1);

    clock_t time_stt = clock();
    Eigen::Matrix<double, MATRIX_SIZE, 1> x = matrix_NN.inverse() * v_Nd;
    cout << "Direct guidance uses times: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;

    time_stt = clock();
    x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
    cout << "QR uses times: " << 1000 * (clock() - time_stt) / (double)CLOCKS_PER_SEC << "ms" << endl;

    return 0;
}
  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值