Blame test/mpi/errors/comm/ccreate1.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
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 <string.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/* Check that Comm_create detects the case where the group is not a subset of
Packit 0848f5
   the group of the input communicator */
Packit 0848f5
Packit 0848f5
void abortMsg(const char *, int);
Packit 0848f5
Packit 0848f5
void abortMsg(const char *str, int code)
Packit 0848f5
{
Packit 0848f5
    char msg[MPI_MAX_ERROR_STRING];
Packit 0848f5
    int class, resultLen;
Packit 0848f5
Packit 0848f5
    MPI_Error_class(code, &class);
Packit 0848f5
    MPI_Error_string(code, msg, &resultLen);
Packit 0848f5
    fprintf(stderr, "%s: errcode = %d, class = %d, msg = %s\n", str, code, class, msg);
Packit 0848f5
    MPI_Abort(MPI_COMM_WORLD, code);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    MPI_Comm evencomm, lowcomm, newcomm;
Packit 0848f5
    int wrank, wsize, gsize, err, errs = 0;
Packit 0848f5
    int ranges[1][3], mygrank;
Packit 0848f5
    MPI_Group wGroup, godd, ghigh, geven;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
Packit 0848f5
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &wsize);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
Packit 0848f5
Packit 0848f5
    /* Create some communicators */
Packit 0848f5
    MPI_Comm_split(MPI_COMM_WORLD, wrank % 2, wrank, &evencomm);
Packit 0848f5
    MPI_Comm_split(MPI_COMM_WORLD, wrank < wsize / 2, wsize - wrank, &lowcomm);
Packit 0848f5
    MPI_Comm_set_errhandler(evencomm, MPI_ERRORS_RETURN);
Packit 0848f5
    MPI_Comm_set_errhandler(lowcomm, MPI_ERRORS_RETURN);
Packit 0848f5
Packit 0848f5
    /* Create some groups */
Packit 0848f5
    MPI_Comm_group(MPI_COMM_WORLD, &wGroup);
Packit 0848f5
Packit 0848f5
    ranges[0][0] = 2 * (wsize / 2) - 1;
Packit 0848f5
    ranges[0][1] = 1;
Packit 0848f5
    ranges[0][2] = -2;
Packit 0848f5
    err = MPI_Group_range_incl(wGroup, 1, ranges, &godd);
Packit 0848f5
    if (err)
Packit 0848f5
        abortMsg("Failed to create odd group: ", err);
Packit 0848f5
    err = MPI_Group_size(godd, &gsize);
Packit 0848f5
    if (err)
Packit 0848f5
        abortMsg("Failed to get size of odd group: ", err);
Packit 0848f5
    if (gsize != wsize / 2) {
Packit 0848f5
        fprintf(stderr, "Group godd size is %d should be %d\n", gsize, wsize / 2);
Packit 0848f5
        errs++;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    ranges[0][0] = wsize / 4;
Packit 0848f5
    ranges[0][1] = wsize - 1;
Packit 0848f5
    ranges[0][2] = 1;
Packit 0848f5
    err = MPI_Group_range_incl(wGroup, 1, ranges, &ghigh);
Packit 0848f5
    if (err)
Packit 0848f5
        abortMsg("Failed to create high group\n", err);
Packit 0848f5
    ranges[0][0] = 0;
Packit 0848f5
    ranges[0][1] = wsize - 1;
Packit 0848f5
    ranges[0][2] = 2;
Packit 0848f5
    err = MPI_Group_range_incl(wGroup, 1, ranges, &geven);
Packit 0848f5
    if (err)
Packit 0848f5
        abortMsg("Failed to create even group:", err);
Packit 0848f5
Packit 0848f5
    /* Check that a correct case returns success */
Packit 0848f5
    if ((wrank % 2) == 0) {
Packit 0848f5
        err = MPI_Group_rank(geven, &mygrank);
Packit 0848f5
        if (err) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "Could not get rank from geven group\n");
Packit 0848f5
        }
Packit 0848f5
        else if (mygrank == MPI_UNDEFINED) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "mygrank should be %d but is undefined\n", wrank / 2);
Packit 0848f5
        }
Packit 0848f5
        err = MPI_Comm_create(evencomm, geven, &newcomm);
Packit 0848f5
        /* printf("Created new even comm\n"); */
Packit 0848f5
        if (err != MPI_SUCCESS) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stderr, "Failed to allow creation from even group\n");
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Now, test that these return errors when we try to use them to create
Packit 0848f5
     * groups */
Packit 0848f5
    if ((wrank % 2) == 0) {
Packit 0848f5
        /* printf("Even comm...\n"); */
Packit 0848f5
        /* evencomm is the comm of even-ranked processed in comm world */
Packit 0848f5
        err = MPI_Comm_create(evencomm, godd, &newcomm);
Packit 0848f5
        MPI_Group_rank(godd, &mygrank);
Packit 0848f5
        if (err == MPI_SUCCESS) {
Packit 0848f5
            if (mygrank != MPI_UNDEFINED) {
Packit 0848f5
                errs++;
Packit 0848f5
                fprintf(stderr, "Did not detect group of odd ranks in even comm\n");
Packit 0848f5
            }
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    if (wrank < wsize / 2) {
Packit 0848f5
        /* printf("low comm...\n"); */
Packit 0848f5
        err = MPI_Comm_create(lowcomm, ghigh, &newcomm);
Packit 0848f5
        MPI_Group_rank(ghigh, &mygrank);
Packit 0848f5
        if (err == MPI_SUCCESS) {
Packit 0848f5
            if (mygrank != MPI_UNDEFINED) {
Packit 0848f5
                errs++;
Packit 0848f5
                fprintf(stderr, "Did not detect group of high ranks in low comm\n");
Packit 0848f5
            }
Packit 0848f5
            MPI_Comm_free(&newcomm);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Comm_free(&lowcomm);
Packit 0848f5
    MPI_Comm_free(&evencomm);
Packit 0848f5
    MPI_Group_free(&ghigh);
Packit 0848f5
    MPI_Group_free(&godd);
Packit 0848f5
    MPI_Group_free(&geven);
Packit 0848f5
    MPI_Group_free(&wGroup);
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return 0;
Packit 0848f5
}