-
Notifications
You must be signed in to change notification settings - Fork 86
Sparse GPU array and broadcasting support #628
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
kshyatt
wants to merge
1
commit into
master
Choose a base branch
from
ksh/sparse
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.
Open
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# on-device sparse array types | ||
# should be excluded from coverage counts | ||
# COV_EXCL_START | ||
using SparseArrays | ||
|
||
# NOTE: this functionality is currently very bare-bones, only defining the array types | ||
# without any device-compatible sparse array functionality | ||
|
||
|
||
# core types | ||
|
||
export GPUSparseDeviceVector, GPUSparseDeviceMatrixCSC, GPUSparseDeviceMatrixCSR, | ||
GPUSparseDeviceMatrixBSR, GPUSparseDeviceMatrixCOO | ||
|
||
abstract type AbstractGPUSparseDeviceMatrix{Tv, Ti} <: AbstractSparseMatrix{Tv, Ti} end | ||
|
||
|
||
struct GPUSparseDeviceVector{Tv,Ti,Vi,Vv} <: AbstractSparseVector{Tv,Ti} | ||
iPtr::Vi | ||
nzVal::Vv | ||
len::Int | ||
nnz::Ti | ||
end | ||
|
||
Base.length(g::GPUSparseDeviceVector) = g.len | ||
Base.size(g::GPUSparseDeviceVector) = (g.len,) | ||
SparseArrays.nnz(g::GPUSparseDeviceVector) = g.nnz | ||
SparseArrays.nonzeroinds(g::GPUSparseDeviceVector) = g.iPtr | ||
SparseArrays.nonzeros(g::GPUSparseDeviceVector) = g.nzVal | ||
|
||
struct GPUSparseDeviceMatrixCSC{Tv,Ti,Vi,Vv} <: AbstractGPUSparseDeviceMatrix{Tv, Ti} | ||
colPtr::Vi | ||
rowVal::Vi | ||
nzVal::Vv | ||
dims::NTuple{2,Int} | ||
nnz::Ti | ||
end | ||
|
||
SparseArrays.rowvals(g::GPUSparseDeviceMatrixCSC) = g.rowVal | ||
SparseArrays.getcolptr(g::GPUSparseDeviceMatrixCSC) = g.colPtr | ||
SparseArrays.nzrange(g::GPUSparseDeviceMatrixCSC, col::Integer) = SparseArrays.getcolptr(g)[col]:(SparseArrays.getcolptr(g)[col+1]-1) | ||
|
||
struct GPUSparseDeviceMatrixCSR{Tv,Ti,Vi,Vv} <: AbstractGPUSparseDeviceMatrix{Tv,Ti} | ||
rowPtr::Vi | ||
colVal::Vi | ||
nzVal::Vv | ||
dims::NTuple{2, Int} | ||
nnz::Ti | ||
end | ||
|
||
struct GPUSparseDeviceMatrixBSR{Tv,Ti,Vi,Vv} <: AbstractGPUSparseDeviceMatrix{Tv,Ti} | ||
rowPtr::Vi | ||
colVal::Vi | ||
nzVal::Vv | ||
dims::NTuple{2,Int} | ||
blockDim::Ti | ||
dir::Char | ||
nnz::Ti | ||
end | ||
|
||
struct GPUSparseDeviceMatrixCOO{Tv,Ti,Vi,Vv} <: AbstractGPUSparseDeviceMatrix{Tv,Ti} | ||
rowInd::Vi | ||
colInd::Vi | ||
nzVal::Vv | ||
dims::NTuple{2,Int} | ||
nnz::Ti | ||
end | ||
|
||
Base.length(g::AbstractGPUSparseDeviceMatrix) = prod(g.dims) | ||
Base.size(g::AbstractGPUSparseDeviceMatrix) = g.dims | ||
SparseArrays.nnz(g::AbstractGPUSparseDeviceMatrix) = g.nnz | ||
SparseArrays.getnzval(g::AbstractGPUSparseDeviceMatrix) = g.nzVal | ||
|
||
struct GPUSparseDeviceArrayCSR{Tv, Ti, Vi, Vv, N, M} <: AbstractSparseArray{Tv, Ti, N} | ||
rowPtr::Vi | ||
colVal::Vi | ||
nzVal::Vv | ||
dims::NTuple{N, Int} | ||
nnz::Ti | ||
end | ||
|
||
function GPUSparseDeviceArrayCSR{Tv, Ti, Vi, Vv, N}(rowPtr::Vi, colVal::Vi, nzVal::Vv, dims::NTuple{N,<:Integer}) where {Tv, Ti<:Integer, M, Vi<:AbstractDeviceArray{<:Integer,M}, Vv<:AbstractDeviceArray{Tv, M}, N} | ||
@assert M == N - 1 "GPUSparseDeviceArrayCSR requires ndims(rowPtr) == ndims(colVal) == ndims(nzVal) == length(dims) - 1" | ||
GPUSparseDeviceArrayCSR{Tv, Ti, Vi, Vv, N, M}(rowPtr, colVal, nzVal, dims, length(nzVal)) | ||
end | ||
|
||
Base.length(g::GPUSparseDeviceArrayCSR) = prod(g.dims) | ||
Base.size(g::GPUSparseDeviceArrayCSR) = g.dims | ||
SparseArrays.nnz(g::GPUSparseDeviceArrayCSR) = g.nnz | ||
SparseArrays.getnzval(g::GPUSparseDeviceArrayCSR) = g.nzVal | ||
|
||
# input/output | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceVector) | ||
println(io, "$(length(A))-element device sparse vector at:") | ||
println(io, " iPtr: $(A.iPtr)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceMatrixCSR) | ||
println(io, "$(length(A))-element device sparse matrix CSR at:") | ||
println(io, " rowPtr: $(A.rowPtr)") | ||
println(io, " colVal: $(A.colVal)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceMatrixCSC) | ||
println(io, "$(length(A))-element device sparse matrix CSC at:") | ||
println(io, " colPtr: $(A.colPtr)") | ||
println(io, " rowVal: $(A.rowVal)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceMatrixBSR) | ||
println(io, "$(length(A))-element device sparse matrix BSR at:") | ||
println(io, " rowPtr: $(A.rowPtr)") | ||
println(io, " colVal: $(A.colVal)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceMatrixCOO) | ||
println(io, "$(length(A))-element device sparse matrix COO at:") | ||
println(io, " rowPtr: $(A.rowPtr)") | ||
println(io, " colInd: $(A.colInd)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", A::GPUSparseDeviceArrayCSR) | ||
println(io, "$(length(A))-element device sparse array CSR at:") | ||
println(io, " rowPtr: $(A.rowPtr)") | ||
println(io, " colVal: $(A.colVal)") | ||
print(io, " nzVal: $(A.nzVal)") | ||
end | ||
|
||
# COV_EXCL_STOP |
Oops, something went wrong.
Oops, something went wrong.
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.
It's a bit inconsistent that we keep the host sparse object layout to the back-end, but define the device one concretely. I'm not sure if it's better to entirely move the definitions away from (or rather into) GPUArrays.jl though. I guess back-ends may want additional control over the object layout in order to facilitate vendor library interactions, but maybe we should then also leave the device-side version up to the back-end and only implement things here in terms of SparseArrays interfaces (
rowvals
,getcolptr
, etc). Thoughts?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.
Yeah I was torn on this too. The advantage here is that libraries get a working device-side implementation "for free" -- they are able to implement their own (better) one and just give
Adapt.jl
information about how to move their host-side structs to it.