Skip to content

Commit 86d33b5

Browse files
leburgelclaude
andauthored
Use a plain loop in taskforeach for a single worker (#627)
With one worker, `taskforeach` still went through the atomic counter that balances the load between tasks, at the cost of an atomic increment per item. A single worker now uses `foreach` directly. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 6a009ba commit 86d33b5

2 files changed

Lines changed: 3 additions & 0 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4141

4242
- `twist!` and `twist` on a `TensorMap`, a `DiagonalTensorMap` or their adjoints use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor, diagonal or not, into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
4343
- `foreachblock` with several tensors is type stable, which removes the dynamic dispatch on every call to it and to the factorizations that go through it
44+
- `taskforeach` with a single worker is a plain loop without an atomic counter, which lowers the cost per subblock of single-threaded index manipulations and planar traces
4445

4546
## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30
4647

‎src/auxiliary/auxiliary.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Apply `f(item)` (respectively `f(item, resource)`) to all elements of `items`, d
102102
The calling thread acts as the first of the workers, so at most `ntasks - 1` tasks are spawned, and none at all for a single worker.
103103
"""
104104
function taskforeach(f, items, ntasks::Integer)
105+
ntasks == 1 && return foreach(f, items)
105106
items′ = items isa AbstractArray ? items : collect(items)
106107
n = length(items′)
107108
nworkers = min(ntasks, n)
@@ -116,6 +117,7 @@ function taskforeach(f, items, ntasks::Integer)
116117
return nothing
117118
end
118119
function taskforeach(f, items, resources)
120+
length(resources) == 1 && return foreach(Base.Fix2(f, only(resources)), items)
119121
items′ = items isa AbstractArray ? items : collect(items)
120122
n = length(items′)
121123
nworkers = min(length(resources), n)

0 commit comments

Comments
 (0)