Loughborough University
Leicestershire, UK
LE11 3TU
+44 (0)1509 222222
Loughborough University

IT Services : High Performance Computing

MPI Implementation


C++ Example

A simple listing:

#include <iostream>
#include <cstdlib>
#include <mpi.h>

using namespace std;

int main(int argc, char ** argv) {
  MPI_Status status;
  int myrank;
  int num_ranks;
  
  MPI_Init(&argc, &argv); // Starts MPI
  MPI_Comm_rank(MPI_COMM_WORLD, &myrank); // Get current process id 
  MPI_Comm_size(MPI_COMM_WORLD, &&num_ranks); // Get number of processes
  
  const unsigned int size_per_rank = 100; // number of elements per rank
  const unsigned int total_size = size_per_rank * num_ranks; // 100 per rank
  
  double * data = NULL; // For most ranks v doesn't mean anything
  
  if (myrank == 0) // Create the data on rank 0 only
    data = new double [total_size];
    
  double * local_data = new double [size_per_rank]; // Local array to each rank
  
  /* Scatter parts of global array to each rank's local array */
  MPI_Scatter(data, 
              total_size, 
              MPI_DOUBLE, 
              local_data, 
              size_per_rank, 
              MPI_DOUBLE, 
              0,
              MPI_COMM_WORLD);
  
  /* assign random numbers across all the ranks */
  for (int i = 0; i < size_per_rank; i++)
    generate(local_data, local_data + size_per_rank, drand48);
    
  /* Gather the data back to the global 'v' */
  MPI_Gather(local_data, 
             size_per_rank, 
             MPI_DOUBLE, 
             data, 
             total_size, 
             MPI_DOUBLE, 
             0, 
             MPI_COMM_WORLD);
  
  /* tidy up and quit */
  if (data != NULL)
    delete [] data;
  delete [] local_data;
  MPI_Finalize();
  
  return 0;
}