【发布时间】:2018-10-22 19:06:30
【问题描述】:
在网上看了几个关于 Julia 并行的教程后,我决定实现一个小的并行 sn-p 来计算谐波级数。
序列号为:
harmonic = function (n::Int64)
x = 0
for i in n:-1:1 # summing backwards to avoid rounding errors
x +=1/i
end
x
end
我制作了 2 个并行版本,一个使用 @distributed 宏,另一个使用 @everywhere 宏 (julia -p 2 btw):
@everywhere harmonic_ever = function (n::Int64)
x = 0
for i in n:-1:1
x +=1/i
end
x
end
harmonic_distr = function (n::Int64)
x = @distributed (+) for i in n:-1:1
x = 1/i
end
x
end
但是,当我运行上面的代码和@time 时,我没有得到任何加速 - 事实上,@distributed 版本的运行速度要慢得多!
@time harmonic(10^10)
>>> 53.960678 seconds (29.10 k allocations: 1.553 MiB) 23.60306659488827
job = @spawn harmonic_ever(10^10)
@time fetch(job)
>>> 46.729251 seconds (309.01 k allocations: 15.737 MiB) 23.60306659488827
@time harmonic_distr(10^10)
>>> 143.105701 seconds (1.25 M allocations: 63.564 MiB, 0.04% gc time) 23.603066594889185
让我完全困惑的是“0.04% gc time”。我显然遗漏了一些东西,而且我看到的示例不是针对 1.0.1 版本的(例如使用了@parallel)。
【问题讨论】: