tutorial/q07.err_comm_grid2d.c
/* Create two communicators, representing a PxP 2D grid of
 * the processes. Either return MPIX_ERR_PROC_FAILED at all ranks,
 * then no communicator has been created, or MPI_SUCCESS and all communicators
 * have been created at all ranks. */
Create two communicators, representing a PxP 2D grid of
the processes. Either return MPIX_ERR_PROC_FAILED at all ranks,
then no communicator has been created, or MPI_SUCCESS and all communicators
have been created at all ranks.
Create two communicators representing a PxP 2D grid of the processes . Either return MPIX_ERR_PROC_FAILED at all ranks then no communicator has been created or MPI_SUCCESS and all communicators have been created at all ranks .
int ft_comm_grid2d(MPI_Comm comm, int p, MPI_Comm *rowcomm, MPI_Comm *colcomm) {
    int np, rank;
    int rc1, rc2;
    int flag;
    MPI_Comm_rank(comm, &rank);
    rc1 = MPI_Comm_split(comm, rank%p, rank, rowcomm);
    flag = (MPI_SUCCESS==rc1);
    MPIX_Comm_agree(comm, &flag);
    if( !flag ) {
        if( rc1 == MPI_SUCCESS ) {
            MPI_Comm_free(rowcomm);
        }
        return MPIX_ERR_PROC_FAILED;
    }
    rc2 = MPI_Comm_split(comm, rank/p, rank, colcomm);
    flag = (MPI_SUCCESS==rc2);
    MPIX_Comm_agree(comm, &flag);
    if( !flag ) {
        if( rc2 == MPI_SUCCESS ) {
            MPI_Comm_free(colcomm);
        }
        return MPIX_ERR_PROC_FAILED;
    }
    return MPI_SUCCESS;
}
int ft_comm_grid2d ( MPI_Comm comm , int p , MPI_Comm * rowcomm , MPI_Comm * colcomm ) { int np , rank ; int rc1 , rc2 ; int flag ; MPI_Comm_rank ( comm , & rank ) ; rc1 = MPI_Comm_split ( comm , rank % p , rank , rowcomm ) ; flag = ( MPI_SUCCESS == rc1 ) ; MPIX_Comm_agree ( comm , & flag ) ; if ( ! flag ) { if ( rc1 == MPI_SUCCESS ) { MPI_Comm_free ( rowcomm ) ; } return MPIX_ERR_PROC_FAILED ; } rc2 = MPI_Comm_split ( comm , rank / p , rank , colcomm ) ; flag = ( MPI_SUCCESS == rc2 ) ; MPIX_Comm_agree ( comm , & flag ) ; if ( ! flag ) { if ( rc2 == MPI_SUCCESS ) { MPI_Comm_free ( colcomm ) ; } return MPIX_ERR_PROC_FAILED ; } return MPI_SUCCESS ; }
Create two communicators, representing a PxP 2D grid of
 the processes.
Create two communicators representing a PxP 2D grid of the processes .
/*
     * This routine is not optimal. Can you improve it?
     */