155. (Approximate) Current Practice #3

PreviousUpNext
Up: Motivating Examples Next: Example #4 Previous: Current Practice #2


  int main(int argc, char *argv[]) 
  { 
    int me, count, count2; 
    void *send_buf, *recv_buf, *send_buf2, *recv_buf2; 
    MPI_Group group_world, grprem; 
    MPI_Comm commslave; 
    static int ranks[] = {0}; 
    ... 
    MPI_Init(&argc, &argv); 
    MPI_Comm_group(MPI_COMM_WORLD, &group_world); 
    MPI_Comm_rank(MPI_COMM_WORLD, &me);  /* local */ 
 
    MPI_Group_excl(group_world, 1, ranks, &grprem);  /* local */ 
    MPI_Comm_create(MPI_COMM_WORLD, grprem, &commslave); 
 
    if(me != 0) 
    { 
      /* compute on slave */ 
      ... 
      MPI_Reduce(send_buf,recv_buf,count, MPI_INT, MPI_SUM, 1, commslave); 
      ... 
      MPI_Comm_free(&commslave); 
    } 
    /* zero falls through immediately to this reduce, others do later... */ 
    MPI_Reduce(send_buf2, recv_buf2, count2, 
               MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); 
 
    MPI_Group_free(&group_world); 
    MPI_Group_free(&grprem); 
    MPI_Finalize(); 
    return 0; 
  } 
This example illustrates how a group consisting of all but the zeroth process of the ``all'' group is created, and then how a communicator is formed ( commslave) for that new group. The new communicator is used in a collective call, and all processes execute a collective call in the MPI_COMM_WORLD context. This example illustrates how the two communicators (that inherently possess distinct contexts) protect communication. That is, communication in MPI_COMM_WORLD is insulated from communication in commslave, and vice versa.

In summary, ``group safety'' is achieved via communicators because distinct contexts within communicators are enforced to be unique on any process.


PreviousUpNext
Up: Motivating Examples Next: Example #4 Previous: Current Practice #2


Return to MPI-3.1 Standard Index
Return to MPI Forum Home Page

(Unofficial) MPI-3.1 of June 4, 2015
HTML Generated on June 4, 2015