Skip to content

Commit

Permalink
Update doc/src/manual/multi-threading.md
Browse files Browse the repository at this point in the history
Co-authored-by: Mason Protter <mason.protter@icloud.com>
  • Loading branch information
IanButterworth and MasonProtter authored Jun 16, 2023
1 parent 2637437 commit 1d330f7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions doc/src/manual/multi-threading.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,19 @@ To fix this, buffers that are specific to the task may be used to segment the su

```julia-repl
julia> function sum_multi_good(a)
buffers = WeakKeyDict{Task,Int}()
Threads.@threads for i in a
b = get(buffers, current_task(), 0)
b += i
buffers[current_task()] = b
chunks = Iterators.partition(a, length(a) ÷ Threads.nthreads())
tasks = map(chunks) do chunk
Threads.@spawn begin
b = zero(eltype(chunk))
for i ∈ chunk
b += i
end
return b
end
end
s = 0
for b in values(buffers)
buffers = fetch.(tasks)
for b in buffers
s += b
end
s
Expand Down

0 comments on commit 1d330f7

Please sign in to comment.