Keeping data in device memory via Matlab mex files.

I’m trying to set up Matlab mex files that copy data to the device memory and return a pointer to the data (I’m doing estimations that operate on the same data many times, so want to avoid transferring the data with each call). My problem is I am unsure of how to return the pointer to Matlab (and then back again).

So far I have:

[codebox]#include <cuda.h>

#include “mex.h”

#include “matrix.h”

void mexFunction(int nargout, mxArray *argout, int nargin, const mxArray *argin)

{

float *input, *cuda_mem;

size_t size;

input = (float *)mxGetPr(argin[0]);

size = mxGetM(argin[0]) * mxGetN(argin[0]) * sizeof(float);

cudaMalloc((void **) &cuda_mem, size);

cudaMemcpy(cuda_mem, input, size, cudaMemcpyHostToDevice);

/* Now return the pointer to the data.*/

argout[0] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL);

}[/codebox]

but I don’t know the type that the pointer to the single array on the device will be. Any “pointers” would be greatly appreciated.

If you use the pointer in the same mex file afterwards you can just use a persistent variable. Otherwise my guess would be that you cast it to a int32.

Matlab 2010b has GPGPU support in the parallel computing toolbox where you can do all of this from matlab without the need to write mex files. I have not tried it yet as 2010b is one day old and this functionality was not yet in the beta version, but it sounds very promising.

If you use the pointer in the same mex file afterwards you can just use a persistent variable. Otherwise my guess would be that you cast it to a int32.

Matlab 2010b has GPGPU support in the parallel computing toolbox where you can do all of this from matlab without the need to write mex files. I have not tried it yet as 2010b is one day old and this functionality was not yet in the beta version, but it sounds very promising.