Search code examples
cudasegmentation-faultthrustcublas

Cublas Thrust Segmentation Fault


I am new to CUDA programming. I was working on a sample code which multiplies a matrix with a vector and prints the results. I am using Cublas Dgemv API for doing the multiplication. On running the program using cuda-memcheck I get the following error,

Error: process didn't terminate successfully
========= The application may have hit an error when dereferencing Unified Memory from the host. Please rerun the application under cuda-gdb or Nsight Eclipse Edition to catch host side errors.
========= Internal error (20)
========= No CUDA-MEMCHECK results found

The minimal complete code is here,

#include <thrust/device_vector.h>
#include <cublas_v2.h>
#include <iostream>
int main(void)
{
int rowDimension = 3; // number of rows
int columnDimension = 6; // number of columns

// initialize data
thrust::device_vector<double> weightMatrix;
weightMatrix.resize(rowDimension * columnDimension);

thrust::device_vector<double> inputVector;
inputVector.resize(columnDimension);

thrust::device_vector<double> F;
F.resize(rowDimension);

for (size_t i = 0; i < rowDimension; i++)
    for (size_t j = 0; j < columnDimension; j++)
        weightMatrix[j * rowDimension + i]=i;

for (size_t j = 0; j < columnDimension; j++)
    inputVector[j] = j;

for (size_t i = 0; i < rowDimension; i++)
    F[i]=0;

cublasHandle_t handle;

/* Initialize CUBLAS */
cublasStatus_t status = cublasCreate(&handle);

if (status != CUBLAS_STATUS_SUCCESS)
    std::cerr << "!!!! CUBLAS initialization error\n";

double alpha = 1.0f;

//  cudaDeviceSynchronize();
status = cublasDgemv(handle, CUBLAS_OP_N, rowDimension, columnDimension, &alpha, thrust::raw_pointer_cast(weightMatrix.data()), rowDimension,
        thrust::raw_pointer_cast(inputVector.data()), 1, 0, thrust::raw_pointer_cast(F.data()), 1) ;;
//  cudaDeviceSynchronize();

if (status != CUBLAS_STATUS_SUCCESS)
    std::cerr << "!!!! kernel execution error.\n";

for (size_t j = 0; j < rowDimension; j++)
    std::cout << F[j] << " ";

status = cublasDestroy(handle);
if (status != CUBLAS_STATUS_SUCCESS)
    std::cerr << "!!!! shutdown error (A)\n";

return 0;
}

The above prgram produces a segmentation fault at the cublasDgemv function. Onrunning cuda-memcheck i get the message reported above. On Googling i couldn't find much help.

Can someone please help me resolve this issue.


Solution

  • Have a look at the documentation of cublasDgemv.

    The signature is:

    cublasDgemv(cublasHandle_t handle, 
                cublasOperation_t trans,
                int m,
                int n,
                const double *alpha,
                const double *A, 
                int lda, 
                const double *x, 
                int incx, 
                const double *beta, 
                double *y, 
                int incy)
    

    beta has to be supplied as a pointer. But you pass a NULL pointer to it instead of a pointer pointing to the value 0.

    So the following will fix your problem:

    double alpha = 1.0;
    double beta = 0;
    
    status = cublasDgemv(handle,
                         CUBLAS_OP_N,
                         rowDimension,
                         columnDimension,
                         &alpha,
                         thrust::raw_pointer_cast(weightMatrix.data()),
                         rowDimension,
                         thrust::raw_pointer_cast(inputVector.data()),
                         1,
                         &beta, // note the change here!
                         thrust::raw_pointer_cast(F.data()),
                         1);