|
| 1 | +const CuTensorMap{T,S,N₁,N₂,A<:CuVector{T}} = TensorMap{T,S,N₁,N₂,A} |
| 2 | +const CuTensor{T, S, N, A<:CuVector{T}} = CuTensorMap{T, S, N, 0, A} |
| 3 | + |
| 4 | +function TensorKit.tensormaptype(S::Type{<:IndexSpace}, N₁, N₂, TorA::Type{<:StridedCuArray}) |
| 5 | + if TorA <: CuVector |
| 6 | + return TensorMap{scalartype(TorA),S,N₁,N₂,TorA} |
| 7 | + else |
| 8 | + throw(ArgumentError("argument $TorA should specify a scalar type (`<:Number`) or a storage type `<:CuVector{<:Number}`")) |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +function CuTensorMap{T}(::UndefInitializer, V::TensorMapSpace{S, N₁, N₂}) where {T, S, N₁, N₂} |
| 13 | + return CuTensorMap{T,S,N₁,N₂,CuVector{T}}(undef, V) |
| 14 | +end |
| 15 | + |
| 16 | +function CuTensorMap{T}(::UndefInitializer, codomain::TensorSpace{S}, |
| 17 | + domain::TensorSpace{S}) where {T,S} |
| 18 | + return CuTensorMap{T}(undef, codomain ← domain) |
| 19 | +end |
| 20 | +function CuTensor{T}(::UndefInitializer, V::TensorSpace{S}) where {T,S} |
| 21 | + return CuTensorMap{T}(undef, V ← one(V)) |
| 22 | +end |
| 23 | +# constructor starting from block data |
| 24 | +""" |
| 25 | + CuTensorMap(data::AbstractDict{<:Sector,<:CuMatrix}, codomain::ProductSpace{S,N₁}, |
| 26 | + domain::ProductSpace{S,N₂}) where {S<:ElementarySpace,N₁,N₂} |
| 27 | + CuTensorMap(data, codomain ← domain) |
| 28 | + CuTensorMap(data, domain → codomain) |
| 29 | +
|
| 30 | +Construct a `CuTensorMap` by explicitly specifying its block data. |
| 31 | +
|
| 32 | +## Arguments |
| 33 | +- `data::AbstractDict{<:Sector,<:CuMatrix}`: dictionary containing the block data for |
| 34 | + each coupled sector `c` as a matrix of size `(blockdim(codomain, c), blockdim(domain, c))`. |
| 35 | +- `codomain::ProductSpace{S,N₁}`: the codomain as a `ProductSpace` of `N₁` spaces of type |
| 36 | + `S<:ElementarySpace`. |
| 37 | +- `domain::ProductSpace{S,N₂}`: the domain as a `ProductSpace` of `N₂` spaces of type |
| 38 | + `S<:ElementarySpace`. |
| 39 | +
|
| 40 | +Alternatively, the domain and codomain can be specified by passing a [`HomSpace`](@ref) |
| 41 | +using the syntax `codomain ← domain` or `domain → codomain`. |
| 42 | +""" |
| 43 | +function CuTensorMap(data::AbstractDict{<:Sector,<:CuArray}, |
| 44 | + V::TensorMapSpace{S,N₁,N₂}) where {S,N₁,N₂} |
| 45 | + T = eltype(valtype(data)) |
| 46 | + t = CuTensorMap{T}(undef, V) |
| 47 | + for (c, b) in blocks(t) |
| 48 | + haskey(data, c) || throw(SectorMismatch("no data for block sector $c")) |
| 49 | + datac = data[c] |
| 50 | + size(datac) == size(b) || |
| 51 | + throw(DimensionMismatch("wrong size of block for sector $c")) |
| 52 | + copy!(b, datac) |
| 53 | + end |
| 54 | + for (c, b) in data |
| 55 | + c ∈ blocksectors(t) || isempty(b) || |
| 56 | + throw(SectorMismatch("data for block sector $c not expected")) |
| 57 | + end |
| 58 | + return t |
| 59 | +end |
| 60 | + |
| 61 | +for (fname, felt) in ((:zeros, :zero), (:ones, :one)) |
| 62 | + @eval begin |
| 63 | + function CUDA.$fname(codomain::TensorSpace{S}, |
| 64 | + domain::TensorSpace{S}=one(codomain)) where {S<:IndexSpace} |
| 65 | + return CUDA.$fname(codomain ← domain) |
| 66 | + end |
| 67 | + function CUDA.$fname(::Type{T}, codomain::TensorSpace{S}, |
| 68 | + domain::TensorSpace{S}=one(codomain)) where {T,S<:IndexSpace} |
| 69 | + return CUDA.$fname(T, codomain ← domain) |
| 70 | + end |
| 71 | + CUDA.$fname(V::TensorMapSpace) = CUDA.$fname(Float64, V) |
| 72 | + function CUDA.$fname(::Type{T}, V::TensorMapSpace) where {T} |
| 73 | + t = CuTensorMap{T}(undef, V) |
| 74 | + fill!(t, $felt(T)) |
| 75 | + return t |
| 76 | + end |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +for randfun in (:rand, :randn) |
| 81 | + randfun! = Symbol(randfun, :!) |
| 82 | + @eval begin |
| 83 | + # converting `codomain` and `domain` into `HomSpace` |
| 84 | + function CUDA.$randfun(codomain::TensorSpace{S}, |
| 85 | + domain::TensorSpace{S}) where {S<:IndexSpace} |
| 86 | + return CUDA.$randfun(codomain ← domain) |
| 87 | + end |
| 88 | + function CUDA.$randfun(::Type{T}, codomain::TensorSpace{S}, |
| 89 | + domain::TensorSpace{S}) where {T,S<:IndexSpace} |
| 90 | + return CUDA.$randfun(T, codomain ← domain) |
| 91 | + end |
| 92 | + function CUDA.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 93 | + codomain::TensorSpace{S}, |
| 94 | + domain::TensorSpace{S}) where {T,S<:IndexSpace} |
| 95 | + return CUDA.$randfun(rng, T, codomain ← domain) |
| 96 | + end |
| 97 | + |
| 98 | + # accepting single `TensorSpace` |
| 99 | + CUDA.$randfun(codomain::TensorSpace) = CUDA.$randfun(codomain ← one(codomain)) |
| 100 | + function CUDA.$randfun(::Type{T}, codomain::TensorSpace) where {T} |
| 101 | + return CUDA.$randfun(T, codomain ← one(codomain)) |
| 102 | + end |
| 103 | + function CUDA.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 104 | + codomain::TensorSpace) where {T} |
| 105 | + return CUDA.$randfun(rng, T, codomain ← one(domain)) |
| 106 | + end |
| 107 | + |
| 108 | + # filling in default eltype |
| 109 | + CUDA.$randfun(V::TensorMapSpace) = CUDA.$randfun(Float64, V) |
| 110 | + function CUDA.$randfun(rng::Random.AbstractRNG, V::TensorMapSpace) |
| 111 | + return CUDA.$randfun(rng, Float64, V) |
| 112 | + end |
| 113 | + |
| 114 | + # filling in default rng |
| 115 | + function CUDA.$randfun(::Type{T}, V::TensorMapSpace) where {T} |
| 116 | + return CUDA.$randfun(Random.default_rng(), T, V) |
| 117 | + end |
| 118 | + |
| 119 | + # implementation |
| 120 | + function CUDA.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 121 | + V::TensorMapSpace) where {T} |
| 122 | + t = CuTensorMap{T}(undef, V) |
| 123 | + CUDA.$randfun!(rng, t) |
| 124 | + return t |
| 125 | + end |
| 126 | + end |
| 127 | +end |
| 128 | + |
| 129 | +# converters |
| 130 | +# ---------- |
| 131 | +function Base.convert(::Type{CuTensorMap}, d::Dict{Symbol,Any}) |
| 132 | + try |
| 133 | + codomain = eval(Meta.parse(d[:codomain])) |
| 134 | + domain = eval(Meta.parse(d[:domain])) |
| 135 | + data = SectorDict(eval(Meta.parse(c)) => CuArray(b) for (c, b) in d[:data]) |
| 136 | + return TensorMap(data, codomain, domain) |
| 137 | + catch e # sector unknown in TensorKit.jl; user-defined, hopefully accessible in Main |
| 138 | + codomain = Base.eval(Main, Meta.parse(d[:codomain])) |
| 139 | + domain = Base.eval(Main, Meta.parse(d[:domain])) |
| 140 | + data = SectorDict(Base.eval(Main, Meta.parse(c)) => CuArray(b) |
| 141 | + for (c, b) in d[:data]) |
| 142 | + return TensorMap(data, codomain, domain) |
| 143 | + end |
| 144 | +end |
| 145 | + |
0 commit comments