I wrote the n-body simulation in Julia, but I’m experiencing unexpectedly high memory allocation (according to the @time
macro) when running the simulation for 10.000 bodies (the input file I’m using test1e4.julia.txt · GitHub). I tried all the performance recommendations, I replaced array accesses with views, using column-major iterating, but nothing seems to work.
Here is the code:
using DelimitedFiles
const G = 6.67e-11
const dt = 0.1
function main()
data = readdlm("../data/test1e4.julia.txt")
n = size(data, 1)
positions::Array{Float64, 2} = data[:, 1:3]'
velocities::Array{Float64, 2} = data[:, 4:6]'
masses::Array{Float64, 1} = data[:, 7]
println("Done reading.")
forces::Array{Float64, 2} = zeros(3, n)
# sum_forces = @time @sync @distributed (+) for q = 1:n
@inbounds for q = 1:n
position_q_view = view(positions, :, q)
force_q_view = view(forces, :, q)
for k = 1:n
if k <= q
continue
end
force_k_view = view(forces, :, k)
diff::Array{Float64} = position_q_view - view(positions, :, k)
dist::Float64 = sqrt(sum(diff .^ 2))
dist_cubed::Float64 = dist ^ 3
tmp::Float64 = -G * masses[q] * masses[k] / dist_cubed
force_qk::Array{Float64} = tmp * diff
force_q_view = force_q_view + force_qk
force_k_view = force_k_view - force_qk
end
end
@inbounds for q = 1:n
@views positions[:, q] += (dt * velocities[:, q])
@views velocities[:, q] += (dt / masses[q] * forces[:, q])
end
end
Here are the @time
runs:
julia> @time main()
Done reading.
42.568147 seconds (405.27 M allocations: 32.289 GiB, 5.73% gc time)
julia> @time main()
Done reading.
42.548842 seconds (400.27 M allocations: 32.049 GiB, 6.01% gc time)
I also ran it with --track-allocations=user
and these are the results:
julia> analyze_malloc(".")
14-element Array{Coverage.MallocInfo,1}:
Coverage.MallocInfo(0, "./main.jl.mem", 21)
Coverage.MallocInfo(0, "./main.jl.mem", 37)
Coverage.MallocInfo(1312, "./main.jl.mem", 8)
Coverage.MallocInfo(10816, "./main.jl.mem", 13)
Coverage.MallocInfo(240080, "./main.jl.mem", 17)
Coverage.MallocInfo(480256, "./main.jl.mem", 10)
Coverage.MallocInfo(960000, "./main.jl.mem", 20)
Coverage.MallocInfo(960000, "./main.jl.mem", 38)
Coverage.MallocInfo(2740389, "./main.jl.mem", 11)
Coverage.MallocInfo(5440000, "./main.jl.mem", 39)
Coverage.MallocInfo(16222886, "./main.jl.mem", 9)
Coverage.MallocInfo(5599440000, "./main.jl.mem", 33)
Coverage.MallocInfo(10398960000, "./main.jl.mem", 26)
Coverage.MallocInfo(18398160000, "./main.jl.mem", 32)
I’m quite new to Julia, so there might be overlooking a basic performance issues but I cant seem to narrow it down. Thank you for your help.