From 8759adc6095b449e4c8932d4d7bd3f45583710b4 Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Sun, 9 Jul 2023 13:13:46 +0200 Subject: [PATCH] More uniform reduce chunk sizes Compute chunk ranges so that they're more uniform, with at most one item of difference, preventing the last chunk to be too different from the others. --- src/reduce.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/reduce.jl b/src/reduce.jl index 9e1e931e..2fe3efeb 100644 --- a/src/reduce.jl +++ b/src/reduce.jl @@ -181,14 +181,12 @@ function _reduce_threads_for(rf, init, reducible::SizedReducible{<:AbstractArray if nthreads == 1 return foldl_basecase(rf, start(rf, init), arr) else - w = length(arr) ÷ nthreads + mylen = length(arr) results = Vector{Any}(undef, nthreads) Threads.@threads for i in 1:nthreads - if i == nthreads - chunk = @view arr[(i - 1) * w + 1:end] - else - chunk = @view arr[(i - 1) * w + 1:i * w] - end + rangeini = ((i - 1) * mylen) ÷ nthreads + 1 + rangeend = (i * mylen) ÷ nthreads + chunk = @view arr[rangeini, rangeend] results[i] = foldl_basecase(rf, start(rf, init), chunk) end # It can be done in `log2(n)` for loops but it's not clear if