Saturday, January 5, 2013

MPI Hello World - 1

Here is the Hello World Sample.
#include <stdio.h>
#include "mpi.h"
int main (int argc, char *argv[])
{
int rank, nprocs;
MPI_Init (&argc, &argv);
/* creates MPI execution environment */
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
/* get current process rank */
MPI_Comm_size (MPI_COMM_WORLD, &nprocs);
/* get number of processes */
printf("Hello world from process %d of %d\n", rank, nprocs);
MPI_Finalize();
/* terminates the MPI execution environment */
}
view raw helloworld.c hosted with ❤ by GitHub


Compile it using
mpicc helloworld.c
view raw Compiling hosted with ❤ by GitHub


Execute it using
mpirun -np 5 ./a.out
view raw execute hosted with ❤ by GitHub


Here is the Hello World Sample for Python. You cannot execute it in CC server but you can install the mpi4py module on your workstation
#!/usr/bin/env python
from mpi4py import MPI
comm = MPI.COMM_WORLD
print "Hello! I'm rank %d from %d running in total..." % (comm.rank, comm.size)
comm.Barrier()
#Waits for all process to synchronize _here_
#It is optional as it just allows Hello World to be printed in order 0 ... n-1


Execute it using
mpirun -np 4 helloworld_with_barrier.py
view raw execute python hosted with ❤ by GitHub


Lecture
MPI Hello World - 2
MPI Pi Calculation