1

私はCUDA Cを初めて使用します。単純な配列のAddと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];
}

ありがとう

4

2 に答える 2

4

編集:あなたのカーネルでは、範囲外の配列要素にアクセスしようとしています! 配列のサイズは 100 ですが、256 のスレッド次元を使用しており、それぞれがそれに書き込もうとしています! 一貫したサイズを使用する必要があります。

どの時点でエラーが発生していますか? 2 つの malloc 関数は正しく機能するように見えますが、cudaGetErrorString が間違っている可能性は低いです。不明なエラーに関する私の典型的な経験は、コピーをコピーしようとしている、またはコピーしてはいけない場所にコピーしようとしている、または間違ったサイズでコピーしようとしているということです。

割り当てられていない配列をメモリにコピーするのはなぜですか? メインの配列に入力したことはありません。

また、カーネル関数を <<<>>> で宣言する必要はありません。これらは、関数を使用する場合にのみ必要です。

于 2012-04-22T20:27:01.657 に答える