最近,我尝试使用Cublas库编写GPU编程。我要做的只是在设备上分配内存并进行矩阵计算。但是,当我尝试按以下方式分配内存时,会出现类似的错误。
我的代码如下。 C等于21,N等于53940。SNIP等于550482 /30。奇怪的是,我发现无论我在设备上分配内存的顺序如何,由于这条线我总是会出错。 cudaStat = cudaMalloc((void**)&d_WTW, C * C * sizeof(float));
完整的代码如下。
// Set cuda context
cudaError_t cudaStat;
cublasStatus_t stat;
// Initialize device pointer
float* d_data;
float* d_W;
float* d_v;
float* d_result;
float* d_result2;
float* d_temp;
float* d_one;
float* d_mean;
float prod;
float* d_cby1;
float* d_cby2;
float* d_cby3;
float* d_R;
float* d_data_final;
float* d_temp_final;
float* d_WTW;
cudaStat = cudaMalloc((void**)&d_data, SNIPs * N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 1" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_W, N * C * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 2" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_v, N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 4" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_result2, N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 5" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_result, N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 6" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_temp, SNIPs * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 7" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_cby1, C * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 9" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_cby2, C * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 10" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_cby3, N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 11" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_v, N * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 13" << endl;
return EXIT_FAILURE;
}
// stat = cublasSetVector((int)N, sizeof(*vector_ones), vector_ones, 1, d_one, 1);
stat = cublasSetMatrix((int)N, C, sizeof(*W), W, (int)N, d_W, (int)N);
stat = cublasSetMatrix(C, C, sizeof(*WTWInv), WTWInv, C, d_WTW, C);
// allocate memeory for temp result
float* d_R_temp;
cudaStat = cudaMalloc((void**)&d_R, N * B * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 14" << endl;
return EXIT_FAILURE;
}
cudaStat = cudaMalloc((void**)&d_WTW, C * C * sizeof(float));
if (cudaStat != cudaSuccess) {
cout << "device memory allocation failed 12" << endl;
return EXIT_FAILURE;
}
答案 0 :(得分:0)
最后,我发现我的内存分配大小有问题,这会导致溢出。