Hello World Program

    The "Hello World" program was my first test of the parallel processor.  The way it works is this.  A string is send around to each process and each process appends a string to it and send it on.  Process 0 starts it off, and then sends it on to process 1.  Process 1 sends it to process 2, etc.
    This is written as one program that gets copied to each processor.  This is the listing of the program:
 

#include <mpi.h>
#include <stdio.h>

main(int argc, char *argv[])
{
  char message[1024];
  int rank,numnodes;
  char node[2];
  MPI_Status stat;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  node[0] = rank + '0';
  node[1] = '\0';
  MPI_Comm_size(MPI_COMM_WORLD, &numnodes);
  if (rank == 0)
    {
      printf("\n\nRunning %d processes.\n\n",numnodes);
      strcpy(message,"Hello World from process 0.");
      if (numnodes != 0)
        {
          MPI_Send(message,strlen(message),MPI_CHAR,1,11,MPI_COMM_WORLD);
          MPI_Recv(message,1024,MPI_CHAR,numnodes-1,11,MPI_COMM_WORLD,&stat);
        }
      printf("\n\n%s\n\n",message);
    }
  else
    {
      MPI_Recv(message,1024,MPI_CHAR,rank-1,11,MPI_COMM_WORLD,&stat);
      strcat(message,"\nHello World from process ");
      strcat(message,node);
      strcat(message,".");
      if (rank < numnodes-1)
        MPI_Send(message,strlen(message),MPI_CHAR,rank+1,11,MPI_COMM_WORLD);
      else
        MPI_Send(message,strlen(message),MPI_CHAR,0,11,MPI_COMM_WORLD);
    }
  MPI_Finalize();
}

Compile the program with "hcc:

and run the program with the "mpirun" program the output is like this: For more information on the mpirun and hcc commands try the man pages included with Lam-MPI.

Written by: David Randall
randaldt@acad.etown.edu
http://www.etown.edu/~randaldt