Unavoidable memory leaks in MEX
Show older comments
I'm trying to write a MEX running some CUDA code (can't use feval). Unfortunately, no matter what I do, I can't avoid the memory leaks.
I give, as a parameter to the function, a gpuArray of 0-s. I tried making the prhs non-constant, so I could overwrite it:
double * outPtr=(double* ) mxGetData(prhs[2]);
The data is a gpuArray, so It should be stored on the GPU, but when I pass outPtr as a parameter to a CUDA kernel, I get an error (CUDA-related).
so, overwriting didn't work, I tried:
mxGPUArray * tmp=mxGPUCopyFromMxArray(prhs[2]);
double * outPtr=(double* ) mxGPUGetData(tmp);
and then
plhs[0]=mxGPUCreateMxArrayOnGPU(tmp);
which causes memory leaks on the GPU. I suspect the memory on the GPU isn't freed later when I clear/overwrite the variable in plhs[0].
I tried many things, but none of them solved this problem. I even tried making a persistent variable "out", and destroying it at the beginning of the MEX code (I figured maybe if I explicitly told MATLAB to clear it, instead of expecting MATLAB to clear it as soon as I overwrite it). But it seems mxArrayDestroy doesn't clear memory on GPU.
So, Is there ANY way for me to free the space stored in plhs[0] when I'm done with it?
I am using MATLAB 2013a
3 Comments
Jill Reese
on 26 Aug 2013
Ayal, you asked a very similar question recently and I suggested that you try applying the patch for a known memory leak with GPU computing in R2013a. Was that one of the things that you tried which didn't work?
Best,
Jill
Ayal
on 26 Aug 2013
Ayal
on 27 Aug 2013
Answers (0)
Categories
Find more on GPU CUDA and MEX Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!