Are cudaMalloc and cudaFree synchronous or asynchronous calls?

cuda

Solution

Both functions are synchronous.

Problem

I want to test whether cudaMalloc and cudaFree are synchronous calls, so I did some modification to the "simpleMultiGPU.cu" sample code in CUDA SDK. Following is the part I changed (the added lines are not indented): ``` 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); } ``` By commenting out the cudaMalloc line and cudaFree line respectively in the large loop, I found that for a 2-GPU system, the GPU processing time are 30 milliseconds and 20 milliseconds respectively, so I concluded that cudaMalloc is an asynchronous call and cudaFree is a synchronous call. Not sure this is true or not, and not sure what is the designing concern of the CUDA architecture. My computation capability is 2.0, and I tried both cuda4.0 and cuda5.0.

Original source