cuBLAS cublasSgemv “Segmentation fault"

若如初见. 提交于 2020-01-03 04:57:12

问题


I have gotten a segmentation fault when running cublasSegmv.My GPU is K20Xm.Here is my code.

float *a, *x, *y;
int NUM_VEC = 8;
y = (float*)malloc(sizeof(float) * rows * NUM_VEC);
a = (float*)malloc(sizeof(float) * rows * cols);
x = (float*)malloc(sizeof(float) * cols * NUM_VEC);
get_mat_random(a, rows, cols);
get_vec_random(x, cols * NUM_VEC);

float *d_a = 0;
float *d_x = 0;
float *d_y = 0;

cudaMalloc((void **)&d_a, rows * cols * sizeof(float);
cudaMalloc((void **)&d_x, cols * NUM_VEC * sizeof(float);
cudaMalloc((void **)&d_y, rows * NUM_VEC * sizeof(float);
cublasSetVector(rows * cols, sizeof(float), a, 1, d_a, 1);
cublasSetVector(NUM_VEC * cols, sizeof(float), x, 1, d_x, 1);
cublasSetVector(NUM_VEC * rows, sizeof(float), y, 1, d_y, 1);
float alpha = 1.0f;
for (int i = 0; i < NUM_VEC; i++) {
  cublasSgemv(handle, CUBLAS_OP_T, cols, rows, &alpha, d_a, rows, d_x + i * cols, 1,0, d_y + i * rows, 1);
}

回答1:


In my limited testing, the error is because the beta parameter of cublasSgemv cannot be NULL. You should allocate memory for the beta variable either on host or device. Following is the code which I used to reproduce and fix the error.

#include <cstdio>
#include <iostream>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cstdlib>

using namespace std;

void get_vec_random(float* a, int count)
{
    for(int i=0; i<count; i++)
        a[i] = rand() / float(RAND_MAX);    
}

void get_mat_random(float* a, int rows, int cols)
{
    get_vec_random(a, rows * cols);
}

int main(int argc, char** argv)
{
    int rows = 10, cols = 10;

    cublasHandle_t handle;
    cublasCreate(&handle);


    float *a, *x, *y;
    int NUM_VEC = 8;
    y = (float*)malloc(sizeof(float) * rows * NUM_VEC);
    a = (float*)malloc(sizeof(float) * rows * cols);
    x = (float*)malloc(sizeof(float) * cols * NUM_VEC);
    get_mat_random(a, rows, cols);
    get_vec_random(x, cols * NUM_VEC);

    float *d_a = 0;
    float *d_x = 0;
    float *d_y = 0;

    cudaMalloc((void **)&d_a, rows * cols * sizeof(float));
    cudaMalloc((void **)&d_x, cols * NUM_VEC * sizeof(float));
    cudaMalloc((void **)&d_y, rows * NUM_VEC * sizeof(float));


    cublasSetVector(rows * cols, sizeof(float), a, 1, d_a, 1);
    cublasSetVector(NUM_VEC * cols, sizeof(float), x, 1, d_x, 1);
    cublasSetVector(NUM_VEC * rows, sizeof(float), y, 1, d_y, 1);

    float alpha = 1.0f, beta = 1.0f;

    cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
    for (int i = 0; i < NUM_VEC; i++) 
    {
        cublasSgemv(handle, 
                    CUBLAS_OP_T, 
                    cols, 
                    rows, 
                    &alpha, 
                    d_a, 
                    rows, 
                    d_x + i * cols, 
                    1, 
                    &beta, 
                    d_y + i * rows, 
                    1);
    }
    return 0;
}

Hope this solves the problem.



来源:https://stackoverflow.com/questions/47752700/cublas-cublassgemv-segmentation-fault

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!