Sparse gpuArray indexing limitations
Show older comments
When trying to extract a sub-matrix of a sparse gpuArray, I get,
>> S=gpuArray(sprand(100,100,0.1));
>> S(1:5,1:5)
Sparse gpuArray matrices only support referencing whole rows or whole columns.
OK, but here, I am indexing a block of complete rows and get the same error,
>> S(1:5,:)
Sparse gpuArray matrices only support referencing whole rows or whole columns.
Aside from this, what is the internal hurdle that makes subsrefing a sparse matrix so hard on the GPU? This has been a limitation for quite a while now.
3 Comments
Walter Roberson
on 3 Jul 2026 at 20:01
I do not have easy access to a gpu system at this time.
I wonder if
S(1,:)
S(:,1)
would work? That is, perhaps the limit is on indexing more than one complete row or column ?
Matt J
6 minutes ago
Theoretically, yes it could be done but doing so creates the complexity issue noted before so the ability isn't directly supported. Reading the gpuArray doc on <Working With Sparse GPU Arrays> again and parsing the addressing discussion more carefully, it appears to be a restriction documented by example and the implication that index is a single value, not by an explicit statement. "Sparse GPU arrays only support referencing whole rows or columns by index. For example ..." uses 5 in that example but doesn't directly state that "index" cannot be a colon expression. Like you apparently, @Matt J, I initially presumed the index could be generalized as with addressing in-memory arrays, sparse or not.
If this operation is needed, it would have to be implemented by extracting each row/column individually and catenating them in local memory as assigning values to sparse GPU arrays by index is not supported. The result would then have to be moved to a GPU array from memory.
It's not clear from the documentation whether the writing restrictions precludes something like
B=gpuArray(sparse([])); % empty sparse GPU array????
for i=1:5
B=[B;S(i,:)];
end
but I suspect they do/will.
Syntax issues aside, even if it were to work, constructing a new sparse matrix dynamically would require the GPU to rebuild the entire CSR/CSC data structure for every iteration. If it were toy-sized arrays this might be feasible, but presuming real cases would be large enough to actually need sparse instead of dense arrays, the reconstruction sequentially would undoubtedly turn out to be prohibitively slow.
It's hard to not expect all the basic indexing flexibility that is inherent in MATLAB memory access to be available, but the GPU is a completely different architecture and the sotware implementation is so drastically different that virtually all the pardigms that are so ingrained just don't carry over effectively to that environment.
Accepted Answer
More Answers (1)
Ben Tordoff
24 minutes ago
1 vote
We have been working on improvements to sparse gpuArray indexing for R2026b. If you have access to the R2026b pre-release please give it a try and send us your feedback. It should address your use-case.
We do try to prioritise features or improvements that people are asking for. If you have other specific requests for improvements to our sparse gpuArray support please let us know: https://supportcases.mathworks.com/mwsupport/s/casecreation?c__caseParameter=Suggest_an_enhancement
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!