Work with Sparse Arrays on a GPU - MATLAB & Simulink (original) (raw)

Main Content

Create Sparse GPU Arrays

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

X = [0 1 0 0 0; 0 0 0 0 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

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, callA(5,:) or A(5,1:end).

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

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

gpuArray | sparse | find