Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce some tests duration as comm_create takes longer now (comms)
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / commcreate1.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2007 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <string.h>
9 #include "mpitest.h"
10
11 /* Check that Communicators can be created from various subsets of the
12    processes in the communicator.
13 */
14
15 void abortMsg(const char *, int);
16 int BuildComm(MPI_Comm, MPI_Group, const char[]);
17
18 void abortMsg(const char *str, int code)
19 {
20     char msg[MPI_MAX_ERROR_STRING];
21     int class, resultLen;
22
23     MPI_Error_class(code, &class);
24     MPI_Error_string(code, msg, &resultLen);
25     fprintf(stderr, "%s: errcode = %d, class = %d, msg = %s\n", str, code, class, msg);
26     MPI_Abort(MPI_COMM_WORLD, code);
27 }
28
29 int main(int argc, char *argv[])
30 {
31     MPI_Comm dupWorld;
32     int wrank, wsize, gsize, err, errs = 0;
33     int ranges[1][3];
34     MPI_Group wGroup, godd, ghigh, geven;
35
36     MTest_Init(&argc, &argv);
37
38     MPI_Comm_size(MPI_COMM_WORLD, &wsize);
39     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
40
41     /* Create some groups */
42     MPI_Comm_group(MPI_COMM_WORLD, &wGroup);
43
44     MTestPrintfMsg(2, "Creating groups\n");
45     ranges[0][0] = 2 * (wsize / 2) - 1;
46     ranges[0][1] = 1;
47     ranges[0][2] = -2;
48     err = MPI_Group_range_incl(wGroup, 1, ranges, &godd);
49     if (err)
50         abortMsg("Failed to create odd group: ", err);
51     err = MPI_Group_size(godd, &gsize);
52     if (err)
53         abortMsg("Failed to get size of odd group: ", err);
54     if (gsize != wsize / 2) {
55         fprintf(stderr, "Group godd size is %d should be %d\n", gsize, wsize / 2);
56         errs++;
57     }
58
59     ranges[0][0] = wsize / 2 + 1;
60     ranges[0][1] = wsize - 1;
61     ranges[0][2] = 1;
62     err = MPI_Group_range_incl(wGroup, 1, ranges, &ghigh);
63     if (err)
64         abortMsg("Failed to create high group\n", err);
65     ranges[0][0] = 0;
66     ranges[0][1] = wsize - 1;
67     ranges[0][2] = 2;
68     err = MPI_Group_range_incl(wGroup, 1, ranges, &geven);
69     if (err)
70         abortMsg("Failed to create even group:", err);
71
72     MPI_Comm_dup(MPI_COMM_WORLD, &dupWorld);
73     MPI_Comm_set_name(dupWorld, (char *) "Dup of world");
74     /* First, use the groups to create communicators from world and a dup
75      * of world */
76     errs += BuildComm(MPI_COMM_WORLD, ghigh, "ghigh");
77     errs += BuildComm(MPI_COMM_WORLD, godd, "godd");
78     errs += BuildComm(MPI_COMM_WORLD, geven, "geven");
79     errs += BuildComm(dupWorld, ghigh, "ghigh");
80     errs += BuildComm(dupWorld, godd, "godd");
81     errs += BuildComm(dupWorld, geven, "geven");
82
83 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
84     /* check that we can create multiple communicators from a single collective
85      * call to MPI_Comm_create as long as the groups are all disjoint */
86     errs += BuildComm(MPI_COMM_WORLD, (wrank % 2 ? godd : geven), "godd+geven");
87     errs += BuildComm(dupWorld, (wrank % 2 ? godd : geven), "godd+geven");
88     errs += BuildComm(MPI_COMM_WORLD, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY");
89     errs += BuildComm(dupWorld, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY");
90 #endif
91
92     MPI_Comm_free(&dupWorld);
93     MPI_Group_free(&ghigh);
94     MPI_Group_free(&godd);
95     MPI_Group_free(&geven);
96     MPI_Group_free(&wGroup);
97
98     MTest_Finalize(errs);
99
100     MPI_Finalize();
101     return 0;
102 }
103
104 int BuildComm(MPI_Comm oldcomm, MPI_Group group, const char gname[])
105 {
106     MPI_Comm newcomm;
107     int grank, gsize, rank, size, errs = 0;
108     char cname[MPI_MAX_OBJECT_NAME + 1];
109     int cnamelen;
110
111     MPI_Group_rank(group, &grank);
112     MPI_Group_size(group, &gsize);
113     MPI_Comm_get_name(oldcomm, cname, &cnamelen);
114     MTestPrintfMsg(2, "Testing comm %s from %s\n", cname, gname);
115     MPI_Comm_create(oldcomm, group, &newcomm);
116     if (newcomm == MPI_COMM_NULL && grank != MPI_UNDEFINED) {
117         errs++;
118         fprintf(stderr, "newcomm is null but process is in group\n");
119     }
120     if (newcomm != MPI_COMM_NULL && grank == MPI_UNDEFINED) {
121         errs++;
122         fprintf(stderr, "newcomm is not null but process is not in group\n");
123     }
124     if (newcomm != MPI_COMM_NULL && grank != MPI_UNDEFINED) {
125         MPI_Comm_rank(newcomm, &rank);
126         if (rank != grank) {
127             errs++;
128             fprintf(stderr, "Rank is %d should be %d in comm from %s\n", rank, grank, gname);
129         }
130         MPI_Comm_size(newcomm, &size);
131         if (size != gsize) {
132             errs++;
133             fprintf(stderr, "Size is %d should be %d in comm from %s\n", size, gsize, gname);
134         }
135         MPI_Comm_free(&newcomm);
136         MTestPrintfMsg(2, "Done testing comm %s from %s\n", cname, gname);
137     }
138     return errs;
139 }