Blog

Posted 2011/08/31

MPI Hello World

I have included compilation and execution examples.

hello.c
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "mpi.h"

main(int argc, char **argv ) {
  char message[20];
  int i,rank, size, type=99;
  MPI_Status status;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD,&size);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  if(rank == 0) {
    strcpy(message, "Hello, world");
    for (i=1; i<size; i++){
        MPI_Send(message, 13, MPI_CHAR, i, type, MPI_COMM_WORLD);
    }
  }else{
    MPI_Recv(message, 20, MPI_CHAR, 0, type, MPI_COMM_WORLD, &status);
  }
  printf("Message printed by process with rank %d : '%.13s'\n", rank,message);
  MPI_Finalize();
}
How to compile and run hello
$ mpicc -o hello hello.c
$ mpd & 
$ mpiexec -n 10 ./hello