Skip to content

Commit a34df99

Browse files
committed
implement foreachblock
1 parent daf2f53 commit a34df99

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/tensors/blockiterator.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,28 @@ Base.IteratorEltype(::BlockIterator) = Base.HasEltype()
1313
Base.eltype(::Type{<:BlockIterator{T}}) where {T} = blocktype(T)
1414
Base.length(iter::BlockIterator) = length(iter.structure)
1515
Base.isdone(iter::BlockIterator, state...) = Base.isdone(iter.structure, state...)
16+
17+
# TODO: fast-path when structures are the same?
18+
# TODO: do we want f(c, bs...) or f(c, bs)?
19+
# TODO: implement scheduler
20+
# TODO: do we prefer `blocks(t, ts...)` instead or as well?
21+
"""
22+
foreachblock(f, t::AbstractTensorMap, ts::AbstractTensorMap...; [scheduler])
23+
24+
Apply `f` to each block of `t` and the corresponding blocks of `ts`.
25+
Optionally, `scheduler` can be used to parallelize the computation.
26+
This function is equivalent to the following loop:
27+
28+
```julia
29+
for (c, b) in blocks(t)
30+
bs = (b, block.(ts, c)...)
31+
f(c, bs)
32+
end
33+
```
34+
"""
35+
function foreachblock(f, t::AbstractTensorMap, ts::AbstractTensorMap...; scheduler=nothing)
36+
foreach(blocks(t)) do (c, b)
37+
return f(c, (b, block.(ts, c)...))
38+
end
39+
return nothing
40+
end

0 commit comments

Comments
 (0)