My First MPI program

This was long overdue IMO. Here’s the simple hello world…

#include <stdio.h> 
#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("I'm the process number %d out of %d ", rank, size)); 
 return 0; 

to compile,

$ mpicc hello.c -o hello_exec

Note: if you have writtern in C++, use “mpic++” instead.

you can execute this as a normal application as usual but if you want it to execute in a cluster, use “mpirun” command.

$ mpirun -np 5 ./hello_exec

Note: “-np” is for number of processes to spawn. If you haven’t provided where to spawn the processes they will be created in the node 0 which is usually the host machine which you are triggering the MPI application. But if you know the other nodes you just need to specify those nodes. 

$ mpirun -f hosts.txt -np 5 ./hello_exec


$ mpirun -hosts host1,host2,host3 -np 5 ./hello_exec

and the mpirun will spawn processes in those hosts in round robbin fasion.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: