我有一个使用cudaMallocManaged的c ++类,如下所示:
MyMatrix::MyMatrix(int new_rows, int new_cols, int padrr, int padcc)
{
rows = new_rows;
cols = new_cols;
padr = padrr;
padc = padcc;
cout << "allocating memory" << endl;
float *data;
cudaError_t cudaStatus = cudaMallocManaged(&data, new_rows*new_cols*sizeof(float));
if (cudaStatus != cudaSuccess){
cout << cudaStatus << endl << flush;
exit(1);
}
cudaDeviceSynchronize();
cout << "allocating memory successful:" << cudaStatus << endl;
// I CAN ACCESS DATA HERE
//data[15] = 5.5; //fine
}
MyMatrix::~MyMatrix(void)
{
cudaFree(data); // delete the data array
}
我也有头文件.h
:
class MyMatrix
{
public:
MyMatrix(int new_rows, int new_cols, int padr, int padt);
~MyMatrix(void);
float *data;
int padr;
int padc;
int rows;
int cols;
}
我可以在此构造函数内 中访问数据数组。
但是,一旦我尝试在其外部访问(读取或写入),就会得到terminated by signal SIGSEGV (Address boundary error)
。例如:
MyMatrix *newmat = new MyMatrix(totalr, totalc, padr, padc);
cout << (*newmat).data[0] << endl;
或
MyMatrix newmat = new MyMatrix(totalr, totalc, padr, padc);
cout << newmat.data[0] << endl;
如何“保持”该指针?
答案 0 :(得分:2)
在构造函数中,创建一个名为data
的局部变量:
float *data;
分配cuda内存并将其值分配给局部变量data
之后,该内存地址似乎没有永久存储在任何地方。因此,您的记忆变得无法访问。
您有此行newmat.data
,但是在constructro中,您从未为成员data
分配任何值。您确实只使用了具有相同名称的局部变量。
答案 1 :(得分:1)
您的问题是在
MyMatrix::MyMatrix(int new_rows, int new_cols, int padrr, int padcc)
{
rows = new_rows;
cols = new_cols;
padr = padrr;
padc = padcc;
cout << "allocating memory" << endl;
float *data;
cudaError_t cudaStatus = cudaMallocManaged(&data, new_rows*new_cols*sizeof(float));
if (cudaStatus != cudaSuccess){
cout << cudaStatus << endl << flush;
exit(1);
}
cudaDeviceSynchronize();
cout << "allocating memory successful:" << cudaStatus << endl;
// I CAN ACCESS DATA HERE
//data[15] = 5.5; //fine
}
您在data
中使用的cudaMallocManaged(&data, new_rows*new_cols*sizeof(float))
是您在上一行中声明的float *data;
,而不是您班上的data
成员。您只需要摆脱本地float *data;
,就可以像这样使用data
类成员
MyMatrix::MyMatrix(int new_rows, int new_cols, int padrr, int padcc)
{
rows = new_rows;
cols = new_cols;
padr = padrr;
padc = padcc;
cout << "allocating memory" << endl;
cudaError_t cudaStatus = cudaMallocManaged(&data, new_rows*new_cols*sizeof(float));
if (cudaStatus != cudaSuccess){
cout << cudaStatus << endl << flush;
exit(1);
}
cudaDeviceSynchronize();
cout << "allocating memory successful:" << cudaStatus << endl;
// I CAN ACCESS DATA HERE
//data[15] = 5.5; //fine
}