1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 | using PGFPlots # for plotting in show_babel_KNL()
"""
This is an implementation of the NBabel N-body problem.
See nbabel.org for more information.
This is 'naive & native' Julia with type and loop annotations for
fastmath and vectorization, because this is really is no effort.
Without annotations the code will be as slow as naive Python/IDL.
include("nbabel.jl")
NBabel("IC/input128", show=true)
Julius Donnert INAF-IRA 2017
"""
const tbeg = 0.0
const tend = 1.0
const dt = 0.001
function NBabel(fname::String; show=false)
if show
println("Reading file : $fname")
end
ID, mass, pos, vel = read_ICs(fname)
acc = similar(vel)
acc = compute_acceleration(pos, mass, acc)
last_acc = copy(acc)
Ekin, Epot = compute_energy(pos, vel, mass)
Etot_ICs = Ekin + Epot
t = 0.0
nstep = 0
while t < tend
pos = update_positions(pos, vel, acc, dt)
last_acc[:,:] = acc[:,:]
acc = compute_acceleration(pos, mass, acc)
vel = update_velocities(vel, acc, last_acc, dt)
t += dt
nstep += 1
if show && nstep%10 == 0
Ekin, Epot = compute_energy(pos, vel, mass)
Etot = Ekin + Epot
dE = (Etot - Etot_ICs)/Etot_ICs
@printf "t = %g, Etot=%g, Ekin=%g, Epot=%g, dE=%g \n" t Etot Ekin Epot dE
end
end
return size(pos,1)
end
function update_positions(pos::Array{Float64,2}, vel::Array{Float64,2},
acc::Array{Float64,2}, dt::Float64)
@fastmath @inbounds @simd for i=1:size(pos,1)
pos[i,1] += dt * vel[i,1] + 0.5 * dt^2 * acc[i,1]
pos[i,2] += dt * vel[i,2] + 0.5 * dt^2 * acc[i,2]
pos[i,3] += dt * vel[i,3] + 0.5 * dt^2 * acc[i,3]
end
return pos
end
function update_velocities(vel::Array{Float64,2}, acc::Array{Float64,2},
last_acc::Array{Float64,2}, dt::Float64)
@fastmath @inbounds @simd for i=1:size(vel,1)
vel[i,1] += 0.5 * dt * (acc[i,1] + last_acc[i,1])
vel[i,2] += 0.5 * dt * (acc[i,2] + last_acc[i,2])
vel[i,3] += 0.5 * dt * (acc[i,3] + last_acc[i,3])
end
return vel
end
"""
Force calculation.
"""
function compute_acceleration(pos::Array{Float64,2}, mass::Array{Float64,1},
acc::Array{Float64,2})
N = size(pos,1)
acc[:,:] = 0
pos_i = [0.0,0,0]
acc_i = [0.0,0,0]
@inbounds for i = 1:N
pos_i[:] = pos[i,:]
acc_i[:] = 0
@fastmath @inbounds @simd for j = 1:N
if i != j
dx = pos_i[1] - pos[j,1]
dy = pos_i[2] - pos[j,2]
dz = pos_i[3] - pos[j,3]
rinv3 = 1/sqrt((dx^2 + dy^2 + dz^2)^3)
acc_i[1] -= mass[j] * rinv3 * dx
acc_i[2] -= mass[j] * rinv3 * dy
acc_i[3] -= mass[j] * rinv3 * dz
end
end
acc[i,:] = acc_i[:]
end
return acc
end
"""
Kinetic and potential energy.
"""
function compute_energy(pos::Array{Float64,2}, vel::Array{Float64,2}, mass::Array{Float64})
N = size(vel,1)
Ekin = 0
@simd for i = 1:N
@inbounds Ekin += 0.5 * mass[i] * (vel[i,1]^2 + vel[i,2]^2 + vel[i,3]^2)
end
Epot = 0.0
@inbounds for i = 1:N-1
@fastmath @inbounds for j = i+1:N
dx = pos[i,1] - pos[j,1]
dy = pos[i,2] - pos[j,2]
dz = pos[i,3] - pos[j,3]
rinv = 1/sqrt(dx^2 + dy^2 + dz^2)
Epot -= mass[i] * mass[j] * rinv
end
end
return Ekin, Epot
end
function read_ICs(fname::String)
ICs = readdlm(fname)
N = size(ICs,1)
id = Array{Float64}(N)
mass = Array{Float64}(N)
pos = Array{Float64}(N,3)
vel = Array{Float64}(N,3)
id[:] = ICs[:,1]
mass[:] = ICs[:,2]
pos[:,1] = ICs[:,3]
pos[:,2] = ICs[:,4]
pos[:,3] = ICs[:,5]
vel[:,1] = ICs[:,6]
vel[:,2] = ICs[:,7]
vel[:,3] = ICs[:,8]
return id, mass, pos, vel
end
"""
Assume all the IC files from the website are in ./IC/ and run them
"""
function test_babel()
const files = ["16","32","64","128","256","512","1k","2k","4k","8k","16k"]
const npart = [16.0, 32, 64, 128, 256 , 512, 1000, 2000, 4000,8000,16000]
path = "./IC/"
nFiles = length(files)
runtime = Array{Float64}(nFiles)
NBabel(path*"input"*files[1]) # run twice to precompile
NBabel(path*"input"*files[1])
for i = 1:nFiles
print("$(files[i]) : ")
tic()
NBabel(path*"input"*files[i], show=false)
runtime[i] = toc()
end
end
function show_babel_KNL()
const julia_KNL = [0.006,0.019,0.04 ,0.166,0.537,2.027,8.395,33.14,129.69,527.0912,2200]
const F90_KNL = [0.001,0.004,0.016,0.068,0.276,1.028,3.46,13.58,53.088,213.576,902.372]
const F90vec_KNL = [0.001,0.004,0.024,0.1,0.372,1.41,5.024,21.29,87.1,412.45,1676.1]
const files = ["16","32","64","128","256","512","1k","2k","4k","8k","16k"]
const npart = [16.0, 32, 64, 128, 256 , 512, 1000, 2000, 4000,8000,16000]
l1 = PGFPlots.Plots.Linear(npart, julia, style="Set11, solid", mark="square",
markSize=0.5, legendentry="Julia, 1 thread")
l2 = PGFPlots.Plots.Linear(npart, F90, style="Set12, solid", mark="*",
markSize=0.5, legendentry="Fortran 90, scalar")
l3 = PGFPlots.Plots.Linear(npart, F90vec, style="Set13, solid", mark="diamond",
markSize=0.5, legendentry="Fortran 90, vectorized")
lnq = PGFPlots.Plots.Linear(npart, npart.^2/4e4, style="gray, dashed", mark="none",
legendentry=L"t \propto n_{\rm part}^2")
ax = PGFPlots.Axis([l1,l2,l3,lnq], xlabel="Number of Particles", ylabel="Runtime [s]",
xmin=16, xmax=2e4, ymin=0.001, ymax=2^14, title=L"N$^2$ Gravity, KNL",
ymode="log", xmode="log", legendPos="south east")
PGFPlots.save("./nbabel_scaling_KNL.pdf", ax)
end
|