Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

cuda - cuBLAS argmin -- segfault if outputing to device memory?

In cuBLAS, cublasIsamin() gives the argmin for a single-precision array.

Here's the full function declaration: cublasStatus_t cublasIsamin(cublasHandle_t handle, int n, const float *x, int incx, int *result)

The cuBLAS programmer guide provides this information about the cublasIsamin() parameters: enter image description here

If I use host (CPU) memory for result, then cublasIsamin works properly. Here's an example:

void argmin_experiment_hostOutput(){
    float h_A[4] = {1, 2, 3, 4}; int N = 4; 
    float* d_A = 0;
    CHECK_CUDART(cudaMalloc((void**)&d_A, N * sizeof(d_A[0])));
    CHECK_CUBLAS(cublasSetVector(N, sizeof(h_A[0]), h_A, 1, d_A, 1));
    cublasHandle_t handle; CHECK_CUBLAS(cublasCreate(&handle));

    int result; //host memory
    CHECK_CUBLAS(cublasIsamin(handle, N, d_A, 1, &result));
    printf("argmin = %d, min = %f 
", result, h_A[result]);

    CHECK_CUBLAS(cublasDestroy(handle));
}

However, if I use device (GPU) memory for result, then cublasIsamin segfaults. Here's an example that segfaults:

void argmin_experiment_deviceOutput(){
    float h_A[4] = {1, 2, 3, 4}; int N = 4;
    float* d_A = 0;
    CHECK_CUDART(cudaMalloc((void**)&d_A, N * sizeof(d_A[0])));
    CHECK_CUBLAS(cublasSetVector(N, sizeof(h_A[0]), h_A, 1, d_A, 1));
    cublasHandle_t handle; CHECK_CUBLAS(cublasCreate(&handle));

    int* d_result = 0; 
    CHECK_CUDART(cudaMalloc((void**)&d_result, 1 * sizeof(d_result[0]))); //just enough device memory for 1 result
    CHECK_CUDART(cudaMemset(d_result, 0, 1 * sizeof(d_result[0])));
    CHECK_CUBLAS(cublasIsamin(handle, N, d_A, 1, d_result)); //SEGFAULT!

    CHECK_CUBLAS(cublasDestroy(handle));
}

The Nvidia guide says that `cublasIsamin()` can output to device memory. What am I doing wrong?


Motivation: I want to compute the argmin() of several vectors concurrently in multiple streams. Outputting to host memory requires CPU-GPU synchronization and seems to kill the multi-kernel concurrency. So, I want to output the argmin to device memory instead.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The CUBLAS V2 API does support writing scalar results to device memory. But it doesn't support this by default. As per Section 2.4 "Scalar parameters" of the documentation, you need to use cublasSetPointerMode() to make the API aware that scalar argument pointers will reside in device memory. Note this also makes these level 1 BLAS functions asynchronous, so you must ensure that the GPU has completed the kernel(s) before trying to access the result pointer.

See this answer for a complete working example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...