bnp0005 bnp0005 - 3 months ago 6
C++ Question

Is there a better/cleaner/more elegant way to malloc and free in cuda?

I am trying to cudaMalloc a bunch of device pointers, and gracefully exit if any of the mallocs didn't work. I have functioning code - but bloated because I have to cudaFree everything I'd previously malloc'd if one fails. So now I am wondering if there is a more succinct method of accomplishing this. Obviously I can't free something that hasn't been malloc'd - that will definitely cause problems.

Below is the snippet of code I am trying to make more elegant.

//define device pointers
float d_norm, *d_dut, *d_stdt, *d_gamma, *d_zeta;

//allocate space on the device for the vectors and answer
if (cudaMalloc(&d_norm, sizeof(float)*vSize) != cudaSuccess) {
std::cout << "failed malloc";
return;
};

if (cudaMalloc(&d_data, sizeof(float)*vSize) != cudaSuccess) {
std::cout << "failed malloc";
cudaFree(d_norm);
return;
};

if (cudaMalloc(&d_stdt, sizeof(float)*wSize) != cudaSuccess) {
std::cout << "failed malloc";
cudaFree(d_norm);
cudaFree(d_data);
return;
};

if (cudaMalloc(&d_gamma, sizeof(float)*vSize) != cudaSuccess) {
std::cout << "failed malloc";
cudaFree(d_norm);
cudaFree(d_dut);
cudaFree(d_stdt);
return;
};

if (cudaMalloc(&d_zeta, sizeof(float)*w) != cudaSuccess) {
std::cout << "failed malloc";
cudaFree(d_norm);
cudaFree(d_dut);
cudaFree(d_stdt);
cudaFree(d_gamma);
return;
};


This is a shortened version, but you can see how it just keeps building. In reality I am trying to malloc about 15 arrays. It starts getting ugly - but it works correctly.

Thoughts?

Answer

Some possibilities:

  1. cudaDeviceReset() will free all device allocations, without you having to run through a list of pointers.

  2. if you intend to exit (the application), all device allocations are freed automatically upon application termination anyway. The cuda runtime detects the termination of the process associated with an application's device context, and wipes that context at that point. So if you're just going to exit, it should be safe to not perform any cudaFree() operations.