MPI-Tutorial Repository

Resources:

Code Examples

Some code examples to get started.

  • C++
#include  <iostream>
#include  <mpi.h>
int  main(int  argc, char**  argv) {
   // Initialisation
   MPI_Init(&argc, &argv);

   // Reading size and rank
   int size, rank;
   MPI_Comm_size(MPI_COMM_WORLD, &size);
   MPI_Comm_rank(MPI_COMM_WORLD, &rank);

   // Printing
   std::cout <<  "Hello world, from process #"  << rank << std::endl;

   // Finalisation
   MPI_Finalize();
   return  0;
}
  • C Programming Language
#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
   // Initialize the MPI environment
   MPI_Init(NULL, NULL);

   // Get the number of processes
   int world_size;
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);

   // Get the rank of the process
   int world_rank;
   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

   // Get the name of the processor
   char processor_name[MPI_MAX_PROCESSOR_NAME];
   int name_len;
   MPI_Get_processor_name(processor_name, &name_len);

   // Print off a hello world message
   printf("Hello world from processor %s, rank %d out of %d processors\n",
         processor_name, world_rank, world_size);
   // Finalize the MPI environment.
   MPI_Finalize();
}

Install MPI

sudo apt install mpich

Now you've written a basic MPI program. You can compile using

C++
Combining compilation and linking in a single command
   mpicxx -o foo foo.cpp
   mpicxx -o <executable file name> <source file name>.cpp

C
Combining compilation and linking in a single command
   mpicc -o foo foo.c
   mpicc -o <executable file name> <source file name>.c

To run the executable
   mpirun -np <processors to use> ./<executable file name>