#include #include "mpi.h" int main (int argc, char *argv[]) { int id, np; char name[MPI_MAX_PROCESSOR_NAME]; char message[128], filename[128], line[128]; int namelen; MPI_Status status; int i, st; FILE *fid; 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 (); /* ------------------------------------------------------------------- */ /* The following code tests whether every process can read the same input file from the run directory: */ sprintf (filename, "testio.in"); fid = fopen (filename, "r"); if (fid == NULL) { fprintf (stderr, "Problem opening input file %s. Aborting.\n", filename); MPI_Abort (MPI_COMM_WORLD, 1); } fgets (line, sizeof(line), fid); printf ("[%2d] Line from input file: %s\n", id, line); fflush (stdout); fclose (fid); /* ------------------------------------------------------------------- */ /* The following code tests whether each process can write to a file with filename testio-p.out, where is its own process number, and whether these files end up in the run directory: */ sprintf (filename, "testio-p%2.2d.out", id); fid = fopen (filename, "w"); if (fid == NULL) { fprintf (stderr, "Problem opening input file %s. Aborting.\n", filename); MPI_Abort (MPI_COMM_WORLD, 1); } sprintf (message, "Output filename = %s", filename); st = fprintf (fid, "[%2d] Message to output file: %s\n", id, message); if (st < 0) { fprintf (stderr, "Problem writing line to file. Aborting.\n"); MPI_Abort (MPI_COMM_WORLD, 2); } /* print message to screen in case of success: */ printf ("[%2d] Message successfully printed to file %s\n", id, filename); fflush (stdout); fclose (fid); /* ------------------------------------------------------------------- */ 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); } } /* --------------------------------------------------------------------- */