Skip to content

Commit e3b2462

Browse files
authored
fix number of chunks (#53413)
The manual claims that `a` is split into `nthreads()` chunks, but this is not true in general. As it was you could get an error, if `length(a) < nthreads()`, or a number of chunks larger than `nthreads()`, if `nthreads()` is smaller than `length(a)` but does not divide it. With `cld`, on the other hand, you always get at most `nthreads()` chunks.
1 parent 8bf6a07 commit e3b2462

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

doc/src/manual/multi-threading.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ julia> sum_multi_bad(1:1_000_000)
227227
Note that the result is not `500000500000` as it should be, and will most likely change each evaluation.
228228

229229
To fix this, buffers that are specific to the task may be used to segment the sum into chunks that are race-free.
230-
Here `sum_single` is reused, with its own internal buffer `s`. The input vector `a` is split into `nthreads()`
230+
Here `sum_single` is reused, with its own internal buffer `s`. The input vector `a` is split into at most `nthreads()`
231231
chunks for parallel work. We then use `Threads.@spawn` to create tasks that individually sum each chunk. Finally, we sum the results from each task using `sum_single` again:
232232
```julia-repl
233233
julia> function sum_multi_good(a)
234-
chunks = Iterators.partition(a, length(a) ÷ Threads.nthreads())
234+
chunks = Iterators.partition(a, cld(length(a), Threads.nthreads()))
235235
tasks = map(chunks) do chunk
236236
Threads.@spawn sum_single(chunk)
237237
end

0 commit comments

Comments
 (0)