cudaGetLastError returns “unknown error”

自古美人都是妖i 提交于 2019-12-11 06:21:46

问题


I am new to CUDA C. I am writing a simple array Add and Reduce, when it runs the error check for copying from the device back to the host I get an "unknown error". I am not sure if the error checker is faulty and not returning the correct cudaError but I can't work out what is wrong.......

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

Thanks


回答1:


EDIT: In your kernels, you are trying to access array elements way out of bounds! Your array size is 100, yet you are using a thread dimension of 256, each of which is trying to write to that! You need to use consistent sizes.

At which point are you getting the error? The two malloc functions look like they should function correctly, and cudaGetErrorString is unlikely to be wrong. My typical experience with unknown error is that you are trying to copy from or to somewhere you shouldn't, or with the wrong size.

Why are you copying an unassigned array into memory? You never filled out the arrays in main.

Also, you do not need to declare the kernal functions with <<<>>>. Those are only needed when you use the function.




回答2:


I encountered this error, and it turned out to be my OS version. There was a mismatch with one of the dependent libraries. Once I upgraded the OS and then reinstalled the drivers, this error went away.

--John



来源:https://stackoverflow.com/questions/10271747/cudagetlasterror-returns-unknown-error

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