当特征矩阵类型的矩阵大小超过一定限制时,c ++错误分配 [英] c++ bad allocation when matrix size exceeds a certain limit with Eigen matrix type

查看:221
本文介绍了当特征矩阵类型的矩阵大小超过一定限制时,c ++错误分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++动态库中,我使用本征库.这个Dll在解决问题配置的python软件中称为.在小问题中,代码可以正常工作并返回正确的解决方案.如果点数增加,则库将抛出std::bad_alloc.

In a c++ dynamic library I solve a least square problem using the Eigen Library. This Dll is called inside a python software where the problem configuration is settled. In a small sized problem the code works properly and returns the correct solution. If the number of points increases then the library throws std::bad_alloc.

更准确地说,将错误简化为最大程度的代码是

More precisely, The code which creates the error simplified to its most is

try {
    matrixA = new Eigen::MatrixXd(sizeX,NvalidBtuple); // initialize A
    for (int i=0;i<sizeX;++i) {
        int secondIndex = 0;
        for (int k=0;k<btermSize;++k) {
            if (bterm[k] == 1) {                        // select btuple that are validated by density exclusion
                // product of terms
                (*matrixA)(i,secondIndex) = 1.0;
                secondIndex += 1;
            }
        }
    }
} catch (std::bad_alloc& e) {
    errorString = "Error 3: bad allocation in computation of coefficients!";
    std::cout<<errorString<<" "<<e.what()<<std::endl;
    return;
} catch (...) {
    errorString = "Error 4: construction of matrix A failed! Unknown error.";
    std::cout<<errorString<<std::endl;
    return;
}

其中matrixA在头文件中用Eigen::MatrixXd *matrixA;定义.

where matrixA is defined in the header file with Eigen::MatrixXd *matrixA;.

如果sizeXNvalidBtuple小于约20'000x3'000,则矩阵定义有效.如果尺寸较大,则会崩溃.

if sizeX and NvalidBtuple are smaller than about 20'000x3'000, the matrix definition works. If the size is bigger, it crashes.

我进行测试的计算机上有足够的可用内存,大约有15G的可用内存.

The computer on which I did the tests has enough memory available, about 15G of free memory.

这是堆问题吗? 如何使图书馆接受更大的矩阵?

Is this a heap/stack problem? How can I make the library accept bigger matrices?

任何评论都欢迎.谢谢.

Any comment is welcom. Thanks.

修改: 如下面的回答所述,我对NvalidBtuple的定义不清楚:

As remarked in an answer below, I was not clear on the NvalidBtuple defintion:

NvalidBtuple = 0;
for (int i=0;i<btermSize;++i) {NvalidBtuple += bterm[i];}

其中bterm是布尔向量.因此,由于在循环中我们执行了if (bterm[k] == 1)检查,因此secondIndex始终小于NvalidBtuple.

where bterm is a boolean vector. Thus, since in the loop we do the check if (bterm[k] == 1), the secondIndex is always smaller than NvalidBtuple.

推荐答案

从您的问题的详细信息来看,矩阵占用480Mb的RAM. 32位应用程序只能访问2Gb RAM(请参阅例如

From the details of your question, the matrix takes 480Mb of RAM. A 32bit application can only access 2Gb of RAM (see e.g. How much memory can a 32 bit process access on a 64 bit operating system?); the allocation fails because there is no free continuous 480Mb block in the address space of the application.

解决问题的最佳方法是将应用程序重新编译为64位.您将无法在32位系统中运行它,但是这应该不会成为问题,因为由于内存有限,您无论如何都无法在这样的系统上运行算法.

The best way to solve the problem is to recompile the application as 64 bit. You won't be able to run it in a 32 bit system, but this shouldn't be a problem since you aren't able to run your algorithm on such a system anyways due to the limited memory.

这篇关于当特征矩阵类型的矩阵大小超过一定限制时,c ++错误分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆