Blame test/mpi/comm/iccreate.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2007 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
 * This program tests that MPI_Comm_create applies to intercommunicators;
Packit 0848f5
 * this is an extension added in MPI-2
Packit 0848f5
 */
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int size, isLeft, wrank;
Packit 0848f5
    MPI_Comm intercomm, newcomm;
Packit 0848f5
    MPI_Group oldgroup, newgroup;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit 0848f5
    if (size < 4) {
Packit 0848f5
        printf("This test requires at least 4 processes\n");
Packit 0848f5
        MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
    }
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
Packit 0848f5
Packit 0848f5
    while (MTestGetIntercomm(&intercomm, &isLeft, 2)) {
Packit 0848f5
        int ranks[10], nranks, result;
Packit 0848f5
Packit 0848f5
        if (intercomm == MPI_COMM_NULL)
Packit 0848f5
            continue;
Packit 0848f5
Packit 0848f5
        MPI_Comm_group(intercomm, &oldgroup);
Packit 0848f5
        ranks[0] = 0;
Packit 0848f5
        nranks = 1;
Packit 0848f5
        MTestPrintfMsg(1, "Creating a new intercomm 0-0\n");
Packit 0848f5
        MPI_Group_incl(oldgroup, nranks, ranks, &newgroup);
Packit 0848f5
        MPI_Comm_create(intercomm, newgroup, &newcomm);
Packit 0848f5
Packit 0848f5
        /* Make sure that the new communicator has the appropriate pieces */
Packit 0848f5
        if (newcomm != MPI_COMM_NULL) {
Packit 0848f5
            int new_rsize, new_size, flag, commok = 1;
Packit 0848f5
Packit 0848f5
            MPI_Comm_set_name(newcomm, (char *) "Single rank in each group");
Packit 0848f5
            MPI_Comm_test_inter(intercomm, &flag;;
Packit 0848f5
            if (!flag) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("[%d] Output communicator is not an intercomm\n", wrank);
Packit 0848f5
                commok = 0;
Packit 0848f5
            }
Packit 0848f5
Packit 0848f5
            MPI_Comm_remote_size(newcomm, &new_rsize);
Packit 0848f5
            MPI_Comm_size(newcomm, &new_size);
Packit 0848f5
            /* The new communicator has 1 process in each group */
Packit 0848f5
            if (new_rsize != 1) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("[%d] Remote size is %d, should be one\n", wrank, new_rsize);
Packit 0848f5
                commok = 0;
Packit 0848f5
            }
Packit 0848f5
            if (new_size != 1) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("[%d] Local size is %d, should be one\n", wrank, new_size);
Packit 0848f5
                commok = 0;
Packit 0848f5
            }
Packit 0848f5
            /* ... more to do */
Packit 0848f5
            if (commok) {
Packit 0848f5
                errs += MTestTestComm(newcomm);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        MPI_Group_free(&newgroup);
Packit 0848f5
        if (newcomm != MPI_COMM_NULL) {
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Now, do a sort of dup, using the original group */
Packit 0848f5
        MTestPrintfMsg(1, "Creating a new intercomm (manual dup)\n");
Packit 0848f5
        MPI_Comm_create(intercomm, oldgroup, &newcomm);
Packit 0848f5
        MPI_Comm_set_name(newcomm, (char *) "Dup of original");
Packit 0848f5
        MTestPrintfMsg(1, "Creating a new intercomm (manual dup (done))\n");
Packit 0848f5
Packit 0848f5
        MPI_Comm_compare(intercomm, newcomm, &result);
Packit 0848f5
        MTestPrintfMsg(1, "Result of comm/intercomm compare is %d\n", result);
Packit 0848f5
        if (result != MPI_CONGRUENT) {
Packit 0848f5
            const char *rname = 0;
Packit 0848f5
            errs++;
Packit 0848f5
            switch (result) {
Packit 0848f5
            case MPI_IDENT:
Packit 0848f5
                rname = "IDENT";
Packit 0848f5
                break;
Packit 0848f5
            case MPI_CONGRUENT:
Packit 0848f5
                rname = "CONGRUENT";
Packit 0848f5
                break;
Packit 0848f5
            case MPI_SIMILAR:
Packit 0848f5
                rname = "SIMILAR";
Packit 0848f5
                break;
Packit 0848f5
            case MPI_UNEQUAL:
Packit 0848f5
                rname = "UNEQUAL";
Packit 0848f5
                break;
Packit 0848f5
                printf("[%d] Expected MPI_CONGRUENT but saw %d (%s)", wrank, result, rname);
Packit 0848f5
                fflush(stdout);
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            /* Try to communication between each member of intercomm */
Packit 0848f5
            errs += MTestTestComm(newcomm);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        if (newcomm != MPI_COMM_NULL) {
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
        /* test that an empty group in either side of the intercomm results in
Packit 0848f5
         * MPI_COMM_NULL for all members of the comm */
Packit 0848f5
        if (isLeft) {
Packit 0848f5
            /* left side reuses oldgroup, our local group in intercomm */
Packit 0848f5
            MPI_Comm_create(intercomm, oldgroup, &newcomm);
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            /* right side passes MPI_GROUP_EMPTY */
Packit 0848f5
            MPI_Comm_create(intercomm, MPI_GROUP_EMPTY, &newcomm);
Packit 0848f5
        }
Packit 0848f5
        if (newcomm != MPI_COMM_NULL) {
Packit 0848f5
            printf("[%d] expected MPI_COMM_NULL, but got a different communicator\n", wrank);
Packit 0848f5
            fflush(stdout);
Packit 0848f5
            errs++;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        if (newcomm != MPI_COMM_NULL) {
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
        MPI_Group_free(&oldgroup);
Packit 0848f5
        MPI_Comm_free(&intercomm);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}