|
| 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 CuTensorMap{T, S, N₁, N₂}(t::TensorMap{T, S, N₁, N₂, A}) where {T, S, N₁, N₂, A} |
| 7 | + return CuTensorMap{T, S, N₁, N₂}(CuArray(t.data), t.space) |
| 8 | +end |
| 9 | + |
| 10 | +# project_symmetric! doesn't yet work for GPU types, so do this on the host, then copy |
| 11 | +function TensorKit._project_symmetric_and_check(::Type{T}, ::Type{A}, data::AbstractArray, V::TensorMapSpace; tol = sqrt(eps(real(float(eltype(data)))))) where {T, A <: CuVector{T}} |
| 12 | + h_t = TensorKit.TensorMapWithStorage{T, Vector{T}}(undef, V) |
| 13 | + h_t = TensorKit.project_symmetric!(h_t, Array(data)) |
| 14 | + # verify result |
| 15 | + isapprox(Array(reshape(data, dims(h_t))), convert(Array, h_t); atol = tol) || |
| 16 | + throw(ArgumentError("Data has non-zero elements at incompatible positions")) |
| 17 | + return TensorKit.TensorMapWithStorage{T, A}(A(h_t.data), V) |
| 18 | +end |
| 19 | + |
| 20 | +for (fname, felt) in ((:zeros, :zero), (:ones, :one)) |
| 21 | + @eval begin |
| 22 | + function CUDA.$fname( |
| 23 | + codomain::TensorSpace{S}, |
| 24 | + domain::TensorSpace{S} = one(codomain) |
| 25 | + ) where {S <: IndexSpace} |
| 26 | + return CUDA.$fname(codomain ← domain) |
| 27 | + end |
| 28 | + function CUDA.$fname( |
| 29 | + ::Type{T}, codomain::TensorSpace{S}, |
| 30 | + domain::TensorSpace{S} = one(codomain) |
| 31 | + ) where {T, S <: IndexSpace} |
| 32 | + return CUDA.$fname(T, codomain ← domain) |
| 33 | + end |
| 34 | + CUDA.$fname(V::TensorMapSpace) = CUDA.$fname(Float64, V) |
| 35 | + function CUDA.$fname(::Type{T}, V::TensorMapSpace) where {T} |
| 36 | + t = CuTensorMap{T}(undef, V) |
| 37 | + fill!(t, $felt(T)) |
| 38 | + return t |
| 39 | + end |
| 40 | + end |
| 41 | +end |
| 42 | + |
| 43 | +for randfun in (:curand, :curandn) |
| 44 | + randfun! = Symbol(randfun, :!) |
| 45 | + @eval begin |
| 46 | + # converting `codomain` and `domain` into `HomSpace` |
| 47 | + function $randfun( |
| 48 | + codomain::TensorSpace{S}, |
| 49 | + domain::TensorSpace{S} = one(codomain), |
| 50 | + ) where {S <: IndexSpace} |
| 51 | + return $randfun(codomain ← domain) |
| 52 | + end |
| 53 | + function $randfun( |
| 54 | + ::Type{T}, codomain::TensorSpace{S}, |
| 55 | + domain::TensorSpace{S} = one(codomain), |
| 56 | + ) where {T, S <: IndexSpace} |
| 57 | + return $randfun(T, codomain ← domain) |
| 58 | + end |
| 59 | + function $randfun( |
| 60 | + rng::Random.AbstractRNG, ::Type{T}, |
| 61 | + codomain::TensorSpace{S}, |
| 62 | + domain::TensorSpace{S} = one(codomain), |
| 63 | + ) where {T, S <: IndexSpace} |
| 64 | + return $randfun(rng, T, codomain ← domain) |
| 65 | + end |
| 66 | + |
| 67 | + # filling in default eltype |
| 68 | + $randfun(V::TensorMapSpace) = $randfun(Float64, V) |
| 69 | + function $randfun(rng::Random.AbstractRNG, V::TensorMapSpace) |
| 70 | + return $randfun(rng, Float64, V) |
| 71 | + end |
| 72 | + |
| 73 | + # filling in default rng |
| 74 | + function $randfun(::Type{T}, V::TensorMapSpace) where {T} |
| 75 | + return $randfun(Random.default_rng(), T, V) |
| 76 | + end |
| 77 | + |
| 78 | + # implementation |
| 79 | + function $randfun( |
| 80 | + rng::Random.AbstractRNG, ::Type{T}, |
| 81 | + V::TensorMapSpace |
| 82 | + ) where {T} |
| 83 | + t = CuTensorMap{T}(undef, V) |
| 84 | + $randfun!(rng, t) |
| 85 | + return t |
| 86 | + end |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +for randfun in (:rand, :randn, :randisometry) |
| 91 | + randfun! = Symbol(randfun, :!) |
| 92 | + @eval begin |
| 93 | + # converting `codomain` and `domain` into `HomSpace` |
| 94 | + function $randfun( |
| 95 | + ::Type{A}, codomain::TensorSpace{S}, |
| 96 | + domain::TensorSpace{S} |
| 97 | + ) where {A <: CuArray, S <: IndexSpace} |
| 98 | + return $randfun(A, codomain ← domain) |
| 99 | + end |
| 100 | + function $randfun( |
| 101 | + ::Type{T}, ::Type{A}, codomain::TensorSpace{S}, |
| 102 | + domain::TensorSpace{S} |
| 103 | + ) where {T, S <: IndexSpace, A <: CuArray{T}} |
| 104 | + return $randfun(T, A, codomain ← domain) |
| 105 | + end |
| 106 | + function $randfun( |
| 107 | + rng::Random.AbstractRNG, ::Type{T}, ::Type{A}, |
| 108 | + codomain::TensorSpace{S}, |
| 109 | + domain::TensorSpace{S} |
| 110 | + ) where {T, S <: IndexSpace, A <: CuArray{T}} |
| 111 | + return $randfun(rng, T, A, codomain ← domain) |
| 112 | + end |
| 113 | + |
| 114 | + # accepting single `TensorSpace` |
| 115 | + $randfun(::Type{A}, codomain::TensorSpace) where {A <: CuArray} = $randfun(A, codomain ← one(codomain)) |
| 116 | + function $randfun(::Type{T}, ::Type{A}, codomain::TensorSpace) where {T, A <: CuArray{T}} |
| 117 | + return $randfun(T, A, codomain ← one(codomain)) |
| 118 | + end |
| 119 | + function $randfun( |
| 120 | + rng::Random.AbstractRNG, ::Type{T}, |
| 121 | + ::Type{A}, codomain::TensorSpace |
| 122 | + ) where {T, A <: CuArray{T}} |
| 123 | + return $randfun(rng, T, A, codomain ← one(domain)) |
| 124 | + end |
| 125 | + |
| 126 | + # filling in default eltype |
| 127 | + $randfun(::Type{A}, V::TensorMapSpace) where {A <: CuArray} = $randfun(eltype(A), A, V) |
| 128 | + function $randfun(rng::Random.AbstractRNG, ::Type{A}, V::TensorMapSpace) where {A <: CuArray} |
| 129 | + return $randfun(rng, eltype(A), A, V) |
| 130 | + end |
| 131 | + |
| 132 | + # filling in default rng |
| 133 | + function $randfun(::Type{T}, ::Type{A}, V::TensorMapSpace) where {T, A <: CuArray{T}} |
| 134 | + return $randfun(Random.default_rng(), T, A, V) |
| 135 | + end |
| 136 | + |
| 137 | + # implementation |
| 138 | + function $randfun( |
| 139 | + rng::Random.AbstractRNG, ::Type{T}, |
| 140 | + ::Type{A}, V::TensorMapSpace |
| 141 | + ) where {T, A <: CuArray{T}} |
| 142 | + t = CuTensorMap{T}(undef, V) |
| 143 | + $randfun!(rng, t) |
| 144 | + return t |
| 145 | + end |
| 146 | + end |
| 147 | +end |
| 148 | + |
| 149 | +function Base.convert(::Type{CuTensorMap}, t::AbstractTensorMap) |
| 150 | + return copy!(CuTensorMap{scalartype(t)}(undef, space(t)), t) |
| 151 | +end |
| 152 | + |
| 153 | +# Scalar implementation |
| 154 | +#----------------------- |
| 155 | +function TensorKit.scalar(t::CuTensorMap) |
| 156 | + # TODO: should scalar only work if N₁ == N₂ == 0? |
| 157 | + return @allowscalar dim(codomain(t)) == dim(domain(t)) == 1 ? |
| 158 | + first(blocks(t))[2][1, 1] : throw(DimensionMismatch()) |
| 159 | +end |
| 160 | + |
| 161 | +TensorKit.scalartype(A::StridedCuArray{T}) where {T} = T |
| 162 | +TensorKit.scalartype(::Type{<:CuTensorMap{T}}) where {T} = T |
| 163 | +TensorKit.scalartype(::Type{<:CuArray{T}}) where {T} = T |
| 164 | + |
| 165 | +function Base.convert( |
| 166 | + TT::Type{CuTensorMap{T, S, N₁, N₂}}, |
| 167 | + t::AbstractTensorMap{<:Any, S, N₁, N₂} |
| 168 | + ) where {T, S, N₁, N₂} |
| 169 | + if typeof(t) === TT |
| 170 | + return t |
| 171 | + else |
| 172 | + tnew = TT(undef, space(t)) |
| 173 | + return copy!(tnew, t) |
| 174 | + end |
| 175 | +end |
| 176 | + |
| 177 | +function LinearAlgebra.isposdef(t::CuTensorMap) |
| 178 | + domain(t) == codomain(t) || |
| 179 | + throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same")) |
| 180 | + InnerProductStyle(spacetype(t)) === EuclideanInnerProduct() || return false |
| 181 | + for (c, b) in blocks(t) |
| 182 | + # do our own hermitian check |
| 183 | + isherm = TensorKit.MatrixAlgebraKit.ishermitian(b; atol = eps(real(eltype(b))), rtol = eps(real(eltype(b)))) |
| 184 | + isherm || return false |
| 185 | + isposdef(Hermitian(b)) || return false |
| 186 | + end |
| 187 | + return true |
| 188 | +end |
| 189 | + |
| 190 | +function Base.promote_rule( |
| 191 | + ::Type{<:TT₁}, |
| 192 | + ::Type{<:TT₂} |
| 193 | + ) where { |
| 194 | + S, N₁, N₂, TTT₁, TTT₂, |
| 195 | + TT₁ <: CuTensorMap{TTT₁, S, N₁, N₂}, |
| 196 | + TT₂ <: CuTensorMap{TTT₂, S, N₁, N₂}, |
| 197 | + } |
| 198 | + T = TensorKit.VectorInterface.promote_add(TTT₁, TTT₂) |
| 199 | + return CuTensorMap{T, S, N₁, N₂} |
| 200 | +end |
| 201 | + |
| 202 | +# CuTensorMap exponentation: |
| 203 | +function TensorKit.exp!(t::CuTensorMap) |
| 204 | + domain(t) == codomain(t) || |
| 205 | + error("Exponential of a tensor only exist when domain == codomain.") |
| 206 | + for (c, b) in blocks(t) |
| 207 | + copy!(b, parent(Base.exp(Hermitian(b)))) |
| 208 | + end |
| 209 | + return t |
| 210 | +end |
| 211 | + |
| 212 | +# functions that don't map ℝ to (a subset of) ℝ |
| 213 | +for f in (:sqrt, :log, :asin, :acos, :acosh, :atanh, :acoth) |
| 214 | + sf = string(f) |
| 215 | + @eval function Base.$f(t::CuTensorMap) |
| 216 | + domain(t) == codomain(t) || |
| 217 | + throw(SpaceMismatch("`$($sf)` of a tensor only exist when domain == codomain")) |
| 218 | + T = complex(float(scalartype(t))) |
| 219 | + tf = similar(t, T) |
| 220 | + for (c, b) in blocks(t) |
| 221 | + copy!(block(tf, c), parent($f(Hermitian(b)))) |
| 222 | + end |
| 223 | + return tf |
| 224 | + end |
| 225 | +end |
0 commit comments