从文件中读取矩阵到Eigen mpfr

时间:2017-07-13 15:41:12

标签: c++ segmentation-fault eigen mpfr

我已经整理了一个基本的C ++代码,它从文本文件中读取两个矩阵到C ++中,并使用Eigen库来解决广义特征值问题。这非常有效,在使用数据类型doubleMatrixXd时可以获得预期的结果。

对于特定实例,此代码需要以更高的精度运行。通过查看文档,Eigen已经mpfr support我试图实现但是得到了错误:

Segmentation fault (core dumped)

我已经将问题缩小到从文本文件中读取矩阵的代码。在使用双数据类型的旧代码中,没有任何mpfr类型的声明,而是使用标准MatrixXd而不是MatrixXmp。

下面这个最小的工作示例代码只涉及文件的读入,因为这是错误发生的地方。矩阵文件test1.txt只有简单的格式(例如2 x 2矩阵):

1.368237598319937   8.572948739583564
0.582759275301284   8.457285728753445

这是主要的C ++代码

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues> 
#include <stdint.h>
#include <stdio.h>
#include <Eigen/MPRealSupport>
using namespace mpfr;
using namespace std;
using namespace Eigen;

#define MAXBUFSIZE  ((int) 1e6)
typedef Matrix<mpreal,Dynamic,Dynamic>  MatrixXmp;
typedef Matrix<mpreal,Dynamic,1>        VectorXmp;

// The following declares a read function to read in a matrix file
MatrixXmp readMatrix(const char *filename)
    {
    int cols = 0, rows = 0;
    mpreal buff[MAXBUFSIZE];

    // Read numbers from file into buffer.
    ifstream infile;
    infile.open(filename);
    while (! infile.eof())
        {
        string line;
        getline(infile, line);

        int temp_cols = 0;
        stringstream stream(line);
        while(! stream.eof())
            stream >> buff[cols*rows+temp_cols++];

        if (temp_cols == 0)
            continue;

        if (cols == 0)
            cols = temp_cols;

        rows++;
        }

    infile.close();

    rows--;

    MatrixXmp result(rows,cols);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            result(i,j) = buff[ cols*i+j ];

    return result;
    };

int main()
{
  // set precision to 256 bits (double has only 53 bits)
  mpreal::set_default_prec(256);

  MatrixXmp A = readMatrix("test1.txt");
}

首先,我想检查库的所有链接是否都已正确完成,因此注释掉了读取行并从mpfr Eigen页面插入了示例问题,该页面编译并运行没有问题。这会将其缩小到读取功能,但我无法看到问题所在,并且在使用double数据类型时它可以正常工作。我相信我错过了一些明显的东西,任何帮助都会非常感激。

修改

包含堆栈跟踪:

*** Segmentation fault
Register dump:

 RAX: 00007fff15cc73e0   RBX: 0000000000000000   RCX: 0000000000000042
 RDX: 0000000000000100   RSI: 0000000000402bff   RDI: 00007fff15cc73e0
 RBP: 00007fff15cc73c0   R8 : 00007fd24417fac0   R9 : 00007fd244174780
 R10: 0000000000000016   R11: 00007fd2445ffae0   R12: 0000000000401650
 R13: 00007fff15cc74f0   R14: 0000000000000000   R15: 0000000000000000
 RSP: 00007fff02b99fa0

 RIP: 0000000000401756   EFLAGS: 00010206

 CS: 0033   FS: 0000   GS: 0000

 Trap: 0000000e   Error: 00000006   OldMask: 00000000   CR2: 02b99fa8

 FPUCW: 0000037f   FPUSW: 00000000   TAG: 00000000
 RIP: 00000000   RDP: 00000000

 ST(0) 0000 0000000000000000   ST(1) 0000 0000000000000000
 ST(2) 0000 0000000000000000   ST(3) 0000 0000000000000000
 ST(4) 0000 0000000000000000   ST(5) 0000 0000000000000000
 ST(6) 0000 0000000000000000   ST(7) 0000 0000000000000000
 mxcsr: 1f80
 XMM0:  00000000000000000000000000000000 XMM1:      00000000000000000000000000000000
 XMM2:  00000000000000000000000000000000 XMM3:  00000000000000000000000000000000
 XMM4:  00000000000000000000000000000000 XMM5:  00000000000000000000000000000000
 XMM6:  00000000000000000000000000000000 XMM7:  00000000000000000000000000000000
 XMM8:  00000000000000000000000000000000 XMM9:  00000000000000000000000000000000
 XMM10: 00000000000000000000000000000000 XMM11: 00000000000000000000000000000000
 XMM12: 00000000000000000000000000000000 XMM13: 00000000000000000000000000000000
 XMM14: 00000000000000000000000000000000 XMM15: 00000000000000000000000000000000

Backtrace:
/home/main.cpp:30(_Z10readMatrixPKc)[0x401756]
/home/main.cpp:101(main)[0x401c22]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fd243841830]
??:?(_start)[0x401679]

Memory map:

00400000-00404000 r-xp 00000000 08:01 3861463 /home/
00604000-00605000 r--p 00004000 08:01 3861463 /home/
00605000-00606000 rw-p 00005000 08:01 3861463 /home/
01174000-011a6000 rw-p 00000000 00:00 0 [heap]
7fd243298000-7fd2433a0000 r-xp 00000000 08:01 2243272 /lib/x86_64-linux-gnu/libm-2.23.so
7fd2433a0000-7fd24359f000 ---p 00108000 08:01 2243272 /lib/x86_64-linux-gnu/libm-2.23.so
7fd24359f000-7fd2435a0000 r--p 00107000 08:01 2243272 /lib/x86_64-linux-gnu/libm-2.23.so
7fd2435a0000-7fd2435a1000 rw-p 00108000 08:01 2243272 /lib/x86_64-linux-gnu/libm-2.23.so
7fd2435a1000-7fd243620000 r-xp 00000000 08:01 6191906 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0
7fd243620000-7fd24381f000 ---p 0007f000 08:01 6191906 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0
7fd24381f000-7fd243820000 r--p 0007e000 08:01 6191906 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0
7fd243820000-7fd243821000 rw-p 0007f000 08:01 6191906 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.0
7fd243821000-7fd2439e0000 r-xp 00000000 08:01 2243263 /lib/x86_64-linux-gnu/libc-2.23.so
7fd2439e0000-7fd243be0000 ---p 001bf000 08:01 2243263 /lib/x86_64-linux-gnu/libc-2.23.so
7fd243be0000-7fd243be4000 r--p 001bf000 08:01 2243263 /lib/x86_64-linux-gnu/libc-2.23.so
7fd243be4000-7fd243be6000 rw-p 001c3000 08:01 2243263 /lib/x86_64-linux-gnu/libc-2.23.so
7fd243be6000-7fd243bea000 rw-p 00000000 00:00 0
7fd243bea000-7fd243c00000 r-xp 00000000 08:01 2229287 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd243c00000-7fd243dff000 ---p 00016000 08:01 2229287 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd243dff000-7fd243e00000 rw-p 00015000 08:01 2229287 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fd243e00000-7fd243f72000 r-xp 00000000 08:01 6168047 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd243f72000-7fd244172000 ---p 00172000 08:01 6168047 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd244172000-7fd24417c000 r--p 00172000 08:01 6168047 /usr/lib/x86_64-   linux-gnu/libstdc++.so.6.0.21
7fd24417c000-7fd24417e000 rw-p 0017c000 08:01 6168047 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7fd24417e000-7fd244182000 rw-p 00000000 00:00 0
7fd244182000-7fd2441e5000 r-xp 00000000 08:01 6180286 /usr/lib/x86_64-linux-gnu/libmpfr.so.4.1.4
7fd2441e5000-7fd2443e4000 ---p 00063000 08:01 6180286 /usr/lib/x86_64-linux-gnu/libmpfr.so.4.1.4
7fd2443e4000-7fd2443e6000 r--p 00062000 08:01 6180286 /usr/lib/x86_64-linux-gnu/libmpfr.so.4.1.4
7fd2443e6000-7fd2443e7000 rw-p 00064000 08:01 6180286 /usr/lib/x86_64-linux-gnu/libmpfr.so.4.1.4
7fd2443e7000-7fd2443eb000 r-xp 00000000 08:01 2243269 /lib/x86_64-linux-gnu/libSegFault.so
7fd2443eb000-7fd2445ea000 ---p 00004000 08:01 2243269 /lib/x86_64-linux-gnu/libSegFault.so
7fd2445ea000-7fd2445eb000 r--p 00003000 08:01 2243269 /lib/x86_64-linux-gnu/libSegFault.so
7fd2445eb000-7fd2445ec000 rw-p 00004000 08:01 2243269 /lib/x86_64-linux-gnu/libSegFault.so
7fd2445ec000-7fd244612000 r-xp 00000000 08:01 2230157 /lib/x86_64-linux-gnu/ld-2.23.so
7fd2447d0000-7fd2447d6000 rw-p 00000000 00:00 0
7fd24480f000-7fd244811000 rw-p 00000000 00:00 0
7fd244811000-7fd244812000 r--p 00025000 08:01 2230157 /lib/x86_64-linux-gnu/ld-2.23.so
7fd244812000-7fd244813000 rw-p 00026000 08:01 2230157 /lib/x86_64-linux-gnu/ld-2.23.so
7fd244813000-7fd244814000 rw-p 00000000 00:00 0
7fff15ca8000-7fff15cc9000 rw-p 00000000 00:00 0 [stack]
7fff15de8000-7fff15dea000 r--p 00000000 00:00 0 [vvar]
7fff15dea000-7fff15dec000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

1 个答案:

答案 0 :(得分:1)

你可能会分配太多的堆栈空间,正如@StephanLechner所说。即32 MB,因为MAXBUFSIZE是1000000,并且您使用mpreal::set_default_prec使每个条目占用at least 32个字节。这比Mac OS 10.12.5上的默认8 MB限制要多得多。

我运行了原始代码并遇到了一个段错误。我更改了代码以为buff分配堆空间,并且运行正常:而不是

mpreal buff[MAXBUFSIZE];

mpreal *buff = new mpreal[MAXBUFSIZE];

当然,你应该delete