Skip to content

Commit e22908f

Browse files
authored
move and rename eltype conversion (#430)
1 parent e76fcbe commit e22908f

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

ext/QuantumToolboxCUDAExt.jl

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

33
using QuantumToolbox
44
using QuantumToolbox: makeVal, getVal
5-
import QuantumToolbox: _sparse_similar
5+
import QuantumToolbox: _sparse_similar, _convert_eltype_wordsize
66
import CUDA: cu, CuArray, allowscalar
77
import CUDA.CUSPARSE: CuSparseVector, CuSparseMatrixCSC, CuSparseMatrixCSR, AbstractCuSparseArray
88
import SparseArrays: SparseVector, SparseMatrixCSC
@@ -81,27 +81,20 @@ function cu(A::QuantumObject; word_size::Union{Val,Int} = Val(64))
8181

8282
return cu(A, makeVal(word_size))
8383
end
84-
cu(A::QuantumObject, word_size::Union{Val{32},Val{64}}) = CuArray{_change_eltype(eltype(A), word_size)}(A)
84+
cu(A::QuantumObject, word_size::Union{Val{32},Val{64}}) = CuArray{_convert_eltype_wordsize(eltype(A), word_size)}(A)
8585
function cu(
8686
A::QuantumObject{ObjType,DimsType,<:SparseVector},
8787
word_size::Union{Val{32},Val{64}},
8888
) where {ObjType<:QuantumObjectType,DimsType<:AbstractDimensions}
89-
return CuSparseVector{_change_eltype(eltype(A), word_size)}(A)
89+
return CuSparseVector{_convert_eltype_wordsize(eltype(A), word_size)}(A)
9090
end
9191
function cu(
9292
A::QuantumObject{ObjType,DimsType,<:SparseMatrixCSC},
9393
word_size::Union{Val{32},Val{64}},
9494
) where {ObjType<:QuantumObjectType,DimsType<:AbstractDimensions}
95-
return CuSparseMatrixCSC{_change_eltype(eltype(A), word_size)}(A)
95+
return CuSparseMatrixCSC{_convert_eltype_wordsize(eltype(A), word_size)}(A)
9696
end
9797

98-
_change_eltype(::Type{T}, ::Val{64}) where {T<:Int} = Int64
99-
_change_eltype(::Type{T}, ::Val{32}) where {T<:Int} = Int32
100-
_change_eltype(::Type{T}, ::Val{64}) where {T<:AbstractFloat} = Float64
101-
_change_eltype(::Type{T}, ::Val{32}) where {T<:AbstractFloat} = Float32
102-
_change_eltype(::Type{Complex{T}}, ::Val{64}) where {T<:Union{Int,AbstractFloat}} = ComplexF64
103-
_change_eltype(::Type{Complex{T}}, ::Val{32}) where {T<:Union{Int,AbstractFloat}} = ComplexF32
104-
10598
QuantumToolbox.to_dense(A::MT) where {MT<:AbstractCuSparseArray} = CuArray(A)
10699

107100
QuantumToolbox.to_dense(::Type{T1}, A::CuArray{T2}) where {T1<:Number,T2<:Number} = CuArray{T1}(A)

src/utilities.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,10 @@ _CType(::Type{Complex{Int32}}) = ComplexF32
190190
_CType(::Type{Complex{Int64}}) = ComplexF64
191191
_CType(::Type{Complex{Float32}}) = ComplexF32
192192
_CType(::Type{Complex{Float64}}) = ComplexF64
193+
194+
_convert_eltype_wordsize(::Type{T}, ::Val{64}) where {T<:Int} = Int64
195+
_convert_eltype_wordsize(::Type{T}, ::Val{32}) where {T<:Int} = Int32
196+
_convert_eltype_wordsize(::Type{T}, ::Val{64}) where {T<:AbstractFloat} = Float64
197+
_convert_eltype_wordsize(::Type{T}, ::Val{32}) where {T<:AbstractFloat} = Float32
198+
_convert_eltype_wordsize(::Type{Complex{T}}, ::Val{64}) where {T<:Union{Int,AbstractFloat}} = ComplexF64
199+
_convert_eltype_wordsize(::Type{Complex{T}}, ::Val{32}) where {T<:Union{Int,AbstractFloat}} = ComplexF32

0 commit comments

Comments
 (0)