在我进入之前,这是对正在发生的事情的总体看法:
一般的想法是我有浮点数的x数组,我想将每一个顺序添加到另一个数组(标量添加):
t = array;
a =数组数组;
t =零
t + = a [0]
t + = a [1]
...
t + = a [N]
其中+ =表示标量加法。
这是直截了当的。我试图缩小我必须尽可能紧凑的代码并保留功能。这里的问题是对于某些大小的数组 - 我看到的问题是大于128 x 128 x 108.基本上复制回主机的内存总和与我计算的不一样。我整天都被困在这里,所以我不会再浪费时间了。我真的无法解释为什么会这样。我理由通过:
该名单可以继续下去。如果你有时间,感谢您浏览这个。该问题几乎与内存相关(即内存大小> 128 * 128 * 108不起作用。因此64 * 128 * 256 将工作,或其任何排列。/ p>
这是完整的源代码(应该与nvcc编译):
#include <cuda.h>
#include <iostream>
#include <stdio.h>
#include <assert.h>
#define BSIZE 8
void cudaCheckError(cudaError_t e,const char * msg) {
if (e != cudaSuccess){
printf("Error number: %d\n",e);
printf("%s\n",msg);
}
};
__global__ void accumulate(float * in,float * out, int3 gdims, int zlevel) {
int idx = blockIdx.x*blockDim.x + threadIdx.x;
int idy = blockIdx.y*blockDim.y + threadIdx.y;
int idz = threadIdx.z;
long int index = (zlevel*((int)BSIZE)+idz)*gdims.x*gdims.y+ \
idy*gdims.x+ \
idx;
if ( idx < gdims.x && idy < gdims.y && (idz + zlevel*(int)BSIZE) < gdims.z) {
out[index] += in[index];
}
};
int main(int argc, char * argv[]) {
int width,
height,
depth;
if (argc != 4) {
printf("Must have 3 inputs: width height depth\n");
exit(0);
}
float tempsum;
int count =0;
width = atoi(argv[1]);
height = atoi(argv[2]);
depth = atoi(argv[3]);
printf("Dimensions (%d,%d,%d)\n",width,height,depth);
int3 dFull;
dFull.x = width+2;
dFull.y = height+2;
dFull.z = depth+2;
printf("Dimensions (%d,%d,%d)\n",dFull.x,dFull.y,dFull.z);
int fMemSize=dFull.x*dFull.y*dFull.z;
int nHostF=9;
float * f_hostZero;
float ** f_dev;
float * f_temp_host;
float * f_temp_dev;
dim3 grid( dFull.x/(int)BSIZE+1, dFull.y/(int)BSIZE + 1);
dim3 threads((int)BSIZE,(int)BSIZE,(int)BSIZE);
printf("Threads (x,y) : (%d,%d)\nGrid (x,y) : (%d,%d)\n",threads.x,threads.y,grid.x,grid.y);
int num_zsteps=dFull.z/(int)BSIZE + 1;
printf("Number of z steps to take : %d\n",num_zsteps);
// Host array allocation
f_temp_host = new float[fMemSize];
f_hostZero = new float[fMemSize];
// Allocate nHostF address on host
f_dev = new float*[nHostF];
// Host array assignment
for(int i=0; i < fMemSize; i++){
f_temp_host[i] = 1.0;
f_hostZero[i] = 0.0;
}
// Device allocations - allocated for array size + 2
for(int i=0; i<nHostF; i++){
cudaMalloc((void**)&f_dev[i],sizeof(float)*fMemSize);
}
// Allocate the decive pointer
cudaMalloc( (void**)&f_temp_dev, sizeof(float)*fMemSize);
cudaCheckError(cudaMemcpy((void *)f_temp_dev,(const void *)f_hostZero,
sizeof(float)*fMemSize,cudaMemcpyHostToDevice),"At first mem copy");
printf("Memory regions allocated\n");
// Copy memory to each array
for(int i=0; i<nHostF; i++){
cudaCheckError(cudaMemcpy((void *)(f_dev[i]),(const void *)f_temp_host,
sizeof(float)*fMemSize,cudaMemcpyHostToDevice),"At first mem copy");
}
// Add value 1.0 (from each array n f_dev[i]) to f_temp_dev
for (int i=0; i<nHostF; i++){
for (int zLevel=0; zLevel<num_zsteps; zLevel++){
accumulate<<<grid,threads>>>(f_dev[i],f_temp_dev,dFull,zLevel);
cudaThreadSynchronize();
}
cudaCheckError(cudaMemcpy((void *)f_temp_host,(const void *)f_temp_dev,
sizeof(float)*fMemSize,cudaMemcpyDeviceToHost),"At mem copy back");
tempsum=0.f;
count =0;
for(int k = 0 ; k< fMemSize; k++){
tempsum += f_temp_host[k];
assert ( (int)f_temp_host[k] == (i+1) );
if ( f_temp_host[k] !=(float)(i+1) ) {
printf("Found invalid return value\n");
exit(0);
}
count++;
}
printf("Total Count: %d\n",count);
printf("Real Array sum: %18f\nTotal values counted : %d\n",tempsum,count*(i+1));
printf("Calculated Array sum: %ld\n\n",(i+1)*fMemSize );
}
for(int i=0; i<nHostF; i++){
cudaFree(f_dev[i]);
}
cudaFree(f_temp_dev);
printf("Memory free. Program successfully complete\n");
delete f_dev;
delete f_temp_host;
}
答案 0 :(得分:4)
您的设备代码没有任何问题。所有发生的事情是,在大问题大小的情况下,您正在耗尽单精度浮点的容量来精确计算代码在大运行大小时产生的大整数值。如果您将主机端汇总代码替换为Kahan summation,请执行以下操作:
tempsum=0.f;
count =0;
float c=0.f;
for(int k = 0 ; k< fMemSize; k++){
float y = f_temp_host[k] - c;
float t = tempsum + y;
c = (t - tempsum) - y;
tempsum = t;
assert ( (int)f_temp_host[k] == (i+1) );
if ( f_temp_host[k] !=(float)(i+1) ) {
printf("Found invalid return value\n");
exit(0);
}
count++;
}
你应该发现代码按照预期的更大尺寸运行。或者,主机侧求和可以用双精度算术来完成。如果您还没有阅读过,我强烈推荐What Every Computer Scientist Should Know About Floating-Point Arithmetic。它将有助于解释你在这个例子中出错的地方,以及它所带来的智慧可能有助于防止将来犯下类似的 faux pas 。