Thursday 12 August 2021

Hello World program in mpi

                           In this post, we will see Hello World program in MPI.

                           For How to install MPI on Linux (Ubuntu and Fedora), check following link: 
#include "mpi.h"

int main(int argc, char **argv)
{
   int rank, size;
   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
   MPI_Comm_size(MPI_COMM_WORLD, &size);
   printf("Hello world! Process of rank %d out of %d processes.\n",rank,size);
   MPI_Finalize();
   return 0;
}
 
Output:
>>> mpic++ helloworldmpi.c 
>>> mpirun -np 5 ./a.out
Hello world! Process of rank 0 out of 5 processes.
Hello world! Process of rank 2 out of 5 processes.
Hello world! Process of rank 3 out of 5 processes.
Hello world! Process of rank 1 out of 5 processes.
Hello world! Process of rank 4 out of 5 processes.

  
Note:
                     Here, 5 is the number of processes. We compile program by mpic++. We get object file a.out. Then, we run it on many processes (in this example 5). 
                     Function MPI_Comm_rank() gives rank of process. Ranks are in the range 0 to n-1.
                     Function MPI_Comm_size() gives number of processes.

                     

No comments:

Post a Comment