由于 "invalid argument",Cublas 编程程序命中了 cudaErrorInvalidValue

Cublas programming program hit cudaErrorInvalidValue due to "invalid argument"

最近尝试用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;
}                     

最后发现是我分配的内存大小有问题,导致溢出