I wrote a mex function and noticed that every time I run it, more and more memory disappears from my GPU, even though I overwrite the previous results every time. In my attempts to find the source of the problem, I wrote the following code (the file is called MexMemoryTrack
):
#include "mex.h"
#include "gpu/mxGPUArray.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, mxArray const *prhs[])
{
mxInitGPU();
const mxGPUArray * inp=mxGPUCreateFromMxArray(prhs[0]);
const mxGPUArray * ms=mxGPUCreateFromMxArray(prhs[1]);
const double * inpPtr=(const double*) mxGPUGetDataReadOnly(inp);
const double * masksPtr=(const double*) mxGPUGetDataReadOnly(ms);
mxGPUArray * out=mxGPUCopyFromMxArray(prhs[2]);
double * outPtr=(double* ) mxGPUGetData(out);
plhs[0] = mxGPUCreateMxArrayOnGPU(out);
mxGPUDestroyGPUArray(inp);
mxGPUDestroyGPUArray(ms);
mxGPUDestroyGPUArray(out);
}
I run it using:
foo=gpuArray.zeros([3 3 10000 18]);
foo2=gpuArray.randn([7 7 10000 20]);
foo3=gpuArray.randn([5 5 18 20]);
dumdum=MexMemoryTrack(foo2,foo3,foo);
If I put this code in a loop, all my memory ends up disappearing and I get an "out of memory" error. It's very simple. I allocate the memory, I destroy the memory I created except plhs[0] = mxGPUCreateMxArrayOnGPU(out);
which isn't, and shouldn't be destroyed. Since I'm overwriting dumdum
(the only lhs
argument), I assume Matlab is smart enough to either overwrite the values, or free them and re-allocate space for dumdum
. Using clear dumdum
doesn't work either (if that was the solution, I'd be worried about how to keep the information returned...).
Am I missing something?
Could the compiler be the source of the problem (Visual Studio 2010)? Maybe it doesn't work well with Matlab (I'm using Matlab 2013a)?
After many google searches, I eventually reached a (possible) solution, found here
according to this, lhs is not overwritten when, so when calling the code more than once, copies of previous results remain in the memory - so maybe lhs[0]
should be destroyed in the beginning of the code (though first we need to check if there is anything to destroy, as it may not have been allocated yet)
I am unable to verify that this will indeed solve the problem, as I am currently without access to a computer with MATLAB and a graphics card. Basically, the (possible) problem is that the lhs isn't overwritten, but re-allocated (I am actually not surprised, but I wouldn't have guessed that, since I have a poor understanding of exactly how data is handled by MATLAB).
If anyone happens to test out this theory, please let me know what you find out.
In a couple of posts
Memory leak while using gpuArray in parallel computing toolbox 2013a
it was recognized that:
When calling functions using
gpuArray
data inMATLAB R2013a
, MATLAB's memory usage can increase. A very large number of GPU function calls might exhaust the available memory, causing anOut of Memory
orJava heap space error
.
There is a patch published at this bug report page capable to overcome the problem. This bug has been fixed as of MATLAB R2013b
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.