Search code examples
mpiopenmpihpcmpi4py

mpi4py create multiple groups and scatter from each group


Say I have a comm for 64 ranks. How can I create a group in mpi4py consisting of the first x ranks, a second group consisting of the remaining 64-x ranks, and comms for each group?


Solution

  • MPI_Comm_split creates new communicators by splitting a communicator into a group of sub-communicators based on the input values color and key.

    All processes which pass in the same value for color are assigned to the same communicator. In your case, the first x processes should pass in a value for color and the rest should choose a different value.

    key determines the ordering (rank) within each new communicator. The process which passes in the smallest value for key will be rank 0, the next smallest will be rank 1, and so on. If you don't need to change the original order of processes, you can use their rank as the key.

    Combining these, here is an example in C:

    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    
    int x = 10;
    int color = rank < x;
    
    MPI_Comm new_comm;
    MPI_Comm_split(MPI_COMM_WORLD, color, rank, &new_comm);
    

    Source and further information: http://mpitutorial.com/tutorials/introduction-to-groups-and-communicators/