CUDA中的并行列表约简
我正在阅读Cuda并行简化白皮书,但不幸的是,我的算法似乎反复产生错误的结果,我似乎无法找出原因(当然教科书上的例子一定有用?当然我只是做了一些非常明显的错误?)。这是我的内核函数: 我的定义:CUDA中的并行列表约简,c,cuda,parallel-processing,gpu,C,Cuda,Parallel Processing,Gpu,我正在阅读Cuda并行简化白皮书,但不幸的是,我的算法似乎反复产生错误的结果,我似乎无法找出原因(当然教科书上的例子一定有用?当然我只是做了一些非常明显的错误?)。这是我的内核函数: 我的定义: #define BLOCK_SIZE 512 __global__ void total(float * inputList, float * outputList, int len) { __shared__ float sdata[2*BLOCK_SIZE]; unsi
#define BLOCK_SIZE 512
__global__ void total(float * inputList, float * outputList, int len) {
__shared__ float sdata[2*BLOCK_SIZE];
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*(blockDim.x*2) + threadIdx.x;
sdata[t] = inputList[i]+inputList[i+blockDim.x];
__syncthreads();
for (unsigned int s=blockDim.x/2; s>0; s>>=1) {
if (tid < s) {
sdata[tid] += sdata[tid + s];
}
__syncthreads();
}
if (tid == 0)
outputList[blockIdx.x] = sdata[0];
}
outputSize = inputSize / (BLOCK_SIZE<<1);
cudaMalloc((void**) &deviceInput, inputSize*sizeof(float));
cudaMalloc((void**) &deviceOutput, outputSize*sizeof(float));
cudaMemcpy(deviceInput, hostInput, inputSize*sizeof(float), cudaMemcpyHostToDevice);
dim3 dimGrid((inputSize-1)/BLOCK_SIZE +1, 1, 1);
dim3 dimBlock(BLOCK_SIZE,1,1);
total<<<dimBlock, dimGrid>>>(deviceInput, deviceOutput, outputSize);
cudaDeviceSynchronize();
cudaMemcpy(hostOutput, deviceOutput, outputSize*sizeof(float), cudaMemcpyDeviceToHost);
我的内核函数:
#define BLOCK_SIZE 512
__global__ void total(float * inputList, float * outputList, int len) {
__shared__ float sdata[2*BLOCK_SIZE];
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*(blockDim.x*2) + threadIdx.x;
sdata[t] = inputList[i]+inputList[i+blockDim.x];
__syncthreads();
for (unsigned int s=blockDim.x/2; s>0; s>>=1) {
if (tid < s) {
sdata[tid] += sdata[tid + s];
}
__syncthreads();
}
if (tid == 0)
outputList[blockIdx.x] = sdata[0];
}
outputSize = inputSize / (BLOCK_SIZE<<1);
cudaMalloc((void**) &deviceInput, inputSize*sizeof(float));
cudaMalloc((void**) &deviceOutput, outputSize*sizeof(float));
cudaMemcpy(deviceInput, hostInput, inputSize*sizeof(float), cudaMemcpyHostToDevice);
dim3 dimGrid((inputSize-1)/BLOCK_SIZE +1, 1, 1);
dim3 dimBlock(BLOCK_SIZE,1,1);
total<<<dimBlock, dimGrid>>>(deviceInput, deviceOutput, outputSize);
cudaDeviceSynchronize();
cudaMemcpy(hostOutput, deviceOutput, outputSize*sizeof(float), cudaMemcpyDeviceToHost);
最后,我的最后计算:
for (int counter = 1; counter < outputSize; counter++) {
hostOutput[0] += hostOutput[counter];
}
for(int counter=1;counter
任何帮助都将不胜感激。代码假定输入大小是块大小的倍数。如果inputSize不是块大小的倍数,它将读取inputList数组的末尾 代码假定输入大小是块大小的倍数。如果inputSize不是块大小的倍数,它将读取inputList数组的末尾 下面代码行中的内核启动配置不正确
total<<<dimBlock, dimGrid>>>(deviceInput, deviceOutput, outputSize);
total(设备输入、设备输出、输出大小);
内核配置的第一个参数是网格大小,第二个参数是块大小
您应该这样做:
total<<<dimGrid, dimBlock>>>(deviceInput, deviceOutput, outputSize);
total(设备输入、设备输出、输出大小);
请始终检查返回的错误代码,以了解程序失败的原因
在当前代码中,内核启动应该失败。如果对
cudaDeviceSynchronize
调用进行错误检查,则可能会找到错误结果的原因。代码下一行中的内核启动配置不正确
total<<<dimBlock, dimGrid>>>(deviceInput, deviceOutput, outputSize);
total(设备输入、设备输出、输出大小);
内核配置的第一个参数是网格大小,第二个参数是块大小
您应该这样做:
total<<<dimGrid, dimBlock>>>(deviceInput, deviceOutput, outputSize);
total(设备输入、设备输出、输出大小);
请始终检查返回的错误代码,以了解程序失败的原因
在当前代码中,内核启动应该失败。对
cudaDeviceSynchronize
调用进行错误检查可能会导致错误结果的原因。内核函数输入参数int-len
似乎从未使用过。内核函数输入参数int-len
似乎从未使用过。这是一个好地方-这总是会导致错误结果。我在回答中提到的假设只会在某些情况下引起问题……好的地方——这总是会导致不正确的结果。我在回答中提到的假设只会在某些情况下引起问题。。。