#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, flag, np; 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 2 groups */ colour = id % 2; 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 */ 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 two groups */ MPI_Intercomm_merge(InterComm,high,&IntraComm); LocComm = IntraComm; if ( lid == 0 ) { fprintf(stdout,"Check if InterComm is an inter-communicator: "); MPI_Comm_test_inter(InterComm, &flag); fprintf(stdout,"flag = %d\n",flag); fprintf(stdout,"Check if IntraComm is an inter-communicator: "); MPI_Comm_test_inter(IntraComm, &flag); fprintf(stdout,"flag = %d\n",flag); } /* 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("Global sum = %d\n",lsum); MPI_Barrier(GlobalComm); MPI_Comm_free(&InterComm); MPI_Comm_free(&LocComm); MPI_Comm_free(&GlobalComm); MPI_Comm_free(&IntraComm); MPI_Finalize(); return(0); }