/* ----------------------------------------------------------------------- Lab code: split-c.c Solution code: split-c.soln.c Lab exercise for MPI Groups and Communicator Management There are 2 parts to the project: 1. Build 2 communicators, one for even-numbered tasks (starting with task 0), one for odd-numbered tasks. 2. Do a sum reduction over each communicator. Please note that the unmodified lab code is incomplete. It will not calculate or report results correctly. AUTHOR: S. Hotovy 12/95 REVISED: R. Leibensperger 4/96, 7/31 -------------------------------------------------------------------- */ #include #include #include #include #include "mpi.h" main(int argc, char **argv) { int rank_in_world, rank_in_newcomm, color; MPI_Comm newcomm; float xsin, xsum; FILE *fdesc; char fname[13], *gname[2]; gname[0]="even"; gname[1]="odd"; MPI_Init(&argc, &argv); /* ============================================== Establish rank of this task in MPI_COMM_WORLD ============================================== */ MPI_Comm_rank(MPI_COMM_WORLD,&rank_in_world); /* ==================================================================== Project: Assign the variable color a different value for even and odd tasks, then split MPI_COMM_WORLD based on color. Store the new communicator in the variable newcomm ==================================================================== */ /* =====> Put code here */ xsin = fabs(sin(151.*(float)(rank_in_world+10))); /* ================================================================ Project: Sum all values of xsin within each communicator First: Accummulate the sum of the values of xsin across the communicator, store as xsum on the task with rank 0 Second: Learn your rank in the new communicator and assign to the variable rank_in_newcomm. The task with rank 0 will print the results ================================================================ */ /* =====> Put code here */ if (rank_in_newcomm == 0) { sprintf (fname, "%s.rep", gname[color]); fdesc = fopen(fname,"w"); fprintf(fdesc,"Sum of %s tasks: %12.5f\n",gname[color], xsum); fclose (fdesc); } MPI_Finalize(); }