我整理了一个基本的C++代码,从文本文件中读取两个矩阵到C++中,并使用特征库解决广义特征值问题。这工作得很完美,在使用数据类型
对于一个特定的实例,这段代码将需要以更高的精度运行。通过查看文档,Eigen具有mpfr支持,我一直试图实现它,但得到了错误:
Segmentation fault (core dumped)
我已经把问题缩小到从文本文件中读取矩阵的代码。在使用double数据类型的旧代码中,没有任何mpfr类型的声明,我使用标准的matrixxd代替MatrixXmp。
下面这个最小的工作示例代码只涉及文件的读入,因为这是发生错误的地方。矩阵文件
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页面插入示例问题,该问题编译和运行都没有问题。这将它缩小到read函数,但我看不出问题出在哪里,当使用
编辑
包括的堆栈跟踪:
*** 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]
正如@stephanlechner所说,您可能分配了太多的堆栈空间。即32 MB,因为
我运行了你的原始代码,发现了一个seg错误。我更改了您的代码,为
mpreal buff[MAXBUFSIZE];
写
mpreal *buff = new mpreal[MAXBUFSIZE];
当然,您以后应该