User`s guide
Run MEX-Functions Containing CUDA Code
9-31
Run MEX-Functions Containing CUDA Code
In this section...
“Write a MEX-File Containing CUDA Code” on page 9-31
“Set Up for MEX-File Compilation” on page 9-32
“Compile a GPU MEX-File” on page 9-33
“Run the Resulting MEX-Functions” on page 9-33
“Comparison to a CUDA Kernel” on page 9-33
“Access Complex Data” on page 9-34
Write a MEX-File Containing CUDA Code
Note Creating MEX-functions for gpuArray data is supported only on 64-bit platforms
(win64, glnxa64, maci64).
As with all MEX-files, a MEX-file containing CUDA code has a single entry point, known
as mexFunction. The MEX-function contains the host-side code that interacts with
gpuArray objects from MATLAB and launches the CUDA code. The CUDA code in the
MEX-file must conform to the CUDA runtime API.
You should call the function mxInitGPU at the entry to your MEX-file. This ensures that
the GPU device is properly initialized and known to MATLAB.
The interface you use to write a MEX-file for gpuArray objects is different from the MEX
interface for standard MATLAB arrays.
You can see an example of a MEX-file containing CUDA code at:
matlabroot/toolbox/distcomp/gpu/extern/src/mex/mexGPUExample.cu
This file contains the following CUDA device function:
void __global__ TimesTwo(double const * const A,
double * const B,
int const N)