GPU arrays can be created by transferring existing arrays from the workspace to the GPU. Use
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
gpuArray must be numeric (for example:
etc.) or logical. (See also Work with Complex Numbers on a GPU.)
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
Create a 1000-by-1000 random matrix in MATLAB, and then transfer it to the GPU:
X = rand(1000); G = gpuArray(X);
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));
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.
To create a 1024-by-1024 identity matrix of type
the GPU, type
II = eye(1024,'int32','gpuArray'); size(II)
With one numerical argument, you create a 2-dimensional matrix.
To create a 3-dimensional array of ones with data class
double on the GPU, type
G = ones(100,100,50,'gpuArray'); size(G)
100 100 50
The default class of the data is
double, so you do not have
to specify it.
To create a 8192-element column vector of zeros on the GPU, type
Z = zeros(8192,1,'gpuArray'); size(Z)
For a column vector, the size of the second dimension is 1.
On the GPU, the random number generator functions
randi use different
settings compared to the client MATLAB session on the CPU. The following functions control the random
number stream on the GPU:
The GPU uses the Combined Multiplicative Recursive generator
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'); RandStream.setGlobalStream(cpu_stream); gpurng('CombRecursive',seed); 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:
gpurng(0,'CombRecursive') gpurng(0,'Philox') gpurng(0,'Threefry')
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.
There are several functions available for examining the characteristics of a gpuArray object:
|Class of the underlying data in the array|
|Indication if array exists on the GPU and is accessible|
|Indication if array data is real|
|Length of vector or largest array dimension|
|Number of dimensions in the array|
|Size of array dimensions|
For example, to examine the size of the gpuArray object
G = rand(100,'gpuArray'); s = size(G)