1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2007 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
11 /* Check that Communicators can be created from various subsets of the
12 processes in the communicator.
15 void abortMsg(const char *, int);
16 int BuildComm(MPI_Comm, MPI_Group, const char[]);
18 void abortMsg(const char *str, int code)
20 char msg[MPI_MAX_ERROR_STRING];
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);
29 int main(int argc, char *argv[])
32 int wrank, wsize, gsize, err, errs = 0;
34 MPI_Group wGroup, godd, ghigh, geven;
36 MTest_Init(&argc, &argv);
38 MPI_Comm_size(MPI_COMM_WORLD, &wsize);
39 MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
41 /* Create some groups */
42 MPI_Comm_group(MPI_COMM_WORLD, &wGroup);
44 MTestPrintfMsg(2, "Creating groups\n");
45 ranges[0][0] = 2 * (wsize / 2) - 1;
48 err = MPI_Group_range_incl(wGroup, 1, ranges, &godd);
50 abortMsg("Failed to create odd group: ", err);
51 err = MPI_Group_size(godd, &gsize);
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);
59 ranges[0][0] = wsize / 2 + 1;
60 ranges[0][1] = wsize - 1;
62 err = MPI_Group_range_incl(wGroup, 1, ranges, &ghigh);
64 abortMsg("Failed to create high group\n", err);
66 ranges[0][1] = wsize - 1;
68 err = MPI_Group_range_incl(wGroup, 1, ranges, &geven);
70 abortMsg("Failed to create even group:", err);
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
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");
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");
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);
104 int BuildComm(MPI_Comm oldcomm, MPI_Group group, const char gname[])
107 int grank, gsize, rank, size, errs = 0;
108 char cname[MPI_MAX_OBJECT_NAME + 1];
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) {
118 fprintf(stderr, "newcomm is null but process is in group\n");
120 if (newcomm != MPI_COMM_NULL && grank == MPI_UNDEFINED) {
122 fprintf(stderr, "newcomm is not null but process is not in group\n");
124 if (newcomm != MPI_COMM_NULL && grank != MPI_UNDEFINED) {
125 MPI_Comm_rank(newcomm, &rank);
128 fprintf(stderr, "Rank is %d should be %d in comm from %s\n", rank, grank, gname);
130 MPI_Comm_size(newcomm, &size);
133 fprintf(stderr, "Size is %d should be %d in comm from %s\n", size, gsize, gname);
135 MPI_Comm_free(&newcomm);
136 MTestPrintfMsg(2, "Done testing comm %s from %s\n", cname, gname);