Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1c5515ff19bca3e47cd04a64a43a78da52a419f
[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],
44                         i, i, dims[i] );
45             }
46             if (outperiods[i] != periods[i]) {
47                 errs++;
48                 printf( "%d = outperiods[%d] != periods[%d] = %d\n", 
49                         outperiods[i], i, i, periods[i] );
50             }
51         }
52     }
53     MPI_Comm_free( &comm2 );
54     MPI_Comm_free( &comm1 );
55
56     /* Now do the same with a graph topology */
57     if (wsize >= 3) {
58         index = (int*)malloc(wsize * sizeof(int) );
59         edges = (int*)malloc(wsize * 2 * sizeof(int) );
60         if (!index || !edges) {
61             printf( "Unable to allocate %d words for index or edges\n", 
62                     3 * wsize );
63             MPI_Abort( MPI_COMM_WORLD, 1 );
64         }
65         index[0] = 2;
66         for (i=1; i<wsize; i++) {
67             index[i] = 2 + index[i-1];
68         }
69         k=0;
70         for (i=0; i<wsize; i++) {
71             edges[k++] = (i-1+wsize) % wsize;
72             edges[k++] = (i+1) % wsize;
73         }
74         MPI_Graph_create( MPI_COMM_WORLD, wsize, index, edges, 0, &comm1 );
75         MPI_Comm_dup( comm1, &comm2 );
76         MPI_Topo_test( comm2, &topo_type );
77         if (topo_type != MPI_GRAPH) {
78             errs++;
79             printf( "Topo type of duped graph was not graph\n" );
80         }
81         else {
82             int nnodes, nedges;
83             MPI_Graphdims_get( comm2, &nnodes, &nedges );
84             if (nnodes != wsize) {
85                 errs++;
86                 printf( "Nnodes = %d, should be %d\n", nnodes, wsize );
87             }
88             if (nedges != 2*wsize) {
89                 errs++;
90                 printf( "Nedges = %d, should be %d\n", nedges, 2*wsize );
91             }
92             outindex = (int*)malloc(wsize * sizeof(int) );
93             outedges = (int*)malloc(wsize * 2 * sizeof(int) );
94             if (!outindex || !outedges) {
95                 printf( "Unable to allocate %d words for outindex or outedges\n", 
96                         3 * wsize );
97                 MPI_Abort( MPI_COMM_WORLD, 1 );
98             }
99             
100             MPI_Graph_get( comm2, wsize, 2*wsize, outindex, outedges );
101             for (i=0; i<wsize; i++) {
102                 if (index[i] != outindex[i]) {
103                     printf( "%d = index[%d] != outindex[%d] = %d\n",
104                             index[i], i, i, outindex[i] );
105                     errs++;
106                 }
107             }
108             for (i=0; i<2*wsize; i++) {
109                 if (edges[i] != outedges[i]) {
110                     printf( "%d = edges[%d] != outedges[%d] = %d\n",
111                             edges[i], i, i, outedges[i] );
112                     errs++;
113                 }
114             }
115             free( outindex );
116             free( outedges );
117         }
118         free( index );
119         free( edges );
120
121         MPI_Comm_free( &comm2 );
122         MPI_Comm_free( &comm1 );
123     }
124     
125     MTest_Finalize( errs );
126     MPI_Finalize();
127     return 0;
128   
129 }