Cublas 推力分段故障

Cublas Thrust Segmentation Fault

我是 CUDA 编程新手。我正在研究一个示例代码,它将矩阵与向量相乘并打印结果。我正在使用 Cublas Dgemv API 进行乘法运算。在 运行 使用 cuda-memcheck 的程序中,我得到以下错误,

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

最小的完整代码在这里,

#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;
}

上述程序在 cublasDgemv 函数中产生了分段错误。在 运行 cuda-memcheck 上,我收到上面报告的消息。在谷歌搜索上我找不到太多帮助。

谁能帮我解决这个问题。

查看 cublasDgemv 的文档。

签名是:

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 必须作为 指针 提供。但是您将 NULL 指针传递给它而不是指向 value 0.

的指针

因此以下内容将解决您的问题:

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);