资源经验分享Eigen在使用高维矩阵时栈溢出,报错OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG,解决方法及高维矩阵运算

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

2019-11-05 | |  98 |   0

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

原文来自:CSDN      原文链接:https://blog.csdn.net/u011341856/article/details/102882389


本篇内容主要解决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);

&emsp;&emsp;主要原因就是:栈溢出

2.解决办法:

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

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

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

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

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

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

&emsp;&emsp;2).用单位矩阵初始化

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

&emsp;&emsp;3).用零矩阵初始化

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

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

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

3.题外话

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

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

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

#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;
}

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:(Python)从零开始,简单快速学机器仿人视觉Opencv---第十六节:图像金字塔

下一篇:基于opencv的BackgroundSubtractorMOG2和BackgroundSubtractorKNN通过背景减除来实现目标追踪

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...