#include #include main(int argc, char* argv[]) { MPI_Comm GlobalComm, LocComm, InterComm=MPI_COMM_NULL, IntraComm=MPI_COMM_NULL; MPI_Status stat; int high, colour, numproc, id, lid, root, idinter; int val, lsum, gsum; MPI_Init(&argc, &argv); MPI_Comm_dup(MPI_COMM_WORLD,&GlobalComm); MPI_Comm_size(GlobalComm, &numproc); MPI_Comm_rank(GlobalComm, &id); /* Divide processes in 3 groups */ colour = id % 3; if ( id == 0 && numproc < 3 ) { fprintf(stderr,"Less than 3 processes have been activated: error exit\n"); MPI_Finalize(); return(1); } MPI_Comm_split(GlobalComm, colour, id, &LocComm); MPI_Comm_rank(LocComm, &lid); printf("Process %d in global comm is process %d in local comm\n",id,lid); /* Generate inter communicator between 2 leaders of first two groups the value of colour identifies the group to be considered */ if ( colour < 2 ) { if ( colour == 0 ) { MPI_Intercomm_create(LocComm, 0, GlobalComm, 1, 01, &InterComm); } else { MPI_Intercomm_create(LocComm, 0, GlobalComm, 0, 01, &InterComm); } high = colour; /* merge first two groups */ MPI_Intercomm_merge(InterComm,high,&IntraComm); LocComm = IntraComm; } /* Sum of values: each group computes a partial sum */ val = 1; lsum = 0; MPI_Comm_rank(LocComm, &lid); MPI_Reduce(&val,&lsum,1,MPI_INT,MPI_SUM,0,LocComm); if ( lid == 0 ) printf("Local sum = %d\n",lsum); MPI_Barrier(GlobalComm); MPI_Finalize(); return(0); }