可以帮助我任何人!? 我的问题是,当我在我的代码中将我的变量定义为double时,结果不正确但是对于float结果是正确的。 我的代码是:
const int N=10;
__global__ void incrementArrayOnDevice(double *a)
{
int idx = blockIdx.x*blockDim.x + threadIdx.x;
if (idx <N)
a[idx] *= N;
}
int main(void)
{
double *a_h, *b_h; // pointers to host memory
double *a_d; // pointer to device memory
int i;
size_t size = N*sizeof(double);
a_h = (double *)malloc(size);
b_h = (double *)malloc(size);
cudaMalloc((void **) &a_d, size);
for (i=0; i<N;i++){
a_h[i]=i;
b_h[i]=0;
}
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
int blockSize = 4;
int nBlocks = N/blockSize + (N%blockSize == 0?0:1);
incrementArrayOnDevice <<< nBlocks, blockSize >>> (a_d);
cudaMemcpy(b_h, a_d, size, cudaMemcpyDeviceToHost);
for (i=0; i<N;i++)
cout<<a_h[i]<<" ' "<<b_h[i]<<endl;
free(a_h);
free(b_h);
cudaFree(a_d);
getch();
return 0;
}
请帮助我解决这个问题!