Skip to content

Commit 0408069

Browse files
authored
Fix CUDA sparse_to_dense (#386)
2 parents 4832d37 + 74a3c3e commit 0408069

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

10+
- Fix CUDA `sparse_to_dense`. ([#386])
11+
1012
## [v0.25.2]
1113
Release date: 2025-02-02
1214

@@ -104,3 +106,4 @@ Release date: 2024-11-13
104106
[#378]: https://github.com/qutip/QuantumToolbox.jl/issues/378
105107
[#380]: https://github.com/qutip/QuantumToolbox.jl/issues/380
106108
[#383]: https://github.com/qutip/QuantumToolbox.jl/issues/383
109+
[#386]: https://github.com/qutip/QuantumToolbox.jl/issues/386

ext/QuantumToolboxCUDAExt.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module QuantumToolboxCUDAExt
22

33
using QuantumToolbox
44
using QuantumToolbox: makeVal, getVal
5-
import CUDA: cu, CuArray
6-
import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR
5+
import CUDA: cu, CuArray, allowscalar
6+
import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR, AbstractCuSparseArray
77
import SparseArrays: SparseVector, SparseMatrixCSC
88

9+
allowscalar(false)
10+
911
@doc raw"""
1012
CuArray(A::QuantumObject)
1113
@@ -99,10 +101,9 @@ _change_eltype(::Type{T}, ::Val{32}) where {T<:AbstractFloat} = Float32
99101
_change_eltype(::Type{Complex{T}}, ::Val{64}) where {T<:Union{Int,AbstractFloat}} = ComplexF64
100102
_change_eltype(::Type{Complex{T}}, ::Val{32}) where {T<:Union{Int,AbstractFloat}} = ComplexF32
101103

102-
sparse_to_dense(::Type{T}, A::CuArray{T}) where {T<:Number} = A
103-
sparse_to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A)
104-
sparse_to_dense(::Type{T}, A::CuSparseVector) where {T<:Number} = CuArray{T}(A)
105-
sparse_to_dense(::Type{T}, A::CuSparseMatrixCSC) where {T<:Number} = CuArray{T}(A)
106-
sparse_to_dense(::Type{T}, A::CuSparseMatrixCSR) where {T<:Number} = CuArray{T}(A)
104+
QuantumToolbox.sparse_to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A)
105+
106+
QuantumToolbox.sparse_to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A)
107+
QuantumToolbox.sparse_to_dense(::Type{T}, A::AbstractCuSparseArray) where {T<:Number} = CuArray{T}(A)
107108

108109
end

src/qobj/functions.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ Converts a sparse QuantumObject to a dense QuantumObject.
119119
"""
120120
sparse_to_dense(A::QuantumObject) = QuantumObject(sparse_to_dense(A.data), A.type, A.dimensions)
121121
sparse_to_dense(A::MT) where {MT<:AbstractSparseArray} = Array(A)
122-
for op in (:Transpose, :Adjoint)
123-
@eval sparse_to_dense(A::$op{T,<:AbstractSparseMatrix}) where {T<:BlasFloat} = Array(A)
124-
end
125122
sparse_to_dense(A::MT) where {MT<:AbstractArray} = A
126123

127124
sparse_to_dense(::Type{T}, A::AbstractSparseArray) where {T<:Number} = Array{T}(A)

test/ext-test/gpu/cuda_ext.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
@testset "CUDA Extension" verbose = true begin
2+
# Test that scalar indexing is disallowed
3+
@test_throws ErrorException CUDA.rand(1)[1]
4+
25
ψdi = Qobj(Int64[1, 0])
36
ψdf = Qobj(Float64[1, 0])
47
ψdc = Qobj(ComplexF64[1, 0])
@@ -63,6 +66,21 @@
6366
@test typeof(CuSparseMatrixCSR(Xsc).data) == CuSparseMatrixCSR{ComplexF64,Int32}
6467
@test typeof(CuSparseMatrixCSR{ComplexF32}(Xsc).data) == CuSparseMatrixCSR{ComplexF32,Int32}
6568

69+
# Sparse To Dense
70+
# @test sparse_to_dense(cu(ψsi; word_size = 64)).data isa CuVector{Int64} # TODO: Fix this in CUDA.jl
71+
@test sparse_to_dense(cu(ψsf; word_size = 64)).data isa CuVector{Float64}
72+
@test sparse_to_dense(cu(ψsc; word_size = 64)).data isa CuVector{ComplexF64}
73+
# @test sparse_to_dense(cu(Xsi; word_size = 64)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl
74+
@test sparse_to_dense(cu(Xsf; word_size = 64)).data isa CuMatrix{Float64}
75+
@test sparse_to_dense(cu(Xsc; word_size = 64)).data isa CuMatrix{ComplexF64}
76+
77+
# @test sparse_to_dense(Int32, cu(ψsf; word_size = 64)).data isa CuVector{Int32} # TODO: Fix this in CUDA.jl
78+
# @test sparse_to_dense(Float32, cu(ψsf; word_size = 64)).data isa CuVector{Float32} # TODO: Fix this in CUDA.jl
79+
# @test sparse_to_dense(ComplexF32, cu(ψsf; word_size = 64)).data isa CuVector{ComplexF32} # TODO: Fix this in CUDA.jl
80+
# @test sparse_to_dense(Int64, cu(Xsf; word_size = 32)).data isa CuMatrix{Int64} # TODO: Fix this in CUDA.jl
81+
# @test sparse_to_dense(Float64, cu(Xsf; word_size = 32)).data isa CuMatrix{Float64} # TODO: Fix this in CUDA.jl
82+
# @test sparse_to_dense(ComplexF64, cu(Xsf; word_size = 32)).data isa CuMatrix{ComplexF64} # TODO: Fix this in CUDA.jl
83+
6684
# brief example in README and documentation
6785
N = 20
6886
ω64 = 1.0 # Float64

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if (GROUP == "CUDA_Ext")# || (GROUP == "All")
7070
using QuantumToolbox
7171
using CUDA
7272
using CUDA.CUSPARSE
73-
CUDA.allowscalar(false) # Avoid unexpected scalar indexing
73+
# CUDA.allowscalar(false) # This is already set in the extension script
7474

7575
QuantumToolbox.about()
7676
CUDA.versioninfo()

0 commit comments

Comments
 (0)