Main Content

Work with Sparse Arrays on a GPU

Create Sparse GPU Arrays

You can create a sparse gpuArray either by calling sparse with a gpuArray input, or by calling gpuArray with a sparse input. For example,

X = [0 1 0 0 0; 0 0 0 0 1]
     0     1     0     0     0
     0     0     0     0     1
S = sparse(X)
   (1,2)        1
   (2,5)        1
G = gpuArray(S);   % G is a sparse gpuArray
Gt = transpose(G); % Gt is a sparse gpuArray
F = full(Gt)       % F is a full gpuArray
     0     0
     1     0
     0     0
     0     0
     0     1

Indexing GPU Arrays

Sparse GPU arrays only support referencing whole rows or columns by index. For example, to access the fifth row of sparse matrix A, call A(5,:) or A(5,1:end).

A = gpuArray.speye(10);
A(5,:)
   (1,5)       1
full(A(5,:))
     0     0     0     0     1     0     0     0     0     0

To locate nonzero elements of a sparse GPU array, use the find function. You can then replace the values you want and construct a new sparse gpuArray.

A = gpuArray.speye(10);
[row,col] = find(A);
[row,col]
     1     1
     2     2
     3     3
     4     4
     5     5
     6     6
     7     7
     8     8
     9     9
    10    10

Assigning values to sparse GPU arrays by index is not supported.

Functions That Support Sparse GPU Arrays

This tables lists functions that support sparse gpuArray objects.

abs
acos
acosd
acosh
acot
acotd
acoth
acsc
acscd
acsch
angle
asec
asecd
asech
asin
asind
asinh
atan
atand
atanh
bicg
bicgstab
ceil
cgs
classUnderlying
conj
cos
cosd
cosh
cospi
cot
cotd
coth
csc
cscd
csch
ctranspose
deg2rad
diag
end
eps
exp
expint
expm1
find
fix
floor
full
gmres
gpuArray.speye
imag
isaUnderlying
isdiag
isempty
isequal
isequaln
isfinite
isfloat
isinteger
islogical
isnumeric
isreal
issparse
istril
istriu
isUnderlyingType
length
log
log2
log10
log1p
lsqr
minus
mtimes
mpower
mustBeUnderlyingType
ndims
nextpow2
nnz
nonzeros
norm
numel
nzmax
pcg
plus
power
qmr
rad2deg
real
reallog
realsqrt
round
sec
secd
sech
sign
sin
sind
sinh
sinpi
size
sparse
spfun
spones
sprandsym
sqrt
subsref
sum
tan
tand
tanh
tfqmr
times (.*)
trace
transpose
tril
triu
uminus
underlyingType
uplus 

See Also

| |

Related Topics