-
Notifications
You must be signed in to change notification settings - Fork 97
Implementing scalar indexing for device-side sparse arrays #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alonsoC1s
wants to merge
10
commits into
JuliaGPU:master
Choose a base branch
from
alonsoC1s:sparseDevIndexing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+229
−2
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
594eb70
Initial (correct) implmentation of getindex for device-side CSC, CSR …
alonsoC1s a278e73
Fixes for COO indices. It works now
alonsoC1s 560392a
Implementing scalar indexing for BSR formatted sparse device arrays
alonsoC1s 22e3cdf
Implementing logical (scalar) indexing
alonsoC1s 1975f82
Fixing incorrect indexing in BSR and COO matrices
alonsoC1s d172e10
Fixing assumption in COO index that colInd is sorted
alonsoC1s 1e4f0de
Fixing indexing assumption for BSR format
alonsoC1s e147da8
Initial testing for device-side sparse array indexing
alonsoC1s 02d362e
Removing comment
alonsoC1s e52db93
Reintroducing erroneously skipped tests
alonsoC1s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # device-level indexing | ||
| using SparseArrays: nonzeroinds, nonzeros, nnz, getcolptr | ||
| using Base: @propagate_inbounds | ||
|
|
||
| Base.IndexStyle(::Type{GPUSparseDeviceVector}) = Base.IndexLinear() | ||
|
|
||
| # Implementing only scalar indexing. Non-scalar indexing would allocate in device code | ||
|
|
||
| ## Adapted from SparseArrays.AbstractSparseVector | ||
| @propagate_inbounds function Base.getindex( | ||
| v::GPUSparseDeviceVector{Tv,Ti}, | ||
| i::Integer, | ||
| ) where {Tv,Ti} | ||
| @boundscheck checkbounds(v, i) | ||
| m = nnz(v) | ||
| nzind = nonzeroinds(v) | ||
| nzval = nonzeros(v) | ||
|
|
||
| ii = searchsortedfirst(nzind, convert(Ti, i)) | ||
| (ii <= m && nzind[ii] == i) ? nzval[ii] : zero(Tv) | ||
| end | ||
|
|
||
| # Logical getindex | ||
| @propagate_inbounds function Base.getindex( | ||
| v::GPUSparseDeviceVector, | ||
| I::AbstractVector{Bool}, | ||
| ) | ||
| isone(count(I)) ? v[findfirst(I)] : | ||
| error( | ||
| "Logical index contains more than one true value. Device arrays only support scalar indexing.", | ||
| ) | ||
| end | ||
|
|
||
| @propagate_inbounds function Base.getindex( | ||
| A::AbstractGPUSparseDeviceMatrix, | ||
| i::Integer, | ||
| J::AbstractVector{Bool}, | ||
| ) | ||
| isone(count(J)) ? A[i, findfirst(J)] : | ||
| error( | ||
| "Logical index contains more than one true value. Device arrays only support scalar indexing.", | ||
| ) | ||
| end | ||
|
|
||
| @propagate_inbounds function Base.getindex( | ||
| A::AbstractGPUSparseDeviceMatrix, | ||
| I::AbstractVector{Bool}, | ||
| j::Integer, | ||
| ) | ||
| isone(count(I)) ? A[findfirst(I), j] : | ||
| error( | ||
| "Logical index contains more than one true value. Device arrays only support scalar indexing.", | ||
| ) | ||
| end | ||
|
|
||
| @propagate_inbounds function Base.getindex( | ||
| A::AbstractGPUSparseDeviceMatrix, | ||
| I::AbstractVector{Bool}, | ||
| J::AbstractVector{Bool}, | ||
| ) | ||
| if (isone(count(I)) && isone(count(J))) | ||
| A[findfirst(I), findfirst(J)] | ||
| else | ||
| failing_len, failing_idx = findmax((count(I), count(J))) | ||
| error( | ||
| "Logical index $(failing_idx == 1 ? "I" : "J") contains $(failing_len) true " * | ||
| "values. Device arrays only support scalar indexing.", | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| @propagate_inbounds Base.getindex( | ||
| A::AbstractGPUSparseDeviceMatrix, | ||
| I::Tuple{Integer,Integer}, | ||
| ) = getindex(A, I[1], I[2]) | ||
|
|
||
| # Scalar getindex methods linear-scan the minor axis rather than binary-searching | ||
| # and sum across matching entries. cuSPARSE formats don't guarantee sorted indices | ||
| # within a major-axis slice (e.g. SpGEMM output may leave CSR columns unsorted | ||
| # within a row, and COO is only guaranteed row-sorted), nor uniqueness — duplicate | ||
| # (i, j) entries are permitted and their values sum, matching the convention of | ||
| # Julia's `sparse()` constructor and SciPy/CuPy. For Bool we OR instead of sum, | ||
| # also matching `sparse()`, since Bool + Bool doesn't stay Bool. | ||
| sum_duplicate(a, b) = a + b | ||
| sum_duplicate(a::Bool, b::Bool) = a | b | ||
|
|
||
| ## Adapted logic from SparseArrays.AbstractSparseMatrixCSC | ||
| @propagate_inbounds function Base.getindex( | ||
| A::GPUSparseDeviceMatrixCSC{Tv,Ti}, | ||
| i::Integer, | ||
| j::Integer, | ||
| ) where {Tv,Ti} | ||
| @boundscheck checkbounds(A, i, j) | ||
| colPtr, rowVal, nzVal = getcolptr(A), rowvals(A), nonzeros(A) | ||
|
|
||
| # Range of possible row indices | ||
| rl = convert(Ti, @inbounds colPtr[j]) | ||
| rr = convert(Ti, @inbounds colPtr[j+1] - 1) | ||
| (rl > rr) && return zero(Tv) | ||
|
|
||
| ii = searchsortedfirst(rowVal, convert(Ti, i), rl, rr, Base.Order.Forward) | ||
| (ii <= nnz(A) && rowVal[ii] == i) ? nzVal[ii] : zero(Tv) | ||
| end | ||
|
|
||
| @propagate_inbounds function Base.getindex( | ||
| A::GPUSparseDeviceMatrixCSR{Tv,Ti}, | ||
| i::Integer, | ||
| j::Integer, | ||
| ) where {Tv,Ti} | ||
| @boundscheck checkbounds(A, i, j) | ||
| rowPtr, colVal, nzVal = A.rowPtr, A.colVal, A.nzVal | ||
|
|
||
| # Range of possible col indices | ||
| rt = convert(Ti, @inbounds rowPtr[i]) | ||
| rb = convert(Ti, @inbounds rowPtr[i+1] - 1) | ||
| (rt > rb) && return zero(Tv) | ||
|
|
||
| jj = searchsortedfirst(colVal, convert(Ti, j), rt, rb, Base.Order.Forward) | ||
| (jj <= nnz(A) && colVal[jj] == j) ? nzVal[jj] : zero(Tv) | ||
| end | ||
|
|
||
| ## Adapted from CUDA.jl/blob/lib/cusparse/src/array.jl#L490 | ||
| @propagate_inbounds function Base.getindex( | ||
| A::GPUSparseDeviceMatrixCOO{Tv,Ti}, | ||
| i::Integer, | ||
| j::Integer, | ||
| ) where {Tv,Ti} | ||
| # COO in CUDA is assumed to be sorted by row (not col): | ||
| #https://docs.nvidia.com/cuda/cusparse/storage-formats.html?highlight=coo#coordinate-coo | ||
| @boundscheck checkbounds(A, i, j) | ||
| rowInd, colInd, nzVal = A.rowInd, A.colInd, A.nzVal | ||
|
|
||
| # Looking for the range s.t. rowInd[r1:r2] .== i | ||
| rl = searchsortedfirst(rowInd, i, Base.Order.Forward) | ||
| (rl > nnz(A) || rowInd[rl] > i) && return zero(Tv) | ||
| rr = min(searchsortedfirst(rowInd, i+1, Base.Order.Forward), nnz(A)) | ||
| # Important to exclude rr, as including it un-sorts colInd[rl:rr] | ||
| # Column is not guaranteed to be sorted. We linear scan | ||
| result = zero(Tv) | ||
| for k in rl:rl | ||
| A.colInd[k] == i && (result = sum_duplicate(result, nonzeros(A)[k])) | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| ## Adapted from CUDA.jl/blob/lib/cusparse/src/array.jl#L500 | ||
| @propagate_inbounds function Base.getindex( | ||
| A::GPUSparseDeviceMatrixBSR{Tv,Ti}, | ||
| i::Integer, | ||
| j::Integer, | ||
| ) where {Tv,Ti} | ||
| @boundscheck checkbounds(A, i, j) | ||
|
|
||
| i_block, i_idx = fldmod1(i, A.blockDim) | ||
| j_block, j_idx = fldmod1(j, A.blockDim) | ||
| block_idx = (i_idx-1) * A.blockDim + j_idx - 1 | ||
| c1 = convert(Ti, A.rowPtr[i_block]) | ||
| c2 = convert(Ti, A.rowPtr[i_block+1]-1) | ||
| result = zero(T) | ||
| for k in c1:c2 | ||
| A.colVal[k] == i_block && (result == sum_duplicate(result, nonzeros(a)[k+block_idx])) | ||
| end | ||
| return result | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this conversion work? You might need to force
i::Tiabove, not sure...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked with less common number types. This is probably something that would come up naturally during the full testing suite