nbody_brute_force.c
)
computes, for each time step, the force applied to each particle,
and computes its new position.
p1
is computed by
summing the force that all the other particles apply to p1
.
for(i=0; i < nparticles; i++) { compute_force(p1, p[i]->x_pos, p[i]->y_pos, p[i]->mass); }
for(i=0; i < nparticles; i++) { move_particle(&particles[i], step); }
nbody_barnes_hut.c
)
uses approximations during the phase that computes the forces
applied to a particle. The main idea of this algorithm is to group
particles that are close to each other into one big "virtual" particle.
When computing the forces applied to a particle p1
,
if a group of particles is far enough from p1
, the
cumulated force of the particles is approximated by the force of
the virtual particle whose position is at the barycenter of the
particles and whose mass is the sum of the particles mass.
p1
is done recursively starting from the
root of the tree. For each node n
,
the following algorithm is applied:
n
is a leaf, the force of n
's particle (if any) on p1
is computed size
) and the distance between the barycentre and p1
(distance
) is computed.size/distance < θ
, the force of a virtual particle against
p1
is computed. The virtual particle mass is the sum of the mass of
the particles in the node, and its position is the barycentre of these particles.n
θ = 2
.
nbody_brute_force.c
: source code of the naive algorithmnbody_barnes_hut.c
: source code of the Barnes-Hut algorithmnbody.h
: definition of types and macrosnbody_alloc.{c,h}
: memory allocatornbody_tool.{c,h}
: various tools for handling particlesui.{c,h}
: tools for displaying particlesxtools.{c,h}
: tools for displaying particlesMakefile
: for configuring and building the applications
N
particles for a fixed duration T
. Both N
and
T
can be specify at runtime as first and second arguments of command-line binary.
Here are two examples for brute force and Barnes Hut versions:
$ ./nbody_brute_force 1000 2 ----------------------------- nparticles: 1000 T_FINAL: 2.000000 ----------------------------- Simulation took 6.344124 s to complete $ ./nbody_barnes_hut 1000 2 ----------------------------- nparticles: 1000 T_FINAL: 2.000000 ----------------------------- Simulation took 0.583025 s to complete
# DISPLAY =
line in the Makefile
.
particles.log
. To enable this
feature, uncomment the #DUMP = -DDUMP_RESULT
line in the
Makefile
. This feature is mandatory to check if your parallel version is correct (by comparing the sequential dump and the parallel one).
The main source of parallelism will be based on the processing of each particle. But the way to make the code parallel will be different from one version to another (brute force and Barnes Hut).