cudaGetLastError返回“未知错误”

时间:2012-04-22 20:16:49

标签: c++ visual-studio-2008 cuda

我是CUDA C的新手。我正在编写一个简单的数组Add and Reduce,当它运行错误检查从设备复制回主机时,我得到一个“未知错误”。我不确定错误检查器是否有问题并且没有返回正确的cudaError但是我无法解决什么是错误的.......

using namespace std;


#include <iostream>

void CudaAddReduce(int *input, int *output, size_t size);

__global__ void Fill(int *fillItem);

__global__ void Add(int *input1, int *result);

__global__ void Reduce(int *intputArray, int *outputArray);



main(int argc, char *argv[])
{
const int N = 100;
int inp[N];
int outp[N];
size_t size = (N * sizeof(int));

CudaAddReduce(inp,outp,size);

cout << outp[N] << endl;

}

void CudaAddReduce(int *input, int *output, size_t size)
{

// allocate buffers to device 

//input
int *d_input;
if (cudaMalloc(&d_input,size) != cudaSuccess){
    cerr << cudaGetErrorString(cudaGetLastError()) << endl;
    cout << "Input allocation to device" << endl;
    exit(1);
}
////////////////////////////
//output
int *d_output;
if (cudaMalloc(&d_output,size) != cudaSuccess){
    cerr << cudaGetErrorString(cudaGetLastError()) <<endl;
    cout << "output allocation to device" << endl;
    exit(1);
}

//////////////////////////////////
//copy buffers to device from host
//////////////////////////////////
//input
if (cudaMemcpy(d_input, input, size, cudaMemcpyHostToDevice) != cudaSuccess){
    cerr << cudaGetErrorString(cudaGetLastError()) << endl;
    cout << "Input Copy from host to device" << endl;
    exit(1);
}

/////////////////////////////////
//execute device kernals
/////////////////////////////////
int numThreads = 256;
int numBlocks = 1;

//Fill Kernal
Fill<<<numBlocks,numThreads>>>(d_input);



// Add Kernal
Add<<<numBlocks,numThreads>>>(d_input,d_output);


//execute Reduce Kernal
Reduce<<<numBlocks,numThreads>>>(d_output,d_input);

cudaThreadSynchronize();

/////////////////////////////////
//copy result from device to host
/////////////////////////////////
//output 


if  (cudaMemcpy(output,d_output,size,cudaMemcpyDeviceToHost)!= cudaSuccess){
    cerr << cudaGetErrorString(cudaGetLastError()) << endl;
    cout << "Output Copy from device to host" << endl;
    exit(1);
}

//clear device buffers
cudaFree(d_input);
cudaFree(d_output);
} 

__global__ void Fill(int *fillItem)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
fillItem[id] = 1;
}
__global__ void Add (int *input1, int* result)
{
int id = blockIdx.x * blockDim.x + threadIdx.x;
result[id] = input1[id] + input1[id];
}
__global__ void Reduce(int *inputArray, int *outputArray)
{
extern __shared__ int sdata[];

// each thread loads one element from global to shared mem
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
sdata[tid] = inputArray[i];
__syncthreads();

// do reduction in shared mem
for(unsigned int s=1; s < blockDim.x; s *= 2)
{
    if(tid % (2*s) == 0)
    {
        sdata[tid] += sdata[tid + s];
    }
__syncthreads();
}


// write result for this block to global mem
if(tid == 0) outputArray[blockIdx.x] = sdata[0];
}

由于

2 个答案:

答案 0 :(得分:4)

编辑:在你的内核中,你试图以超出界限的方式访问数组元素! 你的数组大小是100,但是你使用的是256的线程维度,每个维度都试图写入!您需要使用一致的尺寸。

您收到错误的时间点是?两个malloc函数看起来应该正常运行,并且cudaGetErrorString不太可能出错。我对未知错误的典型体验是,您试图从不应该或不适合的地方复制或复制。

为什么要将未分配的数组复制到内存中?你永远不会在main中填写数组。

此外,您无需使用&lt;&lt;&lt;&gt;&gt;&gt;声明内核函数。只有在使用该功能时才需要这些。

答案 1 :(得分:0)

我遇到了这个错误,结果证明是我的操作系统版本。与其中一个依赖库不匹配。一旦我升级了操作系统然后重新安装了驱动程序,这个错误就消失了。

- 约翰

相关问题