MPMD Code

The below code is for program A. Just copy the code and modify the print statement (put B instead of A) and save it as program B. Then create two executable named "a" and "b" respectively and use the instructions given in the previous page to test the MPMD job.

#include "mpi.h" #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define  MASTER        0

int main (int argc, char *argv[]) { int   numtasks, taskid, len; char hostname[MPI_MAX_PROCESSOR_NAME];

/* add in MPI startup routines * 1st: launch the MPI processes on each node */ MPI_Init(&argc, &argv);

/* Request a thread id, sometimes called a "rank" from * the MPI master process, which has rank or tid == 0 */ MPI_Comm_rank(MPI_COMM_WORLD,&taskid);

/* Get the number of threads or processes launched by MPI, * and this should be NCPUs-1      */ MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

/* Get the hostname of the slot in which it is running */ MPI_Get_processor_name(hostname, &len);

printf("My id is %d[out of %d] and I am saying hello from program [A] running on %s!\n", taskid, numtasks, hostname);

MPI_Finalize(); return 0; }