User`s guide

Run Element-wise MATLAB Code on GPU
9-13
Run Element-wise MATLAB Code on GPU
In this section...
“MATLAB Code vs. gpuArray Objects” on page 9-13
“Run Your MATLAB Functions on a GPU” on page 9-13
“Example: Run Your MATLAB Code” on page 9-14
“Supported MATLAB Code” on page 9-15
MATLAB Code vs. gpuArray Objects
You have options for performing MATLAB calculations on the GPU:
You can transfer or create data on the GPU, and use the resulting gpuArray as input
to enhanced built-in functions that support them. For more information and a list of
functions that support gpuArray as inputs, see “Run Built-In Functions on a GPU” on
page 9-8.
You can run your own MATLAB function of element-wise operations on a GPU.
Your decision on which solution to adopt depends on whether the functions you require
are enhanced to support gpuArray, and the performance impact of transferring data to/
from the GPU.
Run Your MATLAB Functions on a GPU
To execute your MATLAB function on a GPU, call arrayfun or bsxfun with a function
handle to the MATLAB function as the first input argument:
result = arrayfun(@myFunction,arg1,arg2);
Subsequent arguments provide inputs to the MATLAB function. These input arguments
can be workspace data or gpuArray. If any of the input arguments is a gpuArray,
the function executes on the GPU and returns a gpuArray. (If none of the inputs is a
gpuArray, then arrayfun and bsxfun execute in the CPU.)
Note arrayfun and bsxfun support only element-wise operations on a GPU.
See the arrayfun and bsxfun reference pages for descriptions of their available options.