CUDA:请帮我在代码中找到错误

时间:2011-08-08 12:32:27

标签: c++ cuda gpu gpu-programming

有代码,使用GPU:

__global__ void gpu_process(float* input, float* weights, float* output, int psize, int size)
{
    int i = blockIdx.x*blockDim.x + threadIdx.x;
    int j = blockIdx.y*blockDim.y + threadIdx.y;
    if(i < psize && j < size)
        output[j] += input[i] * weights[i * size + j];
}
void process(float* input, float* weights, float* output, size_t psize, size_t size)
{
    float* in_d, *w_d, *out_d;
    cudaMalloc((void**)&in_d, psize * sizeof(float));
    cudaMalloc((void**)&w_d, psize * size * sizeof(float));
    cudaMalloc((void**)&out_d, size * sizeof(float));
    for(size_t i = 0; i < size; i++)
        output[i] = 0;
    cudaMemcpy(in_d, input, psize * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(w_d, weights, psize * size * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(out_d, output, size * sizeof(float), cudaMemcpyHostToDevice);
    int rx = psize, ry = size, block_x = min((int)psize, 32), block_y = min((int)size, 32);
    dim3 dimBlock(block_x, block_y);
    dim3 dimGrid(ceil(float(rx) / block_x), ceil(float(ry) / block_y));
    gpu_process<<<dimGrid, dimBlock>>>(in_d, w_d, out_d, psize, size);
    cudaThreadSynchronize();
    cudaMemcpy(output, out_d, size * sizeof(float), cudaMemcpyDeviceToHost);
    cudaFree(in_d);
    cudaFree(out_d);
    cudaFree(w_d);
}

有代码,它们做同样的事情,但只使用CPU:

int blockIdxx, blockIdxy, blockDimx, blockDimy, threadIdxx, threadIdxy;
void cpu_process(float* input, float* weights, float* output, int psize, int size)
{
    int i = blockIdxx*blockDimx + threadIdxx;
    int j = blockIdxy*blockDimy + threadIdxy;
    if(i < psize && j < size)
        output[j] += input[i] * weights[i * size + j];
}
void process(float* input, float* weights, float* output, size_t psize, size_t size)
{
    for(size_t i = 0; i < size; i++)
            output[i] = 0;
    int rx = psize, ry = size, block_x = min((int)psize, 32), block_y = min((int)size, 32);
    blockDimx = block_x;
    blockDimy = block_y;
    int gridDimx = ceil(float(rx) / block_x), gridDimy = ceil(float(ry) / block_y);
    for(blockIdxx = 0; blockIdxx < gridDimx; blockIdxx++)
        for(blockIdxy = 0; blockIdxy < gridDimy; blockIdxy++)
            for(threadIdxx = 0; threadIdxx < blockDimx; threadIdxx++)
                for(threadIdxy = 0; threadIdxy < blockDimy; threadIdxy++)
                    cpu_process(input, weights, output, psize, size);
}

为什么CPU变量工作正常但GPU变量在输出中返回垃圾?

有什么不同

cuda-toolkit版本:4.0

OS:Debian GNU / Linux,cuda从它的存储库安装。

GPU:NVIDIA GeForce GT 525M。

1 个答案:

答案 0 :(得分:0)

cudaThreadSyncronize已弃用,不应使用,而是使用cudaDeviceSyncronize,检查这些错误代码,因为如果线程失败,它们将返回错误。这些也会阻止所有代码,直到任务完成,所以你也可以在中间添加一些时间代码来找到瓶颈。