Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try to avoid rare bug on one ci node.
[simgrid.git] / teshsuite / smpi / mpich3-test / topo / topodup.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 int main(int argc, char *argv[])
13 {
14     int errs = 0, i, k;
15     int dims[2], periods[2], wsize;
16     int outdims[2], outperiods[2], outcoords[2];
17     int topo_type;
18     int *index, *edges, *outindex, *outedges;
19     MPI_Comm comm1, comm2;
20
21     MTest_Init(&argc, &argv);
22
23     MPI_Comm_size(MPI_COMM_WORLD, &wsize);
24
25     /* Create a cartesian topology, get its characteristics, then
26      * dup it and check that the new communicator has the same properties */
27     dims[0] = dims[1] = 0;
28     MPI_Dims_create(wsize, 2, dims);
29     periods[0] = periods[1] = 0;
30     MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, 0, &comm1);
31
32     MPI_Comm_dup(comm1, &comm2);
33     MPI_Topo_test(comm2, &topo_type);
34     if (topo_type != MPI_CART) {
35         errs++;
36         printf("Topo type of duped cart was not cart\n");
37     }
38     else {
39         MPI_Cart_get(comm2, 2, outdims, outperiods, outcoords);
40         for (i = 0; i < 2; i++) {
41             if (outdims[i] != dims[i]) {
42                 errs++;
43                 printf("%d = outdims[%d] != dims[%d] = %d\n", outdims[i], i, i, dims[i]);
44             }
45             if (outperiods[i] != periods[i]) {
46                 errs++;
47                 printf("%d = outperiods[%d] != periods[%d] = %d\n",
48                        outperiods[i], i, i, periods[i]);
49             }
50         }
51     }
52     MPI_Comm_free(&comm2);
53     MPI_Comm_free(&comm1);
54
55     /* Now do the same with a graph topology */
56     if (wsize >= 3) {
57         index = (int *) malloc(wsize * sizeof(int));
58         edges = (int *) malloc(wsize * 2 * sizeof(int));
59         if (!index || !edges) {
60             printf("Unable to allocate %d words for index or edges\n", 3 * wsize);
61             MPI_Abort(MPI_COMM_WORLD, 1);
62         }
63         index[0] = 2;
64         for (i = 1; i < wsize; i++) {
65             index[i] = 2 + index[i - 1];
66         }
67         k = 0;
68         for (i = 0; i < wsize; i++) {
69             edges[k++] = (i - 1 + wsize) % wsize;
70             edges[k++] = (i + 1) % wsize;
71         }
72         MPI_Graph_create(MPI_COMM_WORLD, wsize, index, edges, 0, &comm1);
73         MPI_Comm_dup(comm1, &comm2);
74         MPI_Topo_test(comm2, &topo_type);
75         if (topo_type != MPI_GRAPH) {
76             errs++;
77             printf("Topo type of duped graph was not graph\n");
78         }
79         else {
80             int nnodes, nedges;
81             MPI_Graphdims_get(comm2, &nnodes, &nedges);
82             if (nnodes != wsize) {
83                 errs++;
84                 printf("Nnodes = %d, should be %d\n", nnodes, wsize);
85             }
86             if (nedges != 2 * wsize) {
87                 errs++;
88                 printf("Nedges = %d, should be %d\n", nedges, 2 * wsize);
89             }
90             outindex = (int *) malloc(wsize * sizeof(int));
91             outedges = (int *) malloc(wsize * 2 * sizeof(int));
92             if (!outindex || !outedges) {
93                 printf("Unable to allocate %d words for outindex or outedges\n", 3 * wsize);
94                 MPI_Abort(MPI_COMM_WORLD, 1);
95             }
96
97             MPI_Graph_get(comm2, wsize, 2 * wsize, outindex, outedges);
98             for (i = 0; i < wsize; i++) {
99                 if (index[i] != outindex[i]) {
100                     printf("%d = index[%d] != outindex[%d] = %d\n", index[i], i, i, outindex[i]);
101                     errs++;
102                 }
103             }
104             for (i = 0; i < 2 * wsize; i++) {
105                 if (edges[i] != outedges[i]) {
106                     printf("%d = edges[%d] != outedges[%d] = %d\n", edges[i], i, i, outedges[i]);
107                     errs++;
108                 }
109             }
110             free(outindex);
111             free(outedges);
112         }
113         free(index);
114         free(edges);
115
116         MPI_Comm_free(&comm2);
117         MPI_Comm_free(&comm1);
118     }
119
120     MTest_Finalize(errs);
121     MPI_Finalize();
122     return 0;
123
124 }