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?