Hi there.
So soon I will give a talk on the Julia programming language in my research group. In that presentation, I wanted to show how well parallelization works in Julia.
So I decided to take the following as an example:
function lorenz(u, p, t)
dx = 10.0 * (u[2] - u[1])
dy = u[1] * (28.0 - u[3]) - u[2]
dz = u[1] * u[2] - (8 / 3) * u[3]
[dx, dy, dz]
end
function solve_all()
u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 15000.0);
prob = ODEProblem(lorenz, u0, tspan);
@time solve(prob, Tsit5());
return nothing
end
@time for i in 1:4
solve_all();
end
And the output gives 4 times the required amount of time:
0.773542 seconds (16.25 M allocations: 1.221 GiB, 18.02% gc time, 34.42% compilation time: 100% of which was recompilation)
0.554355 seconds (15.69 M allocations: 1.184 GiB, 21.74% gc time)
0.559194 seconds (15.69 M allocations: 1.184 GiB, 24.41% gc time)
0.548238 seconds (15.69 M allocations: 1.184 GiB, 28.70% gc time)
2.436127 seconds (63.32 M allocations: 4.773 GiB, 22.73% gc time, 10.93% compilation time: 100% of which was recompilation)
Now this seems all nice.
However, when I parallelize like this
@time Threads.@threads for i in 1:4
solve_all();
end
I get the output:
@time Threads.@threads for i in 1:4
solve_all();
end
I get the following output
0.922861 seconds (52.87 M allocations: 3.992 GiB, 55.01% gc time, 2.96% compilation time)
0.953759 seconds (55.08 M allocations: 4.158 GiB, 53.23% gc time, 2.87% compilation time)
1.309005 seconds (61.15 M allocations: 4.615 GiB, 58.99% gc time, 5.76% compilation time)
1.343746 seconds (62.75 M allocations: 4.735 GiB, 57.46% gc time)
1.369621 seconds (62.79 M allocations: 4.738 GiB, 56.38% gc time, 9.29% compilation time)
In short: each individual time the Differential equation is solved takes almost twice as long as when the loop is sequential. Plus there are also a lot more allocations and so on.
Why is this? And how can I fix it?
Thank you very much in advance!