|
2 | 2 | struct SparseBlockTensorMap{TT<:AbstractTensorMap{E,S,N₁,N₂}} <: AbstractBlockTensorMap{E,S,N₁,N₂} |
3 | 3 |
|
4 | 4 | Sparse `SparseBlockTensorMap` type that stores tensors of type `TT` in a sparse dictionary. |
5 | | -Temporaries of TensorOperations keep their `allocator` (otherwise `nothing`), from which their |
6 | | -blocks are obtained and to which they are released. |
| 5 | +Temporaries of TensorOperations are marked by `istemp`, as their blocks are obtained from and |
| 6 | +released to the allocator of the operation. |
7 | 7 | """ |
8 | 8 | struct SparseBlockTensorMap{TT <: AbstractTensorMap, E, S, N₁, N₂, N} <: |
9 | 9 | AbstractBlockTensorMap{E, S, N₁, N₂} |
10 | 10 | data::Dict{CartesianIndex{N}, TT} |
11 | 11 | space::TensorMapSumSpace{S, N₁, N₂} |
12 | | - allocator::Any |
| 12 | + istemp::Bool |
13 | 13 |
|
14 | 14 | # uninitialized constructor |
15 | 15 | function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}( |
16 | | - ::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}; allocator = nothing |
| 16 | + ::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}; istemp::Bool = false |
17 | 17 | ) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}} |
18 | 18 | @assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions" |
19 | 19 | data = Dict{CartesianIndex{N}, TT}() |
20 | | - return new{TT, E, S, N₁, N₂, N}(data, space, allocator) |
| 20 | + return new{TT, E, S, N₁, N₂, N}(data, space, istemp) |
21 | 21 | end |
22 | 22 |
|
23 | 23 | # constructor from data |
24 | 24 | function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}( |
25 | 25 | data::Dict{CartesianIndex{N}, TT}, space::TensorMapSumSpace{S, N₁, N₂} |
26 | 26 | ) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}} |
27 | 27 | @assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions" |
28 | | - return new{TT, E, S, N₁, N₂, N}(data, space, nothing) |
| 28 | + return new{TT, E, S, N₁, N₂, N}(data, space, false) |
29 | 29 | end |
30 | 30 | end |
31 | 31 |
|
@@ -226,15 +226,17 @@ for f in (:(Random.rand!), :(Random.randn!), :(Random.randexp!), :(TK.randisomet |
226 | 226 | invoke($f, Tuple{Random.AbstractRNG, AbstractTensorMap}, rng, storeentries!(t)) |
227 | 227 | end |
228 | 228 |
|
229 | | -# blocks of temporaries are obtained from (and released to) their own allocator, those of |
230 | | -# other destinations from the allocator of the operation, if any |
| 229 | +# blocks of temporaries are obtained from (and released to) the allocator of the operation, |
| 230 | +# so kernels without allocator cannot add them |
231 | 231 | istemp(t::AbstractBlockTensorMap) = false |
232 | | -istemp(t::SparseBlockTensorMap) = !isnothing(t.allocator) |
| 232 | +istemp(t::SparseBlockTensorMap) = t.istemp |
233 | 233 |
|
234 | | -function newblock(t::AbstractBlockTensorMap, I::CartesianIndex, allocator = TO.DefaultAllocator()) |
| 234 | +function newblock(t::AbstractBlockTensorMap, I::CartesianIndex, allocator = nothing) |
235 | 235 | V = eachspace(t)[I] |
236 | | - istemp(t) && return TO.tensoralloc(eltype(t), V, Val(true), t.allocator) |
237 | | - return TO.tensoralloc(eltype(t), V, Val(false), allocator) |
| 236 | + istemp(t) || return TO.tensoralloc(eltype(t), V, Val(false), something(allocator, TO.DefaultAllocator())) |
| 237 | + isnothing(allocator) && |
| 238 | + throw(ArgumentError("cannot add block $I to a temporary without its allocator")) |
| 239 | + return TO.tensoralloc(eltype(t), V, Val(true), allocator) |
238 | 240 | end |
239 | 241 |
|
240 | 242 | @propagate_inbounds function getindex!(t::SparseBlockTensorMap, I::CartesianIndex{N}) where {N} |
|
0 commit comments