|
| 1 | +const CuTensorMap{T, S, N₁, N₂} = TensorMap{T, S, N₁, N₂, CuVector{T, CUDA.DeviceMemory}} |
| 2 | +const CuTensor{T, S, N} = CuTensorMap{T, S, N, 0} |
| 3 | + |
| 4 | +const AdjointCuTensorMap{T, S, N₁, N₂} = AdjointTensorMap{T, S, N₁, N₂, CuTensorMap{T, S, N₁, N₂}} |
| 5 | + |
| 6 | +function TensorKit.tensormaptype(S::Type{<:IndexSpace}, N₁, N₂, TorA::Type{<:StridedCuArray}) |
| 7 | + if TorA <: CuArray |
| 8 | + return TensorMap{eltype(TorA), S, N₁, N₂, CuVector{eltype(TorA), CUDA.DeviceMemory}} |
| 9 | + else |
| 10 | + throw(ArgumentError("argument $TorA should specify a scalar type (`<:Number`) or a storage type `<:CuVector{<:Number}`")) |
| 11 | + end |
| 12 | +end |
| 13 | + |
| 14 | +TensorKit.matrixtype(::Type{<:TensorMap{T, S, N₁, N₂, A}}) where {T, S, N₁, N₂, A <: CuVector{T}} = CuMatrix{T} |
| 15 | + |
| 16 | +function CuTensorMap{T}(::UndefInitializer, V::TensorMapSpace{S, N₁, N₂}) where {T, S, N₁, N₂} |
| 17 | + return CuTensorMap{T, S, N₁, N₂}(undef, V) |
| 18 | +end |
| 19 | + |
| 20 | +function CuTensorMap{T}( |
| 21 | + ::UndefInitializer, codomain::TensorSpace{S}, |
| 22 | + domain::TensorSpace{S} |
| 23 | + ) where {T, S} |
| 24 | + return CuTensorMap{T}(undef, codomain ← domain) |
| 25 | +end |
| 26 | +function CuTensor{T}(::UndefInitializer, V::TensorSpace{S}) where {T, S} |
| 27 | + return CuTensorMap{T}(undef, V ← one(V)) |
| 28 | +end |
| 29 | +# constructor starting from block data |
| 30 | +""" |
| 31 | + CuTensorMap(data::AbstractDict{<:Sector,<:CuMatrix}, codomain::ProductSpace{S,N₁}, |
| 32 | + domain::ProductSpace{S,N₂}) where {S<:ElementarySpace,N₁,N₂} |
| 33 | + CuTensorMap(data, codomain ← domain) |
| 34 | + CuTensorMap(data, domain → codomain) |
| 35 | +
|
| 36 | +Construct a `CuTensorMap` by explicitly specifying its block data. |
| 37 | +
|
| 38 | +## Arguments |
| 39 | +- `data::AbstractDict{<:Sector,<:CuMatrix}`: dictionary containing the block data for |
| 40 | + each coupled sector `c` as a matrix of size `(blockdim(codomain, c), blockdim(domain, c))`. |
| 41 | +- `codomain::ProductSpace{S,N₁}`: the codomain as a `ProductSpace` of `N₁` spaces of type |
| 42 | + `S<:ElementarySpace`. |
| 43 | +- `domain::ProductSpace{S,N₂}`: the domain as a `ProductSpace` of `N₂` spaces of type |
| 44 | + `S<:ElementarySpace`. |
| 45 | +
|
| 46 | +Alternatively, the domain and codomain can be specified by passing a [`HomSpace`](@ref) |
| 47 | +using the syntax `codomain ← domain` or `domain → codomain`. |
| 48 | +""" |
| 49 | +function CuTensorMap( |
| 50 | + data::AbstractDict{<:Sector, <:CuArray}, |
| 51 | + V::TensorMapSpace{S, N₁, N₂} |
| 52 | + ) where {S, N₁, N₂} |
| 53 | + T = eltype(valtype(data)) |
| 54 | + t = CuTensorMap{T}(undef, V) |
| 55 | + for (c, b) in blocks(t) |
| 56 | + haskey(data, c) || throw(SectorMismatch("no data for block sector $c")) |
| 57 | + datac = data[c] |
| 58 | + size(datac) == size(b) || |
| 59 | + throw(DimensionMismatch("wrong size of block for sector $c")) |
| 60 | + copy!(b, datac) |
| 61 | + end |
| 62 | + for (c, b) in data |
| 63 | + c ∈ blocksectors(t) || isempty(b) || |
| 64 | + throw(SectorMismatch("data for block sector $c not expected")) |
| 65 | + end |
| 66 | + return t |
| 67 | +end |
| 68 | +function CuTensorMap(data::CuArray{T}, V::TensorMapSpace{S, N₁, N₂}) where {T, S, N₁, N₂} |
| 69 | + return CuTensorMap{T, S, N₁, N₂}(vec(data), V) |
| 70 | +end |
| 71 | + |
| 72 | +for (fname, felt) in ((:zeros, :zero), (:ones, :one)) |
| 73 | + @eval begin |
| 74 | + function CUDA.$fname( |
| 75 | + codomain::TensorSpace{S}, |
| 76 | + domain::TensorSpace{S} = one(codomain) |
| 77 | + ) where {S <: IndexSpace} |
| 78 | + return CUDA.$fname(codomain ← domain) |
| 79 | + end |
| 80 | + function CUDA.$fname( |
| 81 | + ::Type{T}, codomain::TensorSpace{S}, |
| 82 | + domain::TensorSpace{S} = one(codomain) |
| 83 | + ) where {T, S <: IndexSpace} |
| 84 | + return CUDA.$fname(T, codomain ← domain) |
| 85 | + end |
| 86 | + CUDA.$fname(V::TensorMapSpace) = CUDA.$fname(Float64, V) |
| 87 | + function CUDA.$fname(::Type{T}, V::TensorMapSpace) where {T} |
| 88 | + t = CuTensorMap{T}(undef, V) |
| 89 | + fill!(t, $felt(T)) |
| 90 | + return t |
| 91 | + end |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +for randfun in (:curand, :curandn) |
| 96 | + randfun! = Symbol(randfun, :!) |
| 97 | + @eval begin |
| 98 | + # converting `codomain` and `domain` into `HomSpace` |
| 99 | + function $randfun( |
| 100 | + codomain::TensorSpace{S}, |
| 101 | + domain::TensorSpace{S} = one(codomain), |
| 102 | + ) where {S <: IndexSpace} |
| 103 | + return $randfun(codomain ← domain) |
| 104 | + end |
| 105 | + function $randfun( |
| 106 | + ::Type{T}, codomain::TensorSpace{S}, |
| 107 | + domain::TensorSpace{S} = one(codomain), |
| 108 | + ) where {T, S <: IndexSpace} |
| 109 | + return $randfun(T, codomain ← domain) |
| 110 | + end |
| 111 | + function $randfun( |
| 112 | + rng::Random.AbstractRNG, ::Type{T}, |
| 113 | + codomain::TensorSpace{S}, |
| 114 | + domain::TensorSpace{S} = one(codomain), |
| 115 | + ) where {T, S <: IndexSpace} |
| 116 | + return $randfun(rng, T, codomain ← domain) |
| 117 | + end |
| 118 | + |
| 119 | + # filling in default eltype |
| 120 | + $randfun(V::TensorMapSpace) = $randfun(Float64, V) |
| 121 | + function $randfun(rng::Random.AbstractRNG, V::TensorMapSpace) |
| 122 | + return $randfun(rng, Float64, V) |
| 123 | + end |
| 124 | + |
| 125 | + # filling in default rng |
| 126 | + function $randfun(::Type{T}, V::TensorMapSpace) where {T} |
| 127 | + return $randfun(Random.default_rng(), T, V) |
| 128 | + end |
| 129 | + |
| 130 | + # implementation |
| 131 | + function $randfun( |
| 132 | + rng::Random.AbstractRNG, ::Type{T}, |
| 133 | + V::TensorMapSpace |
| 134 | + ) where {T} |
| 135 | + t = CuTensorMap{T}(undef, V) |
| 136 | + $randfun!(rng, t) |
| 137 | + return t |
| 138 | + end |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +for randfun in (:rand, :randn, :randisometry) |
| 143 | + randfun! = Symbol(randfun, :!) |
| 144 | + @eval begin |
| 145 | + # converting `codomain` and `domain` into `HomSpace` |
| 146 | + function $randfun( |
| 147 | + ::Type{A}, codomain::TensorSpace{S}, |
| 148 | + domain::TensorSpace{S} |
| 149 | + ) where {A <: CuArray, S <: IndexSpace} |
| 150 | + return $randfun(A, codomain ← domain) |
| 151 | + end |
| 152 | + function $randfun( |
| 153 | + ::Type{T}, ::Type{A}, codomain::TensorSpace{S}, |
| 154 | + domain::TensorSpace{S} |
| 155 | + ) where {T, S <: IndexSpace, A <: CuArray{T}} |
| 156 | + return $randfun(T, A, codomain ← domain) |
| 157 | + end |
| 158 | + function $randfun( |
| 159 | + rng::Random.AbstractRNG, ::Type{T}, ::Type{A}, |
| 160 | + codomain::TensorSpace{S}, |
| 161 | + domain::TensorSpace{S} |
| 162 | + ) where {T, S <: IndexSpace, A <: CuArray{T}} |
| 163 | + return $randfun(rng, T, A, codomain ← domain) |
| 164 | + end |
| 165 | + |
| 166 | + # accepting single `TensorSpace` |
| 167 | + $randfun(::Type{A}, codomain::TensorSpace) where {A <: CuArray} = $randfun(A, codomain ← one(codomain)) |
| 168 | + function $randfun(::Type{T}, ::Type{A}, codomain::TensorSpace) where {T, A <: CuArray{T}} |
| 169 | + return $randfun(T, A, codomain ← one(codomain)) |
| 170 | + end |
| 171 | + function $randfun( |
| 172 | + rng::Random.AbstractRNG, ::Type{T}, |
| 173 | + ::Type{A}, codomain::TensorSpace |
| 174 | + ) where {T, A <: CuArray{T}} |
| 175 | + return $randfun(rng, T, A, codomain ← one(domain)) |
| 176 | + end |
| 177 | + |
| 178 | + # filling in default eltype |
| 179 | + $randfun(::Type{A}, V::TensorMapSpace) where {A <: CuArray} = $randfun(eltype(A), A, V) |
| 180 | + function $randfun(rng::Random.AbstractRNG, ::Type{A}, V::TensorMapSpace) where {A <: CuArray} |
| 181 | + return $randfun(rng, eltype(A), A, V) |
| 182 | + end |
| 183 | + |
| 184 | + # filling in default rng |
| 185 | + function $randfun(::Type{T}, ::Type{A}, V::TensorMapSpace) where {T, A <: CuArray{T}} |
| 186 | + return $randfun(Random.default_rng(), T, A, V) |
| 187 | + end |
| 188 | + |
| 189 | + # implementation |
| 190 | + function $randfun( |
| 191 | + rng::Random.AbstractRNG, ::Type{T}, |
| 192 | + ::Type{A}, V::TensorMapSpace |
| 193 | + ) where {T, A <: CuArray{T}} |
| 194 | + t = CuTensorMap{T}(undef, V) |
| 195 | + $randfun!(rng, t) |
| 196 | + return t |
| 197 | + end |
| 198 | + end |
| 199 | +end |
| 200 | + |
| 201 | +function Base.convert(::Type{CuTensorMap}, t::AbstractTensorMap) |
| 202 | + return copy!(CuTensorMap{scalartype(t)}(undef, space(t)), t) |
| 203 | +end |
| 204 | + |
| 205 | +# Scalar implementation |
| 206 | +#----------------------- |
| 207 | +function TensorKit.scalar(t::CuTensorMap) |
| 208 | + # TODO: should scalar only work if N₁ == N₂ == 0? |
| 209 | + return @allowscalar dim(codomain(t)) == dim(domain(t)) == 1 ? |
| 210 | + first(blocks(t))[2][1, 1] : throw(DimensionMismatch()) |
| 211 | +end |
| 212 | + |
| 213 | +TensorKit.scalartype(A::StridedCuArray{T}) where {T} = T |
| 214 | +TensorKit.scalartype(::Type{<:CuTensorMap{T}}) where {T} = T |
| 215 | +TensorKit.scalartype(::Type{<:CuArray{T}}) where {T} = T |
| 216 | + |
| 217 | +function TensorKit.similarstoragetype(TT::Type{<:CuTensorMap{TTT, S, N₁, N₂}}, ::Type{T}) where {TTT, T, S, N₁, N₂} |
| 218 | + return CuVector{T, CUDA.DeviceMemory} |
| 219 | +end |
| 220 | + |
| 221 | +function Base.convert( |
| 222 | + TT::Type{CuTensorMap{T, S, N₁, N₂}}, |
| 223 | + t::AbstractTensorMap{<:Any, S, N₁, N₂} |
| 224 | + ) where {T, S, N₁, N₂} |
| 225 | + if typeof(t) === TT |
| 226 | + return t |
| 227 | + else |
| 228 | + tnew = TT(undef, space(t)) |
| 229 | + return copy!(tnew, t) |
| 230 | + end |
| 231 | +end |
| 232 | + |
| 233 | +function LinearAlgebra.isposdef(t::CuTensorMap) |
| 234 | + domain(t) == codomain(t) || |
| 235 | + throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same")) |
| 236 | + InnerProductStyle(spacetype(t)) === EuclideanInnerProduct() || return false |
| 237 | + for (c, b) in blocks(t) |
| 238 | + # do our own hermitian check |
| 239 | + isherm = TensorKit.MatrixAlgebraKit.ishermitian(b; atol = eps(real(eltype(b))), rtol = eps(real(eltype(b)))) |
| 240 | + isherm || return false |
| 241 | + isposdef(Hermitian(b)) || return false |
| 242 | + end |
| 243 | + return true |
| 244 | +end |
| 245 | + |
| 246 | +function Base.promote_rule( |
| 247 | + ::Type{<:TT₁}, |
| 248 | + ::Type{<:TT₂} |
| 249 | + ) where { |
| 250 | + S, N₁, N₂, TTT₁, TTT₂, |
| 251 | + TT₁ <: CuTensorMap{TTT₁, S, N₁, N₂}, |
| 252 | + TT₂ <: CuTensorMap{TTT₂, S, N₁, N₂}, |
| 253 | + } |
| 254 | + T = TensorKit.VectorInterface.promote_add(TTT₁, TTT₂) |
| 255 | + return CuTensorMap{T, S, N₁, N₂} |
| 256 | +end |
| 257 | + |
| 258 | +# Conversion to CuArray: |
| 259 | +#---------------------- |
| 260 | +# probably not optimized for speed, only for checking purposes |
| 261 | +function Base.convert(::Type{CuArray}, t::AbstractTensorMap) |
| 262 | + I = sectortype(t) |
| 263 | + if I === Trivial |
| 264 | + convert(CuArray, t[]) |
| 265 | + else |
| 266 | + cod = codomain(t) |
| 267 | + dom = domain(t) |
| 268 | + T = sectorscalartype(I) <: Complex ? complex(scalartype(t)) : |
| 269 | + sectorscalartype(I) <: Integer ? scalartype(t) : float(scalartype(t)) |
| 270 | + A = CUDA.zeros(T, dims(cod)..., dims(dom)...) |
| 271 | + for (f₁, f₂) in fusiontrees(t) |
| 272 | + F = convert(CuArray, (f₁, f₂)) |
| 273 | + Aslice = StridedView(A)[axes(cod, f₁.uncoupled)..., axes(dom, f₂.uncoupled)...] |
| 274 | + add!(Aslice, StridedView(TensorKit._kron(convert(CuArray, t[f₁, f₂]), F))) |
| 275 | + end |
| 276 | + return A |
| 277 | + end |
| 278 | +end |
0 commit comments