June 2019
Intermediate to advanced
218 pages
5h 19m
English
We've shown many ways to sum numbers over the course of this book. Let's now try to sum an array of numbers across many threads, hoping to improve performance compared to doing it sequentially on a single thread.
Our first attempt, in the following code, simply tries to run the loop on multiple threads:
function sum_thread_base(x) r = zero(eltype(x)) @threads for i in eachindex(x) @inbounds r += x[i] end return rend
We can then compare this function against Julia's built-in sum as follows:
julia> a=rand(10_000_000);julia> @btime sum($a) 6.746 ms (0 allocations: 0 bytes)5.000462435093071e6julia> @btime sum_thread_base($a) 1.566 s (5506561 allocations: 84.01 MiB)1.250442324066673e6
The results may be surprising at first glance. Not ...
Read now
Unlock full access