Monday, 26 August 2013

Matlab failes to free memory stored on GPU allocated in a MEX file

Matlab failes to free memory stored on GPU allocated in a MEX file

I'm trying to write a MEX which will run some CUDA kernels (I'm not using
feval because I need streams). I tried:
mxGPUArray * tmp=mxGPUCopyFromMxArray(prhs[2]);
double * outPtr=(double* ) mxGPUGetData(tmp);
kernel<<<..>>>(outPtr,...);
works perfectly. unfortunately, it seems Matlab's GPU memory management is
lacking... I have to use plhs[0]=mxGPUCreateMxArrayOnGPU(...). Naturally,
I can't destory it in the MEX code. but it seems that after overwriting
it, MATLAB doesn't destroy it either - meaning I get a memory leak.
Since I couldn't figure out how to make Matlab clear that memory when it's
done, I tried:
double * outPtr=(double* ) mxGetData(prhs[2]);
and calling the kernel with this pointer as input (the kernel writes into
that location), I get an error:
kernel<<<...>>>(outPtr,...);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
The line points to cudaDeviceSynchronize as the one who generates the error
The input is a gpuArray, and I removed the const from void
MexFunction(...), so that i'll be allowed to use the space preallocated to
prhs[2], and not have to allocate more memory.
I figured since it's a gpuArray, the pointer will be to GPU memory and I
won't have any problems. But apparently, I was wrong...
Any idea how to force Matalb to clean the memory on the GPU when i'm done,
or just use the space allocated for prhs[2]? I've tried so many solutions,
non of which worked.
I've read about changing values in-place. problem is, I can't use the
memory I preallocated in Matlab - It's on the GPU, but as I've mentioned,
I still get errors when I use that pointer, and instead I have to create a
new array, which casuses the leak...

No comments:

Post a Comment