This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Establish Arrays on a GPU

A gpuArray in MATLAB® represents an array that is stored on the GPU. For a complete list of functions that support arrays on the GPU, see Run MATLAB Functions on a GPU.

Create GPU Arrays from Existing Data

Send Arrays to the GPU

GPU arrays can be created by transferring existing arrays from the workspace to the GPU. Use the gpuArray function to transfer an array from MATLAB to the GPU:

N = 6;
M = magic(N);
G = gpuArray(M);

You can accomplish this in a single line of code:

G = gpuArray(magic(N));

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 Work with Complex Numbers on a GPU.)

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'))

Gathering back to the CPU can be costly, and is generally not necessary unless you need to use your result with functions that do not support gpuArray.

Example: 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);

Example: Transfer Array of a Specified Precision

Create a matrix of double-precision random values in MATLAB, and then transfer the matrix as single-precision from MATLAB to the GPU:

X = rand(1000);
G = gpuArray(single(X));

Create GPU Arrays Directly

A number of methods of the gpuArray class allow you to directly construct arrays on the GPU without having to transfer arrays from the MATLAB workspace. These constructors require only array size and data class information, so they can construct an array without any elements from the workspace.

Example: Construct an Identity Matrix on the GPU

To create a 1024-by-1024 identity matrix of type int32 on the GPU, type

II = eye(1024,'int32','gpuArray');
        1024        1024

With one numerical argument, you create a 2-dimensional matrix.

Example: Construct a Multidimensional Array on the GPU

To create a 3-dimensional array of ones with data class double on the GPU, type

G = ones(100,100,50,'gpuArray');
   100   100    50

The default class of the data is double, so you do not have to specify it.

Example: Construct a Vector on the GPU

To create a 8192-element column vector of zeros on the GPU, type

Z = zeros(8192,1,'gpuArray');
        8192           1

For a column vector, the size of the second dimension is 1.

Control the Random Stream for gpuArray

On the GPU, the random number generator functions rand, randn, and randi use different settings compared to the client MATLAB session on the CPU. The following functions control the random number stream on the GPU:


These functions perform in the same way as rng and RandStream in MATLAB, but with certain limitations on the GPU. For more information, see gpurng, or type

help parallel.gpu.RandStream

The GPU uses the Combined Multiplicative Recursive generator ('CombRecursive' or 'mrg32k3a') by default to create uniformly distributed random values, and uses the Inversion method to generate normally distributed values.

To set the client MATLAB session on the CPU to use the same settings as the default settings on the GPU, use the following command:


In most cases, it does not matter that the default random number generator on the GPU is not the same as the default generator in MATLAB on the CPU. However, if you need to reproduce the same results on both the GPU and CPU, you can set the CPU random number generator accordingly, and use the same seed for both generators:

seed=0; n=4;

cpu_stream = RandStream('CombRecursive','Seed',seed,'NormalTransform','Inversion');


r = rand(n);             % On CPU
R = rand(n,'gpuArray');  % On GPU
OK = isequal(r,R)

When running parallel MATLAB code, the default random number generator settings on worker MATLAB sessions are the same as those of the GPU, even if the workers are in a local cluster on the same machine. That is, a MATLAB client and its workers do not have the same default random number generator settings.

There are three supported random generators on the GPU. The Combined Multiplicative Recursive generator is the default because it is a popular and reliable industry standard generator for parallel computing. The following commands select the GPU random number generator, using the default seed of 0:


For more information about generating random numbers on a GPU, and a comparison between GPU and CPU generation, see Control Random Number Streams. For an example that shows performance comparisons for different random generators, see Generating Random Numbers on a GPU.

Examine gpuArray Characteristics

There are several functions available for examining the characteristics of a gpuArray object:

classUnderlyingClass of the underlying data in the array
existsOnGPUIndication if array exists on the GPU and is accessible
isrealIndication if array data is real
lengthLength of vector or largest array dimension
ndimsNumber of dimensions in the array
sizeSize of array dimensions

For example, to examine the size of the gpuArray object G, type:

G = rand(100,'gpuArray');
s = size(G)
    100   100

See Also