Sparse gpuArray indexing limitations
이전 댓글 표시
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
Walter Roberson
2026년 7월 3일 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
5분 전
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!