我正在尝试计算长度为8192字节的平均256组数据。我有一个内核,它可以处理216个数据集但是更多,并且内核为每个平均值返回0。我正在使用一个非常基本的简化系统来计算平均值。
显卡:GTX 780 Ti
这是我的代码
__global__ void Average(double *Input, int Length, int Sets, double *Average, int N) {
unsigned int Pos = (blockDim.x * blockIdx.x) + threadIdx.x;
unsigned int Offset;
int i = Length / N;
if (Pos < i * Sets) {
Offset = ((Pos / i) * Length) + (Pos % i);
Input[Offset] += Input[Offset + i];
}
__syncthreads();
if (N == Length) {
Average[Pos] = Input[Pos*Length] / Length;
}
}
using namespace std;
int main()
{
const int Length = 8192;
const int Sets =256;
const int Width = Length*Sets;
double *GPU_Average, *GPU_Data;
cudaMalloc((void**)&GPU_Average, CameraWidth*sizeof(double)*Sets);
cudaMalloc((void**)&GPU_Data, CameraWidth*sizeof(double)*Width);
double CPU_Data[Width];
double CPU_Average[Sets];
for (int i = 0; i < Width; i++) {
CPU_Data[i] = i;
}
cudaMemcpy(GPU_Data, CPU_Data, sizeof(double)*Width, cudaMemcpyHostToDevice);
int N = 2;
int Total, Blocks, Threads;
while (N < Length+1) {
Total = (Sets*Length) / N;
if (Total > 1024) {
Threads = 1024;
Blocks = Total / Threads;
}
else {
Threads = Total;
Blocks = 1;
}
Average << < Blocks, Threads>> >(GPU_Data, Length, Sets, GPU_Average, N);
N *= 2;
}
cudaMemcpy(CPU_Average, (GPU_Average), sizeof(double)*Sets, cudaMemcpyDeviceToHost);
return 0;
}
对此事的任何帮助表示赞赏。
答案 0 :(得分:1)
我没有意识到我写的实际代码(不是上面的代码)
cudaMalloc((void**)&GPU_Data, Width*sizeof(double)*Width);
而不是
cudaMalloc((void**)&GPU_Data, sizeof(double)*Width);
这是分配太多内存并导致错误。