CPSC 521 assignment 1
Assignment description
This program performs an n-body physics simulation with one body per process. The code is parallelized with MPI, using only basic MPI functions like MPI_Send
and MPI_Recv
.
Assignment tweaks
This assignment has several macros that can be changed at the top of the source code to alter its behaviour. Some of them are:
SCALAR
andSCALAR_MPI_TYPE
can be set todouble
andMPI_DOUBLE
(the default), or tofloat
andMPI_FLOAT
to use single-precision floating point.RING_COMM
switches between ring communication and broadcast-style communication. You can also control which algorithm is used more finely withRING_COMM_INIT
,RING_COMM_SIMULATE
, andRING_COMM_TERMINATE
VISUALIZE_OUTPUT
generates periodic reports of the positions of all particles, in a format that can be interpreted by the nbody visualization program (see assignment 4).DEBUG_OUTPUT
is as it sounds, it enables lots of debugging output per iteration.
There is also SERIALIZE_VELOCITIES
, if you want the program to have larger messages being sent.
It turns out that the ring communication is much more efficient than broadcast communication: in a typical case of 8 processes and 8 bodies and 1000000 rounds, ring takes 4.799 and broadcast takes 9.081 seconds. Clearly, using a ring instead of my initial first-intuition scheme is a better idea.
Using floats is actually slower than doubles (doubles take 4.799 and floats take 5.262 seconds in the same case as above) on the given hardware setup. This is only based on one test case and results may vary for different scenarios.
Finally: both this assignment and the next calculate velocities and positions alternately, staggering them so that the new values can be used (i.e. backward Euler, not forward). This results in a system that should conserve energy better.
Downloads
- nbody.c as HTML: web page with source code for assignment 1
- assign1.tar.gz (282 KB): Assignment 1, a simple n-body simulation written with MPI