Skip to Main Content Skip to Search
Product Documentation

Using GPUArray

Transfer Data Between Workspace and GPU

Send Data to the GPU

A GPUArray in MATLAB represents data that is stored on the GPU. Use the gpuArray function to transfer an array from the MATLAB workspace to the GPU:

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

G is now a MATLAB GPUArray object that represents the data of the magic square stored on the GPU. The data provided as input to gpuArray must be nonsparse, and either 'single', 'double', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', or 'logical'. (For more information, see Data Types.)

Retrieve Data from the GPU

Use the gather function to retrieve data from the GPU to the MATLAB workspace. This takes data that is on the GPU represented by a GPUArray object, and makes it available in the MATLAB workspace as a regular MATLAB variable. You can use isequal to verify that you get the correct data back:

G = gpuArray(ones(100, 'uint32'));
D = gather(G);
OK = isequal(D, ones(100, 'uint32'))

Examples: Transferring Data

Transfer Data 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);

Transfer Data of a Specified Precision.  Create a matrix of double-precision random data in MATLAB, and then transfer the matrix as single-precision from MATLAB to the GPU:

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

Construct an Array for Storing on the GPU.  Construct a 100-by-100 matrix of uint32 ones and transfer it to the GPU. You can accomplish this with a single line of code:

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

Create GPU Data Directly

A number of static methods on the GPUArray class allow you to directly construct arrays on the GPU without having to transfer them from the MATLAB workspace. These constructors require only array size and data class information, so they can construct an array without any element data from the workspace. Use any of the following to directly create an array on the GPU:

parallel.gpu.GPUArray.onesparallel.gpu.GPUArray.colon
parallel.gpu.GPUArray.zerosparallel.gpu.GPUArray.rand
parallel.gpu.GPUArray.infparallel.gpu.GPUArray.randi
parallel.gpu.GPUArray.nanparallel.gpu.GPUArray.randn
parallel.gpu.GPUArray.trueparallel.gpu.GPUArray.linspace
parallel.gpu.GPUArray.falseparallel.gpu.GPUArray.logspace
parallel.gpu.GPUArray.eye 

For a complete list of available static methods in any release, type

methods('parallel.gpu.GPUArray')

The static constructors appear at the bottom of the output from this command.

For help on any one of the constructors, type

help parallel.gpu.GPUArray/functionname

For example, to see the help on the colon constructor, type

help parallel.gpu.GPUArray/colon

Example: Construct an Identity Matrix on the GPU

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

II = parallel.gpu.GPUArray.eye(1024,'int32');
size(II)
        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 = parallel.gpu.GPUArray.ones(100, 100, 50);
size(G)
   100   100    50
classUnderlying(G)
double

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 = parallel.gpu.GPUArray.zeros(8192, 1);
size(Z)
        8192           1

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

Control the Random Stream

The following functions control the random number stream on the GPU:

parallel.gpu.rng
parallel.gpu.RandStream

These functions perform in the same way as rng and RandStream in MATLAB, but with certain limitations. For more information on the use and limits of these functions, type

help parallel.gpu.rng
help parallel.gpu.RandStream

The GPU uses the combined multiplicative recursive generator to create uniform random values, and uses inversion for creating normal values. This is not the default stream in a client MATLAB session on the CPU, but is the equivalent of

RandStream('CombRecursive','NormalTransform','Inversion');

However, a MATLAB worker session does have the same default stream as its GPU, even if it is a worker in a local cluster on the same machine. That is, a MATLAB client and workers do not have the same default stream.

In most cases, it does not matter that the default random stream on the GPU is not the same as the default stream in MATLAB on the CPU. But if you need to reproduce the same stream on both GPU and CPU, you can set the CPU random stream accordingly, and use the same seed to set both streams:

seed=0; n=4;

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

gpu_stream = parallel.gpu.RandStream('CombRecursive','Seed',seed);
parallel.gpu.RandStream.setGlobalStream(gpu_stream);

r = rand(n);
R = parallel.gpu.GPUArray.rand(n);
isequal(r,R)
    1

Examine GPUArray Characteristics

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

FunctionDescription
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 = gpuArray(rand(100));
s = size(G)
    100   100

Built-In Functions That Support GPUArray

A subset of the MATLAB built-in functions supports the use of GPUArray. Whenever any of these functions is called with at least one GPUArray as an input argument, it executes on the GPU and returns a GPUArray as the result. You can mix input from GPUArray and MATLAB workspace data in the same function call. These functions include the discrete Fourier transform (fft), matrix multiplication (mtimes), and left matrix division (mldivide).

The following functions and their symbol operators are enhanced to accept GPUArray input arguments so that they execute on the GPU:

abs
acos
acosh
acot
acoth
acsc
acsch
all
any
arrayfun
asec
asech
asin
asinh
atan
atan2
atanh
beta
betaln
bitand
bitcmp
bitor
bitshift
bitxor
bsxfun
cast
cat
ceil
chol
circshift
classUnderlying
colon
complex
conj
conv
conv2
cos
cosh
cot
coth
csc
csch
ctranspose
cumprod
cumsum
det
diag
diff
disp
display
dot
double
eig
eps
eq
erf
erfc
erfcinv
erfcx
erfinv
exp
expm1
filter
filter2
find
fft
fft2
fftn
fix
floor
fprintf
full
gamma
gammaln
gather
ge
gt
horzcat
hypot
ifft
ifft2
ifftn
imag
ind2sub
int16
int2str
int32
int64
int8
inv
ipermute
isempty
isequal
isequaln
isfinite
isinf
islogical
isnan
isreal
issorted
ldivide
le
length
log
log10
log1p
log2
logical
lt
lu
mat2str
max
meshgrid
min
minus
mldivide
mod
mrdivide
mtimes
ndgrid
ndims
ne
norm
not
num2str
numel
permute
plot (and related)
plus
power
prod
qr
rdivide
real
reallog
realpow
realsqrt
rem
repmat
reshape
round
sec
sech
shiftdim
sign
sin
single
sinh
size
sort
sprintf
sqrt
sub2ind
subsasgn
subsindex
subsref
sum
svd
tan
tanh
times
transpose
tril
triu
uint16
uint32
uint64
uint8
uminus
uplus
vertcat

See the release notes for information about updates for individual functions.

For the complete list of available functions that support GPUArrays in your current version, use the methods function on the GPUArray class:

 methods('parallel.gpu.GPUArray')

To get help on specific overloaded functions, and to learn about any restrictions concerning their support for GPUArray objects, type:

help parallel.gpu.GPUArray/functionname

For example, to see the help on the overload of lu, type

help parallel.gpu.GPUArray/lu

The following functions are not methods of the GPUArray class, but they do work with GPUArray data:

angle
fliplr
flipud
flipdim
fftshift
ifftshift
kron
mean
perms
rank
squeeze
rot90
trace

In most cases, if any of the input arguments to these functions is a GPUArray, their output arrays are GPUArrays. If the output is always scalar, it is MATLAB data in the workspace. If the result is a GPUArray of complex data and all the imaginary parts are zero, these parts are retained and the data remains complex. This could have an impact when using sort, isreal, etc.

Example: Calling Functions on GPUArray Objects

This example uses the fft and real functions, along with the arithmetic operators + and *. All the calculations are performed on the GPU, then gather retrieves the data from the GPU back to the MATLAB workspace.

Ga = gpuArray(rand(1000, 'single'));
Gfft = fft(Ga); 
Gb = (real(Gfft) + Ga) * 6;
G = gather(Gb);

The whos command is instructive for showing where each variable's data is stored.

whos
 Name       Size         Bytes  Class

 G       1000x1000     4000000  single
 Ga      1000x1000         108  parallel.gpu.GPUArray
 Gb      1000x1000         108  parallel.gpu.GPUArray
 Gfft    1000x1000         108  parallel.gpu.GPUArray

Notice that all the arrays are stored on the GPU (GPUArray), except for G, which is the result of the gather function.

  


Free Parallel Computing Interactive Kit

See how to solve large problems with minimal effort and reduce simulation time.

Get free kit

Trials Available

Try the latest versions of parallel computing products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS