User`s guide

Establish Arrays on a GPU
9-3
Establish Arrays on a GPU
In this section...
“Transfer Arrays Between Workspace and GPU” on page 9-3
“Create GPU Arrays Directly” on page 9-4
“Examine gpuArray Characteristics” on page 9-7
Transfer Arrays Between Workspace and GPU
Send Arrays to the GPU
A gpuArray in MATLAB represents an array that is stored on the GPU. Use the
gpuArray function to transfer an array from MATLAB to the GPU:
N = 6;
M = magic(N);
G = gpuArray(M);
G is now a MATLAB gpuArray object that represents the magic square stored on the
GPU. The input provided to gpuArray must be numeric (for example: single, double,
int8, etc.) or logical. (See also “Considerations for Complex Numbers” on page 9-11.)
Retrieve Arrays from the GPU
Use the gather function to retrieve arrays from the GPU to the MATLAB workspace.
This takes an array that is on the GPU represented by a gpuArray object, and transfers
it to the MATLAB workspace as a regular MATLAB array. You can use isequal to
verify that you get the correct values back:
G = gpuArray(ones(100,'uint32'));
D = gather(G);
OK = isequal(D,ones(100,'uint32'))
Examples: Transfer Array
Transfer Array to the GPU
Create a 1000-by-1000 random matrix in MATLAB, and then transfer it to the GPU:
X = rand(1000);
G = gpuArray(X);