我正在使用Eigen進行一些仿真。無論何時我甚至包括最小的過載特徵運算(即使我有x=y
其中x
,y
是Eigen::VectorXd
具有相同尺寸),我都會得到分段錯誤錯誤(更準確地說Segmentation fault (core dumped)
,沒有其他細節)。這很奇怪的是,只有在某些函數中有矩陣運算時纔會發生這種情況。特徵庫(3.3.4)的分割錯誤
讓我告訴你:
//main.cu
#include <Eigen/Dense>
#include "def.h"
using namespace std;
int main(int argc, char *argv[])
{
params p;
int ns;
//some code here
MatrixXR A(ns,ns);
VectorXR u(ns);
VectorXR v(ns);
VectorXR unew(ns);
VectorXR aux(ns);
VectorXR vnew(ns);
VectorXR vcouple(ns);
VectorXR q(ns);
Real* output;
output=new Real[output_size];
//output_size is a number depending on the system I am simulating, usually about 1000000.
CPUsim(output,p,u,v,A,unew,vnew,q,aux,vcouple);
delete [] &(output[0]);
return 0;
}
//def.h
#ifndef DEF_H_
#include <Eigen/Dense>
#define DEF_H_
#ifdef DOUBLE
typedef double Real;
typedef Eigen::MatrixXd MatrixXR;
typedef Eigen::VectorXd VectorXR;
#else
typedef float Real;
typedef Eigen::MatrixXf MatrixXR;
typedef Eigen::VectorXf VectorXR;
#endif
struct params
{
//some parameters
};
#endif
//sim.h
#ifndef SIM_H_
#define SIM_H_
#include "def.h"
#include <Eigen/Dense>
void CPUsim(Real* output,params &p, VectorXR& u,VectorXR& v,MatrixXR& A,VectorXR& unew,VectorXR& vnew,VectorXR& q,VectorXR& aux,VectorXR& vcouple);
//other functions
#endif
//sim.cu
#include "sim.h"
#include "coupling.h"
//some functions
void CPUsim(Real* output,params &p, VectorXR& u,VectorXR& v,MatrixXR& A,VectorXR& unew,VectorXR& vnew,VectorXR& q,VectorXR& aux,VectorXR& vcouple)
{
//some code
coupling(u,unew,v,vnew,p,A,vcouple,aux,no);
}
//coupling.h
#ifndef COUPLING_H_
#define COUPLING_H_
#include <Eigen/Dense>
#include "def.h"
//some declarations
void coupling(VectorXR& u,VectorXR& unew,VectorXR& v,VectorXR& vnew,params& p,MatrixXR& A,VectorXR& vcouple,VectorXR& aux,noise& no);
//coupling.cpp
void coupling(VectorXR& u,VectorXR& unew,VectorXR& v,VectorXR& vnew,params& p,MatrixXR& A,VectorXR& vcouple,VectorXR& aux,noise& no)
{
vcouple=A*v;
//some other stuff
}
現在,一些解釋:
如果我有vcouple=vcouple
在coupling
,我沒有錯誤,如果我有vcouple=v
,我得到的錯誤。如果我在main
或CPUsim
中有vcouple=A*v
,我不會收到任何錯誤。有人建議定義'EIGEN_DONT_ALIGN',但只適用於某些情況(即對於相同的ns
,但對於矩陣和向量的元素值不同,它可能會顯示錯誤或可能不會)。你碰巧知道什麼可能導致這個錯誤?
順便說一句,我使用的nvcc編譯器,因爲我使用CUDA模擬的一些部分。但是,Eigen僅用於完全在CPU上運行的部分代碼。對於主機編譯器,我使用GCC 5.4.1,我有Ubuntu 16.04。
編輯: 如果我不把結果存儲錯誤消失(即剛A*v;
代替vcouple=A*v;
)
歡迎SO。請勿使用多餘的語言標籤發送垃圾郵件。你的代碼不是有效的C代碼,而是C++。 – Gerhardh
請閱讀如何提供[mcve]。例如'main'中的'output'永遠不會在任何地方聲明。 – chtz