/* ----------------------------------------------------------------------- split-c.soln.c Lab solution 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. AUTHOR: S. Hotovy 12/95 REVISED: R. Leibensperger 4/96 -------------------------------------------------------------------- */ #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 */ if ((rank_in_world % 2) == 0) color = 0; else color = 1; MPI_Comm_split (MPI_COMM_WORLD, color, rank_in_world, &newcomm); 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 */ MPI_Reduce(&xsin,&xsum,1,MPI_FLOAT,MPI_SUM,0,newcomm); MPI_Comm_rank (newcomm, &rank_in_newcomm); 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(); }