Array stored on GPU
A gpuArray
object represents an array stored on the GPU. To
work with gpuArray
objects, use any GPUenabled MATLAB^{®} function. You can use the array for direct calculations or in CUDA kernels that
execute on the GPU. For more information, see Run MATLAB Functions on a GPU.
If you want to retrieve the array from the GPU, for example when using a function that
does not support gpuArray
objects, use the gather
function.
Note
You can load MAT files containing gpuArray data as inmemory arrays when a GPU is not
available. A gpuArray loaded without a GPU is limited and you cannot use it for
computations. To use a gpuArray loaded without a GPU, retrieve the contents using gather
.
Use gpuArray
to convert an array in the MATLAB workspace into a gpuArray
object. Many MATLAB functions also allow you to create gpuArray
objects directly.
For more information, see Establish Arrays on a GPU.
There are several methods for examining the characteristics of a
gpuArray
object. Most behave like the MATLAB functions of the same name.
existsOnGPU  Determine if gpuArray or CUDAKernel is available on GPU 
isequal  Determine array equality 
isnumeric  Determine whether input is numeric array 
issparse  Determine whether input is sparse 
isUnderlyingType  Determine whether input has specified underlying data type 
length  Length of largest array dimension 
ndims  Number of array dimensions 
size  Array size 
underlyingType  Type of underlying data determining array behavior 
Other methods for gpuArray
objects are too numerous to list here. Most
resemble and behave the same as the MATLAB functions of the same name. See Run MATLAB Functions on a GPU.
If you need increased performance, or if a function is not available for GPU,
gpuArray
supports the following options:
To precompile and run purely elementwise code on gpuArray
objects, use the arrayfun
function.
To run C++ code containing CUDA^{®} device code or library calls, use a MEXfunction. For more information, see Run MEXFunctions Containing CUDA Code.
To run existing GPU kernels written in CUDA C++, use the MATLAB CUDAKernel interface. For more information, see Run CUDA or PTX Code on GPU.
To generate CUDA code from MATLAB code, use GPU Coder™. For more information, see Get Started with GPU Coder (GPU Coder).
You can control the random number stream on the GPU using gpurng
.
None of the following can exceed intmax('int32')
:
The number of elements of a dense array.
The number of nonzero elements of a sparse array.
The size in any given dimension. For example,
zeros(0,3e9,'gpuArray')
is not allowed.
You can also create a gpuArray
object using some MATLAB functions by specifying a gpuArray
output. The following
table lists the available MATLAB functions that can create gpuArray
objects directly.






 gpuArray. colon 
 gpuArray. freqspace 
 gpuArray. linspace 
 gpuArray. logspace 
gpuArray. speye 
For classspecific help on the functions with the gpuArray
prefix,
type
help gpuArray.functionname
where functionname
is the name of the method. For example, to
get help on colon
, type
help gpuArray.colon