

/* helloworld.c */
#include <stdio.h>
/* You MUST include this for the MPI_* functions */
#include "mpi.h"
int main(int argc, char **argv) {
int rank;
char host[150];
int namelen;
/* Initialize MPI. This handles mpich-specific command line arguments */
MPI_Init(&argc, &argv);
/* Get my rank. My rank number gets stored in the 'rank' variable */
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* Look up what computer I am running on. Store it in 'host' */
MPI_Get_processor_name(host,&namelen);
printf("Hello world (Rank: %d / Host: %s)\n", rank, host);
fflush(stdout);
/* Finalize: Close connections to the other children, clean up memory
* the MPI library has allocated, etc */
MPI_Finalize();
return 0;
}
mpicc -o hellowowrld helloworld.cIf there were any errors from the compiler, you'll need to fix them (obviously) before you can move on. We need to run the program to make sure it works! This requires us to use the mpirun command. This command takes a few command-line options you'll want to know about:
mpirun -np 6 helloworldThis starts up 6 helloworld processes on the cluster. The machines it picks comes from a hosts list on the master node. It uses a round-robin technique to select machines to run processes on. The output of helloworld is as follows:
Hello world (Rank: 0 / Host: master.bwee01.rit.edu) Hello world (Rank: 2 / Host: n02.bwee01.rit.edu) Hello world (Rank: 3 / Host: n03.bwee01.rit.edu) Hello world (Rank: 4 / Host: n04.bwee01.rit.edu) Hello world (Rank: 5 / Host: n05.bwee01.rit.edu) Hello world (Rank: 1 / Host: n01.bwee01.rit.edu)
/* networld.c */
#include <stdio.h>
#include "mpi.h"
int main(int argc, char **argv) {
int rank;
char host[150];
int namelen;
/* Message tag */
int tag = 1;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(host,&namelen);
printf("Hello world (Rank: %d / Host: %s)\n", rank, host);
fflush(stdout);
if (rank == 0) {
int numprocs, x;
char msg[50];
MPI_Status status;
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
for (x = 1; x < numprocs; x++) {
MPI_Recv(msg, 50, MPI_CHARACTER, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
printf("Msg from %d: '%s'\n", status.MPI_SOURCE, msg);
fflush(stdout);
}
} else {
char msg[50];
snprintf(msg, 50, "Hello from node rank %d.", rank);
MPI_Send(msg, 50, MPI_CHARACTER, 0, tag, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
mpicc -o networld networld.cAssuming you don't get any errors, you can now run it:
mpirun -np 6 networldA sample output is as follows:
Hello world (Rank: 0 / Host: master.bwee01.rit.edu) Msg from 1: 'Hello from node rank 1.' Msg from 2: 'Hello from node rank 2.' Msg from 3: 'Hello from node rank 3.' Msg from 4: 'Hello from node rank 4.' Msg from 5: 'Hello from node rank 5.' Hello world (Rank: 4 / Host: n04.bwee01.rit.edu) Hello world (Rank: 2 / Host: n02.bwee01.rit.edu) Hello world (Rank: 3 / Host: n03.bwee01.rit.edu) Hello world (Rank: 1 / Host: n01.bwee01.rit.edu) Hello world (Rank: 5 / Host: n05.bwee01.rit.edu)Communication is pretty simple. Documentation on both of these functions can be found on the MPICH homepage. The only point worth mentioning of these functions is the 'tag' variable. This variable indicates the kind of message you are sending. MPI doesn't care what this number is, but if a child sends a message with tag 3 to rank 0, and rank 0 is waiting to receive data on tag 15, rank 0 will not get this message tagged 3 becuase it's wanting a message tagged 15.