CUDA:cublas:dot产品异常

时间:2015-12-07 14:46:19

标签: cuda cublas

我正在使用cublas进行矢量点procdut 我正在使用cublasSdot Cublas手册指出结果变量可以在主机上或设备上。 在主机上它工作得非常好,但是当我使用位于设备的变量时,会引发异常。

这是我的代码:

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cublas_v2.h>
#include <stdio.h>


int main()
{
    cublasHandle_t handle;
    cublasCreate(&handle);

    const int size = 8;
    const float a[size] = { 1, 2, 3, 4, 5,6,7,8 };
    const float b[size] = { 10, 20, 30, 40, 50 ,60,70,80};
    float c = 0;

    // Add vectors in parallel.
    float *dev_a = 0;
    float *dev_b = 0;
    float *dev_c = 0;
    cudaError_t cudaStatus;
    cublasStatus_t  state;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");

    }
    cudaStatus = cudaMalloc((void**)&dev_c,sizeof(float));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");

    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(float));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");

    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(float));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");

    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(float), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");

    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(float), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");

    }

     cudaStatus = cudaMemcpy(dev_c, &c, sizeof(float), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");

    }


    cublasSdot(handle, size, dev_a, 1, dev_b, 1, dev_c);


    cudaMemcpy(c, dev_c, size * sizeof(float), cudaMemcpyDeviceToHost);

    printf("%f\n",c);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }
    system("pause");
    return 0;
}

0 个答案:

没有答案