This is machine translation

Translated by Microsoft


Establish Arrays on a GPU


Create GPU Arrays from Existing Data

Send Arrays 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));


Retrieve Arrays from the GPU


D = gather(G);


Example: Transfer Array to the GPU


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

Example: Transfer Array of a Specified Precision


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


        1024        1024


Example: Construct a Multidimensional Array on the GPU


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


Example: Construct a Vector on the GPU


Z = zeros(8192,1,'gpuArray');
        8192           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


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


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

See Also