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