Establish Arrays on a GPU
Create GPU Arrays from Existing Data
Send Arrays to the GPU
You can create GPU arrays by transferring existing arrays from host memory to the GPU. Transfer an array from host memory to the GPU using the
gpuArray function supports only numeric input arguments, for example,
logical input arguments. The
gpuArray function supports complex-valued inputs. For more information, see Work with Complex Numbers on a GPU.
N = 6; M = magic(N); G = gpuArray(M);
Retrieve Arrays from the GPU
To transfer arrays from the GPU to host memory, use the
gather function. This function returns a regular MATLAB array that is stored in host memory. Verify that the
gather function returns the correct value by using
Transferring data to and from the GPU can be costly. Calling
gather is generally not necessary unless you need to use your result with functions that do not support
G = gpuArray(ones(100,"uint32")); D = gather(G); OK = isequal(D,ones(100,"uint32"))
Transfer Array to the GPU
Create a 1000-by-1000 random matrix and then transfer it to the GPU. For more information about generating random numbers on the GPU, see Random Number Streams on a GPU.
X = rand(1000); G = gpuArray(X);
For more information on generating random numbers of the GPU, see Random Number Streams on a GPU.
Transfer Array of a Specified Precision
Create a matrix of double-precision random values and transfer the matrix in single-precision format to the GPU.
X = rand(1000); G = gpuArray(single(X));
Create GPU Arrays Directly
You can directly create arrays on the GPU by specifying the
gpuArray type as an input argument for some functions. These functions require only array size and data class information, so they can construct an array without having to transfer any elements from host memory. For more information, see
You can also create arrays on the GPU by mirroring the data type of an existing
gpuArray using the
"like" name-value argument. You can use some functions to construct arrays with the same data type as a prototype array
p by providing
like=p as an input argument. Functions that support the
"like" option include
Create Identity Matrix on the GPU
Create a 1024-by-1024 identity matrix of type
int32 on the GPU.
II = eye(1024,"int32","gpuArray"); size(II)
Create Multidimensional Array on the GPU
Create a three-dimensional array of ones with the
double data type on the GPU.
G = ones(100,100,50,"gpuArray"); size(G)
100 100 50
Create Vector on the GPU
Create a 8192-element column vector of zeros on the GPU.
Z = zeros(8192,1,"gpuArray"); size(Z)
Create Array on the GPU Based on Prototype Array
Create a 100-by-100 array of random numbers with the same data type as a prototype array.
G = gpuArray([1 2 3]); R = rand(100,like=G);
Use these functions to examine the characteristics of a
|Class of the underlying data in the array|
|Indication if array exists on the GPU and is accessible|
|Indication if array data is real|
Determine if underlying array data is of specified class, such as
|Determine if two or more arrays are equal|
|Determine if an array is of a numeric data type|
|Determine if an array is sparse|
|Length of vector or largest array dimension|
|Validate that array has specified underlying type, such as double|
|Number of dimensions in the array|
|Size of array dimensions|
Examine the size of the
G = rand(100,"gpuArray"); s = size(G)
Save and Load
You can save
gpuArray variables as MAT files. When you save a
gpuArray, MATLAB saves the data as a
gpuArray variable in a MAT file. When you load a MAT file containing a
gpuArray variable, MATLAB loads the data onto the GPU as a
You can load MAT files containing
gpuArray data as in-memory 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
For more information about how to save and load variables in the MATLAB workspace, see Save and Load Workspace Variables.