Random number generation with MPI

In this parallel simulation using MPI, a coin is thrown many times and the number of tails is counted. The probability should be exactly 1/2.

This code is a backbone implementation of a parallelized loop using a thread-safe (reentrant) random number generator (drand48_r), uniformly distributed over the [0,1) interval, with MPI.

This backbone code (random_mpi.c) is well-documented with comments and instructions, and can be used to perform other parallel simulations that are implemented over a loop and use random numbers.

Demo

The execution time is used to define the performance, measured in Mega trials per second. In an 8 processor computer, running 4 processors and 2 millon tosses, the performance on average is about 290 megatrials per second and takes about 7 milliseconds.

$ mpirun -np 4 random_mpi.mpi 2000000
using message-passing interface (MPI) with 4 processors
processor: 1 seed: 1618269490
processor: 2 seed: 97219466
processor: 3 seed: 1799197222
processor: 0 seed: 106293346

number of tosses: 2000000
number of tails: 1002187

probability: 0.5010935

execution time: 0.00681112 s
performance: 293.638 Mtrials/sec

With 2 billion tosses, the performance is on average 400 megatrials per second, and takes about 5 seconds. The following plot, using 8 processors, shows the well-known behavior of performance with increasing problem size,

The next plot shows the performance as a function of number of processors for 2 billion tosses, in an 8 processor computer.

Compilation and execution

Compile the code with,

$ mpicc -o random_mpi.mpi random_mpi.c -I/usr/include/sprng -lsprng -fopenmp

and execute with the command line,

$ mpirun -np num_procs random_mpi.mpi num_tries

where num_procs is the number of processors and num_tries is the number of coin tosses. To suppress the output to stderr,

$ mpirun -np num_procs random_mpi.mpi num_tries 2>/dev/null

Dependencies

This software has been developed and tested using OpenMPI. OpenMPI is open source and part of most major Linux distributions. On Debian-based systems, OpenMPI can be installed with the command,

$ sudo apt-get install libopenmpi-dev

Other implementations of the MPI standard may be used, for example LAM/MPI (deprecated) or MPICH. MPICH is open source and part of most major Linux distributions. On Debian-based systems, MPICH can be installed with the command,

$ sudo apt-get install libmpich-dev

Whichever MPI implementation is used, it is essential that both the compilation of the code and execution is made with the same implementation.

Although OpenMP is not used in this simulation, OpenMP is used to get execution times via the function omp_get_wtime().

Terms of Use and License

Before buying, please read our Terms of Use and License.

List of Files

Included in this software package are the following files:

  • random_mpi.c (C code)
  • README (documentation)
  • terms.md (terms of use)
  • license.md (license)