Skip to content

Commit 2ace55f

Browse files
Add adapt_storage methods for explicit sparse formats in CuSparse arrays (#3129)
1 parent e50d7c4 commit 2ace55f

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/cusparse/src/array.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,19 @@ SparseArrays.SparseMatrixCSC(x::CuSparseMatrixCOO) = SparseMatrixCSC(CuSparseMat
683683
# GPU array adaptor
684684
Adapt.adapt_storage(::Type{CuArray}, xs::SparseVector) = CuSparseVector(xs)
685685
Adapt.adapt_storage(::Type{CuArray}, xs::SparseMatrixCSC) = CuSparseMatrixCSC(xs)
686+
687+
# Explicit sparse formats
688+
Adapt.adapt_storage(::Type{<:CuSparseVector}, xs::AbstractSparseVector) = CuSparseVector(xs)
689+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCSC}, xs::AbstractSparseMatrix) = CuSparseMatrixCSC(xs)
690+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCSR}, xs::AbstractSparseMatrix) = CuSparseMatrixCSR(xs)
691+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCOO}, xs::AbstractSparseMatrix) = CuSparseMatrixCOO(xs)
692+
686693
## preserve type parameters
694+
Adapt.adapt_storage(::Type{<:CuSparseVector{T}}, xs::AbstractSparseVector) where {T} = CuSparseVector{T}(xs)
695+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCSC{T}}, xs::AbstractSparseMatrix) where {T} = CuSparseMatrixCSC{T}(xs)
696+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCSR{T}}, xs::AbstractSparseMatrix) where {T} = CuSparseMatrixCSR{T}(xs)
697+
Adapt.adapt_storage(::Type{<:CuSparseMatrixCOO{T}}, xs::AbstractSparseMatrix) where {T} = CuSparseMatrixCOO{T}(xs)
698+
687699
Adapt.adapt_storage(::Type{<:CuArray{T}}, xs::SparseVector) where {T} = CuSparseVector{T}(xs)
688700
Adapt.adapt_storage(::Type{<:CuArray{T}}, xs::SparseMatrixCSC) where {T} = CuSparseMatrixCSC{T}(xs)
689701
Adapt.adapt_storage(::Type{<:CuArray{T, N}}, xs::SparseVector) where {T, N} = CuSparseVector{T}(xs)

lib/cusparse/test/array.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Adapt
2+
13
@testset "array" begin
24
x = sprand(m,0.2)
35
d_x = CuSparseVector(x)
@@ -366,4 +368,30 @@
366368
let A = sprand(Float64, m, n, 0.2)
367369
@test CuSparseMatrixCSC(A) isa CuSparseMatrixCSC{Float64}
368370
end
371+
372+
@testset "adapt" begin
373+
# CPU sparse array to specific GPU sparse array
374+
let A = sprand(Float64, m, n, 0.2)
375+
@test Adapt.adapt(CuSparseMatrixCSC, A) isa CuSparseMatrixCSC{Float64}
376+
@test Adapt.adapt(CuSparseMatrixCSR, A) isa CuSparseMatrixCSR{Float64}
377+
@test Adapt.adapt(CuSparseMatrixCOO, A) isa CuSparseMatrixCOO{Float64}
378+
@test Adapt.adapt(CuSparseMatrixCSC{Float32}, A) isa CuSparseMatrixCSC{Float32}
379+
@test Adapt.adapt(CuSparseMatrixCSR{Float32}, A) isa CuSparseMatrixCSR{Float32}
380+
@test Adapt.adapt(CuSparseMatrixCOO{Float32}, A) isa CuSparseMatrixCOO{Float32}
381+
end
382+
let v = sprand(Float64, m, 0.2)
383+
@test Adapt.adapt(CuSparseVector, v) isa CuSparseVector{Float64}
384+
@test Adapt.adapt(CuSparseVector{Float32}, v) isa CuSparseVector{Float32}
385+
end
386+
387+
# CuArray target keeps defaults
388+
let A = sprand(Float64, m, n, 0.2)
389+
@test Adapt.adapt(CuArray, A) isa CuSparseMatrixCSC{Float64}
390+
@test Adapt.adapt(CuArray{Float32}, A) isa CuSparseMatrixCSC{Float32}
391+
end
392+
let v = sprand(Float64, m, 0.2)
393+
@test Adapt.adapt(CuArray, v) isa CuSparseVector{Float64}
394+
@test Adapt.adapt(CuArray{Float32}, v) isa CuSparseVector{Float32}
395+
end
396+
end
369397
end

0 commit comments

Comments
 (0)