Random number generation with SPRNG and MPI

In this parallel simulation using SPRNG and 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 the random number generator SPRNG, uniformly distributed over the [0,1) interval. SPRNG comes with six types of random number generation types, which are selectable within the code.

This backbone code (random_sprng_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 ia about 670 megatrials per second and takes about 3 milliseconds.

$ mpirun -np 4 random_sprng.mpi 2000000
using message-passing interface (MPI) with 4 processors
initializing SPRNG random number generator:
type is: 64 bit Linear Congruential Generator with Prime Addend
seed is: 1857645418

number of tosses: 2000000
number of tails: 999239

probability: 0.4996195

execution time: 0.00298106 s
performance: 670.902 Mtrials/sec

With 2 billion tosses, the performance is on average 1280 Mega trials per second, and takes about 1.4 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_sprng.mpi random_sprng_mpi.c -I/usr/include/sprng -lsprng -fopenmp

and execute with the command line,

$ mpirun -np num_procs random_sprng.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_sprng.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.

SPRNG is open source and part of most major Linux distributions. On Debian-based systems, SPRNG can be installed with the command,

$ sudo apt-get install libsprng2-dev

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_sprng_mpi.c (C code)
  • README (documentation)
  • terms.md (terms of use)
  • license.md (license)