cudaMalloc和cudaFree是同步还是异步调用?

时间:2012-12-21 21:42:32

标签: cuda

我想测试cudaMalloc和cudaFree是否是同步调用,所以我对CUDA SDK中的“simpleMultiGPU.cu”示例代码进行了一些修改。以下是我更改的部分(添加的行没有缩进):

float *dd[GPU_N];;
for (i = 0; i < GPU_N; i++){cudaSetDevice(i); cudaMalloc((void**)&dd[i], sizeof(float));}
    //Start timing and compute on GPU(s)
    printf("Computing with %d GPUs...\n", GPU_N);
    StartTimer();

    //Copy data to GPU, launch the kernel and copy data back. All asynchronously
    for (i = 0; i < GPU_N; i++)
    {   
        //Set device
        checkCudaErrors(cudaSetDevice(i));

        //Copy input data from CPU
        checkCudaErrors(cudaMemcpyAsync(plan[i].d_Data, plan[i].h_Data, plan[i].dataN * sizeof(float), cudaMemcpyHostToDevice, plan[i].stream));

        //Perform GPU computations
        reduceKernel<<<BLOCK_N, THREAD_N, 0, plan[i].stream>>>(plan[i].d_Sum, plan[i].d_Data, plan[i].dataN);
        getLastCudaError("reduceKernel() execution failed.\n");

        //Read back GPU results
        checkCudaErrors(cudaMemcpyAsync(plan[i].h_Sum_from_device, plan[i].d_Sum, ACCUM_N *sizeof(float), cudaMemcpyDeviceToHost, plan[i].stream));
cudaMalloc((void**)&dd[i],sizeof(float));
cudaFree(dd[i]);

//cudaStreamSynchronize(plan[i].stream);
    }   

通过在大循环中分别注释出cudaMalloc线和cudaFree线,我发现对于双GPU系统,GPU处理时间分别为30毫秒和20毫秒,因此我得出结论,cudaMalloc是异步调用, cudaFree是同步通话。不确定这是真的,也不确定CUDA架构的设计问题是什么。 我的计算能力是2.0,我尝试了cuda4.0和cuda5.0。

1 个答案:

答案 0 :(得分:7)

两个功能都是同步的。