我想测试一下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行,我发现对于一个2-GPU的系统,GPU处理时间分别是30毫秒和20毫秒,所以得出cudaMalloc是一个异步调用,cudaFree是一个同步调用。不确定这是否正确,也不确定 CUDA 架构的设计关注点是什么。我的计算能力是2.0,我尝试了cuda4.0和cuda5.0。