#include #include "mpi.h" int main (int argc, char *argv[]) { int id, np; char name[MPI_MAX_PROCESSOR_NAME]; char message[128]; int namelen; MPI_Status status; int i; void nodesused (); MPI_Init (&argc, &argv); MPI_Comm_size (MPI_COMM_WORLD, &np); MPI_Comm_rank (MPI_COMM_WORLD, &id); MPI_Get_processor_name (name, &namelen); /* ------------------------------------------------------------------- */ /* The following code prints the name of the run directory: */ if (id == 0) { printf ("\n"); printf ("Running in directory\n"); system ("/bin/pwd"); printf ("\n"); fflush (stdout); } /* ------------------------------------------------------------------- */ /* The following code tests MPI_Bcast: */ sprintf (message, "This is a message from Process %2d to all", id); MPI_Bcast (message, 128, MPI_CHAR, 0, MPI_COMM_WORLD); printf ("[%2d] %s\n", id, message); fflush (stdout); /* ------------------------------------------------------------------- */ /* The following code tests MPI_Send / MPI_Recv: */ nodesused (); /* ------------------------------------------------------------------- */ MPI_Finalize (); return (0); } /* --------------------------------------------------------------------- */ void nodesused (void) { int id, np; char name[MPI_MAX_PROCESSOR_NAME]; int namelen; MPI_Status status; int i; char message[128]; MPI_Comm_size (MPI_COMM_WORLD, &np); MPI_Comm_rank (MPI_COMM_WORLD, &id); MPI_Get_processor_name (name, &namelen); sprintf (message, "Process %2d out of %2d running on host %s", id, np, name); /* uncomment for testing: */ /* printf ("[%2d] %s\n", id, message); */ if (id == 0) { printf ("\n"); /* print own message first: */ printf ("[%2d] %s\n", id, message); /* receive and print messages from other processes: */ for (i = 1; i < np; i++) { MPI_Recv (message, 128, MPI_CHAR, i, i, MPI_COMM_WORLD, &status); printf ("[%2d] %s\n", id, message); } fflush (stdout); } else /* (id > 0) */ { MPI_Send (message, strlen(message)+1, MPI_CHAR, 0, id, MPI_COMM_WORLD); } } /* --------------------------------------------------------------------- */