Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
functioning MPI_Comm_get_name, MPI_Comm_set_name
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / icsplit.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2007 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 /*
13  * This program tests that MPI_Comm_split applies to intercommunicators;
14  * this is an extension added in MPI-2
15  */
16 int main(int argc, char *argv[])
17 {
18     int errs = 0;
19     int size, isLeft;
20     MPI_Comm intercomm, newcomm;
21
22     MTest_Init(&argc, &argv);
23
24     MPI_Comm_size(MPI_COMM_WORLD, &size);
25     if (size < 4) {
26         printf("This test requires at least 4 processes\n");
27         MPI_Abort(MPI_COMM_WORLD, 1);
28     }
29
30     while (MTestGetIntercomm(&intercomm, &isLeft, 2)) {
31         int key, color;
32
33         if (intercomm == MPI_COMM_NULL)
34             continue;
35
36         /* Split this intercomm.  The new intercomms contain the
37          * processes that had odd (resp even) rank in their local group
38          * in the original intercomm */
39         MTestPrintfMsg(1, "Created intercomm %s\n", MTestGetIntercommName());
40         MPI_Comm_rank(intercomm, &key);
41         color = (key % 2);
42         MPI_Comm_split(intercomm, color, key, &newcomm);
43         /* Make sure that the new communicator has the appropriate pieces */
44         if (newcomm != MPI_COMM_NULL) {
45             int orig_rsize, orig_size, new_rsize, new_size;
46             int predicted_size, flag, commok = 1;
47
48             MPI_Comm_test_inter(intercomm, &flag);
49             if (!flag) {
50                 errs++;
51                 printf("Output communicator is not an intercomm\n");
52                 commok = 0;
53             }
54
55             MPI_Comm_remote_size(intercomm, &orig_rsize);
56             MPI_Comm_remote_size(newcomm, &new_rsize);
57             MPI_Comm_size(intercomm, &orig_size);
58             MPI_Comm_size(newcomm, &new_size);
59             /* The local size is 1/2 the original size, +1 if the
60              * size was odd and the color was even.  More precisely,
61              * let n be the orig_size.  Then
62              * color 0     color 1
63              * orig size even    n/2         n/2
64              * orig size odd     (n+1)/2     n/2
65              *
66              * However, since these are integer valued, if n is even,
67              * then (n+1)/2 = n/2, so this table is much simpler:
68              * color 0     color 1
69              * orig size even    (n+1)/2     n/2
70              * orig size odd     (n+1)/2     n/2
71              *
72              */
73             predicted_size = (orig_size + !color) / 2;
74             if (predicted_size != new_size) {
75                 errs++;
76                 printf("Predicted size = %d but found %d for %s (%d,%d)\n",
77                        predicted_size, new_size, MTestGetIntercommName(), orig_size, orig_rsize);
78                 commok = 0;
79             }
80             predicted_size = (orig_rsize + !color) / 2;
81             if (predicted_size != new_rsize) {
82                 errs++;
83                 printf("Predicted remote size = %d but found %d for %s (%d,%d)\n",
84                        predicted_size, new_rsize, MTestGetIntercommName(), orig_size, orig_rsize);
85                 commok = 0;
86             }
87             /* ... more to do */
88             if (commok) {
89                 errs += MTestTestComm(newcomm);
90             }
91         }
92         else {
93             int orig_rsize;
94             /* If the newcomm is null, then this means that remote group
95              * for this color is of size zero (since all processes in this
96              * test have been given colors other than MPI_UNDEFINED).
97              * Confirm that here */
98             /* FIXME: ToDo */
99             MPI_Comm_remote_size(intercomm, &orig_rsize);
100             if (orig_rsize == 1) {
101                 if (color == 0) {
102                     errs++;
103                     printf("Returned null intercomm when non-null expected\n");
104                 }
105             }
106         }
107         if (newcomm != MPI_COMM_NULL)
108             MPI_Comm_free(&newcomm);
109         MPI_Comm_free(&intercomm);
110     }
111     MTest_Finalize(errs);
112
113     MPI_Finalize();
114
115     return 0;
116 }