Blame test/mpi/group/grouptest2.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
   Test the group routines
Packit 0848f5
   (some tested elsewere)
Packit 0848f5
Packit 0848f5
MPI_Group_compare
Packit 0848f5
MPI_Group_excl
Packit 0848f5
MPI_Group_intersection
Packit 0848f5
MPI_Group_range_excl
Packit 0848f5
MPI_Group_rank
Packit 0848f5
MPI_Group_size
Packit 0848f5
MPI_Group_translate_ranks
Packit 0848f5
MPI_Group_union
Packit 0848f5
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
/* stdlib.h Needed for malloc declaration */
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int errs = 0, toterr;
Packit 0848f5
    MPI_Group basegroup;
Packit 0848f5
    MPI_Group g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;
Packit 0848f5
    MPI_Group g3a, g3b;
Packit 0848f5
    MPI_Comm comm, newcomm, splitcomm, dupcomm;
Packit 0848f5
    int i, grp_rank, rank, grp_size, size, result;
Packit 0848f5
    int nranks, *ranks, *ranks_out;
Packit 0848f5
    int range[1][3];
Packit 0848f5
    int worldrank;
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &worldrank);
Packit 0848f5
Packit 0848f5
    comm = MPI_COMM_WORLD;
Packit 0848f5
Packit 0848f5
    MPI_Comm_group(comm, &basegroup);
Packit 0848f5
    MPI_Comm_rank(comm, &rank;;
Packit 0848f5
    MPI_Comm_size(comm, &size);
Packit 0848f5
Packit 0848f5
/* Get the basic information on this group */
Packit 0848f5
    MPI_Group_rank(basegroup, &grp_rank);
Packit 0848f5
    if (grp_rank != rank) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "group rank %d != comm rank %d\n", grp_rank, rank);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Group_size(basegroup, &grp_size);
Packit 0848f5
    if (grp_size != size) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "group size %d != comm size %d\n", grp_size, size);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
Packit 0848f5
/* Form a new communicator with inverted ranking */
Packit 0848f5
    MPI_Comm_split(comm, 0, size - rank, &newcomm);
Packit 0848f5
    MPI_Comm_group(newcomm, &g1;;
Packit 0848f5
    ranks = (int *) malloc(size * sizeof(int));
Packit 0848f5
    ranks_out = (int *) malloc(size * sizeof(int));
Packit 0848f5
    for (i = 0; i < size; i++)
Packit 0848f5
        ranks[i] = i;
Packit 0848f5
    nranks = size;
Packit 0848f5
    MPI_Group_translate_ranks(g1, nranks, ranks, basegroup, ranks_out);
Packit 0848f5
    for (i = 0; i < size; i++) {
Packit 0848f5
        if (ranks_out[i] != (size - 1) - i) {
Packit 0848f5
            errs++;
Packit 0848f5
            fprintf(stdout, "Translate ranks got %d expected %d\n", ranks_out[i], (size - 1) - i);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
/* Check Compare */
Packit 0848f5
    MPI_Group_compare(basegroup, g1, &result);
Packit 0848f5
    if (result != MPI_SIMILAR) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group compare should have been similar, was %d\n", result);
Packit 0848f5
    }
Packit 0848f5
    MPI_Comm_dup(comm, &dupcomm);
Packit 0848f5
    MPI_Comm_group(dupcomm, &g2;;
Packit 0848f5
    MPI_Group_compare(basegroup, g2, &result);
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group compare should have been ident, was %d\n", result);
Packit 0848f5
    }
Packit 0848f5
    MPI_Comm_split(comm, rank < size / 2, rank, &splitcomm);
Packit 0848f5
    MPI_Comm_group(splitcomm, &g3;;
Packit 0848f5
    MPI_Group_compare(basegroup, g3, &result);
Packit 0848f5
    if (result != MPI_UNEQUAL) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group compare should have been unequal, was %d\n", result);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* Build two groups that have this process and one other, but do not
Packit 0848f5
     * have the same processes */
Packit 0848f5
    ranks[0] = rank;
Packit 0848f5
    ranks[1] = (rank + 1) % size;
Packit 0848f5
    MPI_Group_incl(basegroup, 2, ranks, &g3a);
Packit 0848f5
    ranks[1] = (rank + size - 1) % size;
Packit 0848f5
    MPI_Group_incl(basegroup, 2, ranks, &g3b);
Packit 0848f5
    MPI_Group_compare(g3a, g3b, &result);
Packit 0848f5
    if (result != MPI_UNEQUAL) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout,
Packit 0848f5
                "Group compare of equal sized but different groups should have been unequal, was %d\n",
Packit 0848f5
                result);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
Packit 0848f5
/* Build two new groups by excluding members; use Union to put them
Packit 0848f5
   together again */
Packit 0848f5
Packit 0848f5
/* Exclude 0 */
Packit 0848f5
    for (i = 0; i < size; i++)
Packit 0848f5
        ranks[i] = i;
Packit 0848f5
    MPI_Group_excl(basegroup, 1, ranks, &g4;;
Packit 0848f5
/* Exclude 1-(size-1) */
Packit 0848f5
    MPI_Group_excl(basegroup, size - 1, ranks + 1, &g5;;
Packit 0848f5
    MPI_Group_union(g5, g4, &g6;;
Packit 0848f5
    MPI_Group_compare(basegroup, g6, &result);
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        int usize;
Packit 0848f5
        errs++;
Packit 0848f5
        /* See ordering requirements on union */
Packit 0848f5
        fprintf(stdout, "Group excl and union did not give ident groups\n");
Packit 0848f5
        fprintf(stdout, "[%d] result of compare was %d\n", rank, result);
Packit 0848f5
        MPI_Group_size(g6, &usize);
Packit 0848f5
        fprintf(stdout, "Size of union is %d, should be %d\n", usize, size);
Packit 0848f5
    }
Packit 0848f5
    MPI_Group_union(basegroup, g4, &g7;;
Packit 0848f5
    MPI_Group_compare(basegroup, g7, &result);
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        int usize;
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group union of overlapping groups failed\n");
Packit 0848f5
        fprintf(stdout, "[%d] result of compare was %d\n", rank, result);
Packit 0848f5
        MPI_Group_size(g7, &usize);
Packit 0848f5
        fprintf(stdout, "Size of union is %d, should be %d\n", usize, size);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
/* Use range_excl instead of ranks */
Packit 0848f5
    /* printf ("range excl\n"); fflush(stdout); */
Packit 0848f5
    range[0][0] = 1;
Packit 0848f5
    range[0][1] = size - 1;
Packit 0848f5
    range[0][2] = 1;
Packit 0848f5
    MPI_Group_range_excl(basegroup, 1, range, &g8;;
Packit 0848f5
    /* printf("out  of range excl\n"); fflush(stdout); */
Packit 0848f5
    MPI_Group_compare(g5, g8, &result);
Packit 0848f5
    /* printf("out of compare\n"); fflush(stdout); */
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group range excl did not give ident groups\n");
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* printf("intersection\n"); fflush(stdout); */
Packit 0848f5
    MPI_Group_intersection(basegroup, g4, &g9;;
Packit 0848f5
    MPI_Group_compare(g9, g4, &result);
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "Group intersection did not give ident groups\n");
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
/* Exclude EVERYTHING and check against MPI_GROUP_EMPTY */
Packit 0848f5
    /* printf("range excl all\n"); fflush(stdout); */
Packit 0848f5
    range[0][0] = 0;
Packit 0848f5
    range[0][1] = size - 1;
Packit 0848f5
    range[0][2] = 1;
Packit 0848f5
    MPI_Group_range_excl(basegroup, 1, range, &g10);
Packit 0848f5
Packit 0848f5
    /* printf("done range excl all\n"); fflush(stdout); */
Packit 0848f5
    MPI_Group_compare(g10, MPI_GROUP_EMPTY, &result);
Packit 0848f5
    /* printf("done compare to MPI_GROUP_EMPTY\n"); fflush(stdout); */
Packit 0848f5
Packit 0848f5
    if (result != MPI_IDENT) {
Packit 0848f5
        errs++;
Packit 0848f5
        fprintf(stdout, "MPI_GROUP_EMPTY didn't compare against empty group\n");
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* printf("freeing groups\n"); fflush(stdout); */
Packit 0848f5
    MPI_Group_free(&basegroup);
Packit 0848f5
    MPI_Group_free(&g1;;
Packit 0848f5
    MPI_Group_free(&g2;;
Packit 0848f5
    MPI_Group_free(&g3;;
Packit 0848f5
    MPI_Group_free(&g3a);
Packit 0848f5
    MPI_Group_free(&g3b);
Packit 0848f5
    MPI_Group_free(&g4;;
Packit 0848f5
    MPI_Group_free(&g5;;
Packit 0848f5
    MPI_Group_free(&g6;;
Packit 0848f5
    MPI_Group_free(&g7;;
Packit 0848f5
    MPI_Group_free(&g8;;
Packit 0848f5
    MPI_Group_free(&g9;;
Packit 0848f5
    MPI_Group_free(&g10);
Packit 0848f5
    MPI_Comm_free(&dupcomm);
Packit 0848f5
    MPI_Comm_free(&splitcomm);
Packit 0848f5
    MPI_Comm_free(&newcomm);
Packit 0848f5
Packit 0848f5
    MPI_Allreduce(&errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
Packit 0848f5
    if (worldrank == 0) {
Packit 0848f5
        if (toterr == 0)
Packit 0848f5
            printf(" No Errors\n");
Packit 0848f5
        else
Packit 0848f5
            printf("Found %d errors in MPI Group routines\n", toterr);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    free(ranks);
Packit 0848f5
    free(ranks_out);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return toterr;
Packit 0848f5
}