User Tools

Site Tools


c:mpi:comm-split

MPI Comm Split

A number of processes N, divisible by 4, are divided into groups of 4.
Within each group, processes will form a ring and send their rank to the next process in the ring.
The program will use MPI_Comm_split to create these groups.
Each process will then print its original rank and the size of the original group, as well as its new rank and the size of the new group.
Each process is only allowed to communicate within its own group.

mpi-comm-split.c
#include <mpi.h>
#include <stdio.h>
 
int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(&argc, &argv);
 
    // Get the total number of processes and the rank of this process
    int world_rank, world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 
    // Ensure the total number of processes is divisible by 4
    if (world_size % 4 != 0) {
        fprintf(stderr, "World size must be a multiple of 4\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
 
    // Calculate the color for MPI_Comm_split. This will be used to divide processes into groups
    int color = world_rank / 4;
 
    // Split the world communicator into smaller communicators, one for each group of 4 processes
    MPI_Comm new_comm;
    MPI_Comm_split(MPI_COMM_WORLD, color, world_rank, &new_comm);
 
    // Get the new rank and the size of the new group
    int new_rank, new_size;
    MPI_Comm_rank(new_comm, &new_rank);
    MPI_Comm_size(new_comm, &new_size);
 
    // Ring communication within the new group
    int token;
    if (new_rank == 0) {
        token = world_rank;
        MPI_Send(&token, 1, MPI_INT, (new_rank + 1) % new_size, 0, new_comm);
        MPI_Recv(&token, 1, MPI_INT, new_size - 1, 0, new_comm, MPI_STATUS_IGNORE);
    } else {
        MPI_Recv(&token, 1, MPI_INT, new_rank - 1, 0, new_comm, MPI_STATUS_IGNORE);
        MPI_Send(&token, 1, MPI_INT, (new_rank + 1) % new_size, 0, new_comm);
    }
 
    // Each process prints its old and new rank, and the size of the new group, along with the token received
    printf("Old Rank: %d, New Rank: %d, Size of New Group: %d, Received Token: %d\n", world_rank, new_rank, new_size, token);
 
    // Finalize the new communicator
    MPI_Comm_free(&new_comm);
 
    // Finalize the MPI environment
    MPI_Finalize();
}

Initialization: The MPI environment is initialized.

Dividing Processes into Groups: MPI_Comm_split is used to create a new communicator for each group of 4 processes. The 'color' for splitting is calculated based on the world rank.

New Rank and Size: Each process determines its new rank and the size of its new group in the new communicator.

Ring Communication within Each Group: Processes send and receive the token (their world rank) to and from their neighbors in the ring, within their new group.

Output: Each process prints its old rank, new rank, the size of its new group, and the token it received.

Finalization: The new communicator is freed, and the MPI environment is finalized.

This program demonstrates group communication using MPI_Comm_split and ring communication within each group. Number of processes must be multiple of 4.

c/mpi/comm-split.txt · Last modified: 2024/01/17 11:01 by odefta