Blog

Posted 2010/09/01

MPI Hello World

Hello world MPI example.

#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();
}