This project is a simulation of particles moving in a 2D space under the influence of gravitational forces. The simulation is implemented in three versions:
- Serial implementation (C/C++)
- Parallel implementation using OpenMP
- Parallel implementation using MPI (not yet implemented in the assignment but could be extended)
The goal is to gain hands-on experience with parallel programming on shared-memory systems (OpenMP) and compare performance against the sequential version.
The simulation follows an N-body problem approach but optimizes calculations using the particle-in-cell (PIC) method. The main considerations are:
- Particles interact with others in their own grid cell and adjacent cells.
- Collisions occur when two particles come within a distance of ε = 0.005.
- The simulation space wraps around at its boundaries.
- Motion follows Newtonian mechanics with a fixed time-step of Δ = 0.1.
The program expects five command-line arguments:
- Seed (integer) - Initializes the random number generator.
- Space size (double) - Side length of the square simulation area.
- Grid size (integer) - Number of cells along each dimension.
- Number of particles (integer) - Total particles in the simulation.
- Time-steps (integer) - Number of simulation iterations.
The program prints exactly two lines:
- Final x and y coordinates of particle 0, formatted to three decimal places.
- The number of particles that collided during the simulation.
Example:
$ ./parsim 1 2 3 10 1
1.570 0.056
0Execution time is measured using omp_get_wtime() and printed to stderr. The result is printed separately to stdout.
To compile the serial version:
gcc -o parsim parsim.c -fopenmpTo compile the OpenMP version:
gcc -o parsim-omp parsim-omp.c -fopenmpTo run the serial version:
./parsim <seed> <space_size> <grid_size> <num_particles> <time_steps>To run the OpenMP version (with thread count specified by environment variable):
export OMP_NUM_THREADS=4
./parsim-omp <seed> <space_size> <grid_size> <num_particles> <time_steps>There's a debug mode that is activated if the flag -d is passed as an argument. This mode prints the grid and particles at each time step.
To run the serial version in debug mode:
./parsim -v <seed> <space_size> <grid_size> <num_particles> <time_steps> -dTo build the report, run the following command:
cd report
pdflatex report.texSimão Sanguinho - 102082 Pedro Ribeiro - 102663 José Pereira - 103252 Parallel and Distributed Computing - 2024/2025