Documentation Center

  • Trial Software
  • Product Updates

Run Built-In Functions on a GPU

MATLAB

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:

absabs
acosacos
acoshacosh
acotacot
acothacoth
acscacsc
acschacsch
allall
andand
angle
anyany
arrayfun
asecasec
asechasech
asinasin
asinhasinh
atanatan
atan2atan2
atanhatanh
besseljbesselj
besselybessely
betabeta
betalnbetaln
bitandbitand
bitcmpbitcmp
bitgetbitget
bitorbitor
bitsetbitset
bitshiftbitshift
bitxorbitxor
blkdiag
bsxfun
castcast
catcat
ceilceil
cholchol
circshiftcircshift
classUnderlying
coloncolon
complexcomplex
condcond
conjconj
convconv
conv2conv2
convnconvn
coscos
coshcosh
cotcot
cothcoth
covcov
cross
csccsc
cschcsch
ctransposectranspose
cumprodcumprod
cumsumcumsum
detdet
diagdiag
diffdiff
dispdisp
displaydisplay
dotdot
doubledouble
eigeig
epseps
eqeq
erferf
erfcerfc
erfcinverfcinv
erfcxerfcx
erfinverfinv
expexp
expm1expm1
eye
false
fftfft
fft2fft2
fftnfftn
fftshift
filterfilter
filter2filter2
findfind
fixfix
flipflip
fliplrfliplr
flipudflipud
floorfloor
fprintffprintf
fullfull
gammagamma
gammalngammaln
gather
gege
gtgt
horzcathorzcat
hypothypot
ifftifft
ifft2ifft2
ifftnifftn
ifftshift
imagimag
ind2subind2sub
Inf
int16int16
int2strint2str
int32int32
int64int64
int8int8
interp1interp1
interp2interp2
interp3interp3
interpninterpn
invinv
ipermuteipermute
iscolumn
isemptyisempty
isequalisequal
isequalnisequaln
isfiniteisfinite
isfloatisfloat
isinfisinf
isintegerisinteger
islogicalislogical
ismatrix
ismemberismember
isnanisnan
isnumericisnumeric
isrealisreal
isrow
issortedissorted
issparseissparse
isvector
kron
ldivideldivide
lele
lengthlength
loglog
log10log10
log1plog1p
log2log2
logicallogical
ltlt
lulu
mat2strmat2str
maxmax
meanmean
meshgridmeshgrid
minmin
minusminus
mldividemldivide
modmod
mpowermpower
mrdividemrdivide
mtimesmtimes
NaN
ndgridndgrid
ndimsndims
nene
nnznnz
normnorm
normestnormest
notnot
num2strnum2str
numelnumel
ones
oror
pagefun
perms
permutepermute
plotplot (and related)
plusplus
pow2pow2
powerpower
prodprod
qrqr
rand
randi
randn
rank
rdividerdivide
realreal
reallogreallog
realpowrealpow
realsqrtrealsqrt
remrem
repmatrepmat
reshapereshape
rot90rot90
roundround
secsec
sechsech
shiftdimshiftdim
signsign
sinsin
singlesingle
sinhsinh
sizesize
sortsort
sprintfsprintf
sqrtsqrt
squeeze
std
sub2indsub2ind
subsasgnsubsasgn
subsindexsubsindex
subsrefsubsref
sumsum
svdsvd
tantan
tanhtanh
timestimes
trace
transposetranspose
triltril
triutriu
true
uint16uint16
uint32uint32
uint64uint64
uint8uint8
uminusuminus
uplusuplus
varvar
vertcatvertcat
xorxor
zeros

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

To get information about any restrictions or limitations concerning the support of any of these functions for gpuArray objects, type:

help gpuArray/functionname

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

help gpuArray/lu

In most cases, if any of the input arguments to these functions is a gpuArray, any output arrays are gpuArrays. If the output is always scalar, it returns as 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: Call Functions with gpuArrays

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 = rand(1000,'single','gpuArray');
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  gpuArray
 Gb      1000x1000         108  gpuArray
 Gfft    1000x1000         108  gpuArray

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

Considerations for Complex Numbers

If the output of a function running on the GPU could potentially be complex, you must explicitly specify its input arguments as complex. This applies to gpuArray or to functions called in code run by arrayfun.

For example, if creating a gpuArray which might have negative elements, use G = gpuArray(complex(p)), then you can successfully execute sqrt(G).

Or, within a function passed to arrayfun, if x is a vector of real numbers, and some elements have negative values, sqrt(x) will generate an error; instead you should call sqrt(complex(x)).

The following table lists the functions that might return complex data, along with the input range over which the output remains real.

FunctionInput Range for Real Output
acos(x)abs(x) <= 1
acosh(x)x >= 1
acoth(x)abs(x) >= 1
acsc(x)abs(x) >= 1
asec(x)abs(x) >= 1
asech(x)0 <= x <= 1
asin(x)abs(x) <= 1
atanhabs(x) <= 1
log(x)x >= 0
log1p(x)x >= -1
log10(x)x >= 0
log2(x)x >= 0
power(x,y)x >= 0
reallog(x)x >= 0
realsqrt(x)x >= 0
sqrt(x)x >= 0

Was this topic helpful?