在互联网上看了几个关于 Julia 并行性的教程后,我决定实现一个小的并行片段来计算谐波级数。
序列号为:
harmonic = function (n::Int64)
x = 0
for i in n:-1:1 # summing backwards to avoid rounding errors
x +=1/i
end
x
end
Run Code Online (Sandbox Code Playgroud)
我制作了 2 个并行版本,一个使用@distributed宏,另一个使用@everywhere宏(julia -p 2顺便说一句):
@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
Run Code Online (Sandbox Code Playgroud)
但是,当我运行上面的代码和@time它时,我没有得到任何加速 - 事实上,该@distributed版本运行速度明显变慢! …