CUDA and MATLAB memory Issues Pinned Memory, Is it possible?

Good morning to everyone, I have tried optimizing memory transfers for Matlab data to the gpu, as this is now where most of the time is spent in my function. But unfortunately, every time i use pinned memory, matlab simply crashes… :(
Has anyone had any luck using cudaMallocHost in a mex-file?

I understand that matlab is fairly memory sensitive as indicated in nvidias whitepaper:

Accelerating Matlab with CUDA: “Currently it is not possible to use pinned memory on the host, because the data on the host side neds to be allocated with the special mxAlloc function.”

Although the host side mmemory does not have to be allocated as such, as we can pass the pointer of the data directly to the gpu by just collecting it:

double matrix_gpu,matrizHost;
matrixHost=(double
)mxGetData(prhs[0]);
cudaMalloc((void
*)&matrix_gpu,sizeof(double)mMatrixnMatrixzMatrix);
cudaMemcpy(matrix_gpu,matrixHost,sizeof(double)mMatrixnMat
rix
zMatrix,cudaMemcpyHostToDevice);
//So no mxAlloc needed :)

But according to Boxed Cylons thread (very good for starting with Matlab and CUDA) http://forums.nvidia.com/index.php?showtop…rt=#entry407662

He notes:
-> On the other mail list, I inquired about using cudaMallocHost in a mex file to speed up the host-device communications. This is to use “pinned memory”. Matlab is fairly memory sensitive, so it is not generally possible to allocate memory this way. I suspect, however, that one could actually sometimes get away with using cudaMallocHost() in a mex file - it is a matter of avoiding any calls to matlab. So one could cudaMallocHost(), compute away, and then clear out the CUDA variables before Matlab is aware of what is going on and complain/crash (its the “don’t ask, don’t tell” memory policy).

However I have tried to perform cudaMallocHost() in the mex file without invoking any Matlab functions, but to no avail.

If anyone has been able to perform any memory trasnfer optimizations in Matlab, i would be extremely greatful.
Thanks beforehand, kind regards,
David Lisin.

Hi David, thanks for the post. Just curious why you’re not using Jacket? I’d love to figure out how to help and how to make Jacket better so you can avoid these hassles. Feel free to email me (address in my signature). Best, John

The use of pinned memory within a Matlab mex file was only a suspicion… This issue has been discussed either on this forum or elsewhere, and the consensus seems to be that pinned memory within a mex file is not possible. The whole Matlab/mex file system is apparently closed/proprietary. I’ve certainly never heard of an instance where pinned memory within a mex file worked o.k. (happy to be proved wrong!)

Matlab/mex is just SO memory sensitive - I’ve been dealing with this issue all morning. I have a mex routine that basically works - running on 20 cpus for days on end. Except when it doesn’t work…I get occasional random crashes because of a mex routine. Some memory somewhere is stepping on other memory… (nothing to do with CUDA, however, merely to illustrate the memory sensitivity)