
Run MEX-Functions Containing CUDA Code
9-33
Compile a GPU MEX-File
When you have set up the options file, use the mex command in MATLAB to compile
a MEX-file containing the CUDA code. You can compile the example file using the
command:
mex -largeArrayDims mexGPUExample.cu
The -largeArrayDims option is required to ensure that 64-bit values for array
dimensions are passed to the MEX API.
Run the Resulting MEX-Functions
The MEX-function in this example multiplies every element in the input array by 2 to get
the values in the output array. To test it, start with a gpuArray in which every element is
1:
x = ones(4,4,'gpuArray');
y = mexGPUExample(x)
y =
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
Both the input and output arrays are gpuArray objects:
disp(['class(x) = ',class(x),', class(y) = ',class(y)])
class(x) = gpuArray, class(y) = gpuArray
Comparison to a CUDA Kernel
Parallel Computing Toolbox also supports CUDAKernel objects that can be used to
integrate CUDA code with MATLAB. Consider the following when choosing the MEX-file
approach versus the CUDAKernel approach:
• MEX-files can interact with host-side libraries, such as the NVIDIA Performance
Primitives (NPP) or CUFFT libraries, and can also contain calls from the host to
functions in the CUDA runtime library.
Komentarze do niniejszej Instrukcji