Ich möchte die Summe aller Elemente eines Arrays in CUDA berechnen. Ich habe diesen Code entwickelt. Es kompiliert ohne Fehler. Aber das Ergebnis ist immer Null. Ich habe das ungültige Gerätesymbol von cudaMemcpyFromSymbol
. Ich kann keine Bibliotheken wie Thrust oder Cublas verwenden.ungültiges Gerätesymbol cudaMemcpyFromSymbol CUDA
#define TRIALS_PER_THREAD 4096
#define NUM_BLOCKS 256
#define NUM_THREADS 256
double *dev;
__device__ volatile double pi_gpu = 0;
__global__ void ArraySum(double *array)
{
unsigned int tid = threadIdx.x + blockDim.x * blockIdx.x;
pi_gpu = pi_gpu + array[tid];
__syncthreads();
}
int main (int argc, char *argv[]) {
cudaMalloc((void **) &dev, NUM_BLOCKS * NUM_THREADS * sizeof(double));
double pi_gpu_h;
ArraySum<<<NUM_BLOCKS, NUM_THREADS>>>(dev);
cudaDeviceSynchronize();
cudaError err = cudaMemcpyFromSymbol(&pi_gpu_h, &pi_gpu, sizeof(double), cudaMemcpyDeviceToHost);
if(cudaSuccess != err)
{
fprintf(stderr, "cudaMemcpyFromSymbolfailed : %s\n", cudaGetErrorString(err));
exit(-1);
}
return pi_gpu_h; // this is always zero!!!
}
Sie würden jedem einen großen Gefallen tun, wenn Sie 30 Sekunden brauchten, um Ihren Code korrekt zu formatieren. Es ist unglaublich schwierig, wie gepostet zu lesen. – talonmies