File: topodup.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (119 lines) | stat: -rw-r--r-- 3,954 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include "mpitest.h"

int main(int argc, char *argv[])
{
    int errs = 0, i, k;
    int dims[2], periods[2], wsize;
    int outdims[2], outperiods[2], outcoords[2];
    int topo_type;
    int *index, *edges, *outindex, *outedges;
    MPI_Comm comm1, comm2;

    MTest_Init(&argc, &argv);

    MPI_Comm_size(MPI_COMM_WORLD, &wsize);

    /* Create a cartesian topology, get its characteristics, then
     * dup it and check that the new communicator has the same properties */
    dims[0] = dims[1] = 0;
    MPI_Dims_create(wsize, 2, dims);
    periods[0] = periods[1] = 0;
    MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 0, &comm1);

    MPI_Comm_dup(comm1, &comm2);
    MPI_Topo_test(comm2, &topo_type);
    if (topo_type != MPI_CART) {
        errs++;
        printf("Topo type of duped cart was not cart\n");
    } else {
        MPI_Cart_get(comm2, 2, outdims, outperiods, outcoords);
        for (i = 0; i < 2; i++) {
            if (outdims[i] != dims[i]) {
                errs++;
                printf("%d = outdims[%d] != dims[%d] = %d\n", outdims[i], i, i, dims[i]);
            }
            if (outperiods[i] != periods[i]) {
                errs++;
                printf("%d = outperiods[%d] != periods[%d] = %d\n",
                       outperiods[i], i, i, periods[i]);
            }
        }
    }
    MPI_Comm_free(&comm2);
    MPI_Comm_free(&comm1);

    /* Now do the same with a graph topology */
    if (wsize >= 3) {
        index = (int *) malloc(wsize * sizeof(int));
        edges = (int *) malloc(wsize * 2 * sizeof(int));
        if (!index || !edges) {
            printf("Unable to allocate %d words for index or edges\n", 3 * wsize);
            MPI_Abort(MPI_COMM_WORLD, 1);
        }
        index[0] = 2;
        for (i = 1; i < wsize; i++) {
            index[i] = 2 + index[i - 1];
        }
        k = 0;
        for (i = 0; i < wsize; i++) {
            edges[k++] = (i - 1 + wsize) % wsize;
            edges[k++] = (i + 1) % wsize;
        }
        MPI_Graph_create(MPI_COMM_WORLD, wsize, index, edges, 0, &comm1);
        MPI_Comm_dup(comm1, &comm2);
        MPI_Topo_test(comm2, &topo_type);
        if (topo_type != MPI_GRAPH) {
            errs++;
            printf("Topo type of duped graph was not graph\n");
        } else {
            int nnodes, nedges;
            MPI_Graphdims_get(comm2, &nnodes, &nedges);
            if (nnodes != wsize) {
                errs++;
                printf("Nnodes = %d, should be %d\n", nnodes, wsize);
            }
            if (nedges != 2 * wsize) {
                errs++;
                printf("Nedges = %d, should be %d\n", nedges, 2 * wsize);
            }
            outindex = (int *) malloc(wsize * sizeof(int));
            outedges = (int *) malloc(wsize * 2 * sizeof(int));
            if (!outindex || !outedges) {
                printf("Unable to allocate %d words for outindex or outedges\n", 3 * wsize);
                MPI_Abort(MPI_COMM_WORLD, 1);
            }

            MPI_Graph_get(comm2, wsize, 2 * wsize, outindex, outedges);
            for (i = 0; i < wsize; i++) {
                if (index[i] != outindex[i]) {
                    printf("%d = index[%d] != outindex[%d] = %d\n", index[i], i, i, outindex[i]);
                    errs++;
                }
            }
            for (i = 0; i < 2 * wsize; i++) {
                if (edges[i] != outedges[i]) {
                    printf("%d = edges[%d] != outedges[%d] = %d\n", edges[i], i, i, outedges[i]);
                    errs++;
                }
            }
            free(outindex);
            free(outedges);
        }
        free(index);
        free(edges);

        MPI_Comm_free(&comm2);
        MPI_Comm_free(&comm1);
    }

    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}