User`s guide

9 GPU Computing
9-34
MEX-files can analyze the size of the input and allocate memory of a different size,
or launch grids of a different size, from C or C++ code. In comparison, MATLAB code
that calls CUDAKernel objects must pre-allocated output memory and determine the
grid size.
Access Complex Data
Complex data on a GPU device is stored in interleaved complex format. That is, for a
complex gpuArray A, the real and imaginary parts of element i are stored in consecutive
addresses. MATLAB uses CUDA built-in vector types to store complex data on the device
(see the NVIDIA CUDA C Programming Guide).
Depending on the needs of your kernel, you can cast the pointer to complex data either as
the real type or as the built-in vector type. For example, in MATLAB, suppose you create
a matrix:
a = complex(ones(4,'gpuArray'),ones(4,'gpuArray'));
If you pass a gpuArray to a MEX-function as the first argument (prhs[0]), then you can
get a pointer to the complex data by using the calls:
mxGPUArray const * A = mxGPUCreateFromMxArray(prhs[0]);
mwSize numel_complex = mxGPUGetNumberOfElements(A);
double2 * d_A = (double2 const *)(mxGPUGetDataReadOnly(A));
To treat the array as a real double-precision array of twice the length, you could do it this
way:
mxGPUArray const * A = mxGPUCreateFromMxArray(prhs[0]);
mwSize numel_real =2*mxGPUGetNumberOfElements(A);
double * d_A = (double const *)(mxGPUGetDataReadOnly(A));
Various functions exist to convert data between complex and real formats on
the GPU. These operations require a copy to interleave the data. The function
mxGPUCreateComplexGPUArray takes two real mxGPUArrays and interleaves their
elements to produce a single complex mxGPUArray of the same length. The functions
mxGPUCopyReal and mxGPUCopyImag each copy either the real or the imaginary
elements into a new real mxGPUArray. (There is no equivalent of the mxGetImagData
function for mxGPUArray objects.)