PROGRAM Main
    USE MPI
    IMPLICIT NONE
    INTEGER :: comm, comm1, comm2, rank, rank1, rank2, error
    INTEGER, PARAMETER :: dims(2) = (/ 3, 5 /)
    LOGICAL, PARAMETER :: periodic(2) = .TRUE.
    CHARACTER(LEN=*), PARAMETER :: format =    &
        '("Rank ",I2," has ranks ",I0,",",I0)'

    CALL MPI_Init(error)
    CALL MPI_Cart_create(MPI_COMM_WORLD,2,dims,periodic,.TRUE.,comm,error)
    IF (comm /= MPI_COMM_NULL) THEN
        CALL MPI_Comm_rank(comm,rank,error)
        CALL MPI_Cart_sub(comm,(/.TRUE.,.FALSE./),comm1,error)
        CALL MPI_Comm_rank(comm1,rank1,error)
        CALL MPI_Cart_sub(comm,(/.FALSE.,.TRUE./),comm2,error)
        CALL MPI_Comm_rank(comm2,rank2,error)
        PRINT format, rank, rank1, rank2
    END IF
    CALL MPI_Finalize(error)

END PROGRAM Main
