/* builds n communicators each with m processors from a collection of * m*n processors using MPI_Comm_split * * here m is implicitly the number of rows in a virtual * process grid and n is the number of columns in a virtual * process grid. * * Input: m (the global total number of rows) * Note: with m given n is then derived via n = p/m * * Output: n -- program tests correct creation of new communicator * by broadcasting the value 1 to its members -- all other * processes have the value 0 -- global sum computed across * ALL the processes. * * See Chap. 7, pp. 120 & ff in PPMPI * by John L. Weatherwax */ #include #include #include "mpi.h" main(int argc, char* argv[]) { int p,m,n; int my_rank; MPI_Comm my_col_comm; int my_row,my_col; int q; int test,sum; int my_rank_in_col; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* Input arguments */ if( my_rank==0 ){ printf("Enter m: "); scanf("%d",&m); printf("Virtual grid is of size [%d,%d]\n",m,(int) (p/m)); } MPI_Bcast(&m,1,MPI_INT,0,MPI_COMM_WORLD); n = (int) (p/m); /* my_rank is rank in MPI_COMM_WORLD. */ my_row = my_rank/n; my_col = my_rank - (my_row)*n; MPI_Comm_split(MPI_COMM_WORLD, my_col, my_rank, &my_col_comm); /* Test the new communicators */ MPI_Comm_rank(my_col_comm, &my_rank_in_col); if (my_rank_in_col == 0) test = 1; /*my_col;*/ else test = 0; MPI_Bcast(&test, 1, MPI_INT, 0, my_col_comm); MPI_Reduce(&test, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (my_rank == 0) { printf("sum = %d\n", sum); } MPI_Finalize(); } /* main */