none
MPI_Group_incl RRS feed

  • Question

  • Hello,

    I need  divide the processes into two groups

    If I do this:

    # include <mpi.h> # include <stdio.h> # define NPROCS 8 int main(int argc, char** argv) { int rank, new_rank, sendbuf, recvbuf, numtasks, ranks1[4]={0,1,2,3}, ranks2[4]={4,5,6,7}; MPI_Group orig_group, new_group; MPI_Comm new_comm; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); if (numtasks != NPROCS) { printf("Must specify MP_PROCS= %d. Terminating.\n", NPROCS); MPI_Finalize(); exit(0); } sendbuf = rank; /* Step 1 */ MPI_Comm_group(MPI_COMM_WORLD, &orig_group); /* Step 2 */ if (rank < NPROCS/2) { MPI_Group_incl(orig_group, NPROCS/2, ranks1, &new_group); } else { MPI_Group_incl(orig_group, NPROCS/2, ranks2, &new_group); } /* Step 3 */ MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm); /* Step 5 */ MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm); /* Step 4 */ MPI_Group_rank (new_group, &new_rank); printf("rank= %d newrank= %d recvbuf= %d\n",rank,new_rank,recvbuf); MPI_Finalize(); }

    In this case,the code works. But in this example, the number of processes known in advance.

    I'm trying to do this:

    #include "mpi.h"
    #include <stdio.h>
    #include <cstring>
    
    int main(int argc,char *argv[])
    {
    int rank, new_rank, sendbuf, recvbuf, numtasks,proc;
       
      MPI_Group  orig_group, new_group;
      MPI_Comm   new_comm;
    
      MPI_Init(&argc, &argv);
      MPI_Comm_rank(MPI_COMM_WORLD, &rank);
      MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
      
    printf("New group contains processes:");
    int q = numtasks/2;
    int process_ranks[q];
    int process_ranks1[q];
    
    sendbuf = rank;
    
    for (proc = 0; proc < q; proc++)
    {
    
    
    process_ranks[proc] = proc;
    printf("g=%i ", process_ranks[proc]);
    
    }
    
    for (proc = q; proc < numtasks; proc++)
    {
    
    
    process_ranks1[proc] = proc;
    printf("g1=%i ", process_ranks1[proc]);
    }
    
    MPI_Comm_group(MPI_COMM_WORLD, &orig_group);
    
      /* Step 2 */
      
      if (rank < q ) {
        MPI_Group_incl(orig_group, q , process_ranks, &new_group);
      }
      else {
        MPI_Group_incl(orig_group, q , process_ranks1, &new_group);
      }
    
      /* Step 3 */
      
      MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm);
    
      /* Step 5 */
      
      MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm);
    
      /* Step 4 */
      
      MPI_Group_rank (new_group, &new_rank);
    
      printf("rank= %d newrank= %d recvbuf= %d\n",rank,new_rank,recvbuf);
    MPI_Finalize();
    return 0;
    }

    But for some reason this code does not work.
    Thanks in advance.



    • Edited by CaptainV Saturday, March 3, 2012 4:17 AM
    Saturday, March 3, 2012 4:13 AM

All replies

  • Are there any ideas?

    Help me please solve this problem.


    • Edited by CaptainV Saturday, March 3, 2012 12:56 PM
    Saturday, March 3, 2012 12:56 PM
  • I find error.....

    The correct source code

    #include "mpi.h"
    #include <stdio.h>
    #define NPROCS 8
    int main(int argc, char **argv) 
     {
    int rank, new_rank, sendbuf, recvbuf, numtasks;
    
    
    MPI_Group orig_group, new_group;
    MPI_Comm new_comm;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    
    
    int *ranks1 = new int [numtasks/2];
    int *ranks2 = new int [numtasks/2];
    for(int i=0;i<numtasks/2;i++)
    ranks1[i]=i; 
    
    for(int i=0;i<numtasks/2;i++)
    ranks2[i]=i+numtasks/2;
    
    /*if (numtasks != NPROCS) {
    printf("Must specify MP_PROCS= %d. Terminating.\n",NPROCS);
    MPI_Finalize();
    exit(0);
    }*/
    sendbuf = rank;
    /* Extract the original group handle */
    MPI_Comm_group(MPI_COMM_WORLD, &orig_group);
    /* Divide tasks into two distinct groups based upon rank */
    if (rank < NPROCS/2) {
    MPI_Group_incl(orig_group, NPROCS/2, ranks1, &new_group);
    }
    else {
    MPI_Group_incl(orig_group, NPROCS/2, ranks2, &new_group);
    }
    /* Create new new communicator and then perform collective
    communications */
    MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm);
    MPI_Allreduce(&sendbuf, &recvbuf, 1, MPI_INT, MPI_SUM, new_comm);
    MPI_Group_rank (new_group, &new_rank);
    printf("rank= %d newrank= %d recvbuf= %d\n",rank,new_rank,recvbuf);
    MPI_Finalize();
    }


    • Edited by CaptainV Friday, April 6, 2012 9:04 AM
    Friday, April 6, 2012 8:33 AM