After seeing a couple tutorials on the internet on Julia parallelism I decided to implement a small parallel snippet for computing the harmonic series.
The serial co
You're distributed version should be
function harmonic_distr2(n::Int64)
x = @distributed (+) for i in n:-1:1
1/i # no x assignment here
end
x
end
The @distributed
loop will accumulate values of 1/i
on every worker an then finally on the master process.
Note that it is also generally better to use BenchmarkTools's @btime
macro instead of @time
for benchmarking:
julia> using Distributed; addprocs(4);
julia> @btime harmonic(1_000_000_000); # serial
1.601 s (1 allocation: 16 bytes)
julia> @btime harmonic_distr2(1_000_000_000); # parallel
754.058 ms (399 allocations: 36.63 KiB)
julia> @btime harmonic_distr(1_000_000_000); # your old parallel version
4.289 s (411 allocations: 37.13 KiB)
The parallel version is, of course, slower if run only on one process:
julia> rmprocs(workers())
Task (done) @0x0000000006fb73d0
julia> nprocs()
1
julia> @btime harmonic_distr2(1_000_000_000); # (not really) parallel
1.879 s (34 allocations: 2.00 KiB)