/* * "hello world" program, plus hostname and command-line arguments */ #include #include #include /* MPI header file */ #define HOSTNAMELENGTH 80 int main(int argc, char *argv[]) { int nprocs; int myid; int i; char host[HOSTNAMELENGTH]; int hostlength = HOSTNAMELENGTH; /* initialize for MPI (should come before any other calls to * MPI routines) */ if (MPI_Init(&argc, &argv) != MPI_SUCCESS) { fprintf(stderr, "MPI initialization error\n"); exit(EXIT_FAILURE); } /* get number of processes * (function returns success/error, but we ignore it) */ MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* get this process's number (ranges from 0 to nprocs - 1) * (function returns success/error, but we ignore it) */ MPI_Comm_rank(MPI_COMM_WORLD, &myid); /* get hostname */ MPI_Get_processor_name(host, &hostlength); /* print a greeting */ printf("hello from process %d of %d on %s\n", myid, nprocs, host); /* show command-line arguments, if any */ for (i = 0; i < argc; ++i) printf("in process %d, argument %d is %s\n", myid, i, argv[i]); /* clean up for MPI (should come after all other calls to * MPI routines) * (function returns success/error, but we ignore it) */ MPI_Finalize(); return EXIT_SUCCESS; }