#include #include int main(int argc, char** argv) { /* our rank and size */ int rank, size; /* we need to create groups of processes */ MPI_Group orig_group, new_group; /* find our rank and size in MPI_COMM_WORLD */ MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); /* make arrays storing the ranks of the processes in group1 and group2 */ int ranks1[size / 2], ranks2[size / 2], i; for (i = 0; i < size/2; i++) { ranks1[i] = i; } for(i = size/2; i < size; i++) { ranks2[i - size/2] = i; } /* find the original group */ MPI_Comm_group(MPI_COMM_WORLD, &orig_group); /* Divide tasks into two distinct groups based upon rank */ if (rank < size/2) { MPI_Group_incl(orig_group, size/2, ranks1, &new_group); } else { MPI_Group_incl(orig_group, size/2, ranks2, &new_group); } /* Create new communicator for our group */ MPI_Comm new_comm; MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm); /* have the processes sum the ranks of each group */ int send = rank, recv; MPI_Allreduce(&send, &recv, 1, MPI_INT, MPI_SUM, new_comm); /* get our rank within the new group */ int grank; MPI_Comm_rank(new_comm, &grank); /* print the results */ printf("Process %d (%d in sub-group) has %d!\n", rank, grank, recv); /* quit */ MPI_Finalize(); return 0; }