1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2007 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 * This program tests that MPI_Comm_split applies to intercommunicators;
14 * this is an extension added in MPI-2
16 int main(int argc, char *argv[])
20 MPI_Comm intercomm, newcomm;
22 MTest_Init(&argc, &argv);
24 MPI_Comm_size(MPI_COMM_WORLD, &size);
26 printf("This test requires at least 4 processes\n");
27 MPI_Abort(MPI_COMM_WORLD, 1);
30 while (MTestGetIntercomm(&intercomm, &isLeft, 2)) {
33 if (intercomm == MPI_COMM_NULL)
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);
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;
48 MPI_Comm_test_inter(intercomm, &flag);
51 printf("Output communicator is not an intercomm\n");
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
63 * orig size even n/2 n/2
64 * orig size odd (n+1)/2 n/2
66 * However, since these are integer valued, if n is even,
67 * then (n+1)/2 = n/2, so this table is much simpler:
69 * orig size even (n+1)/2 n/2
70 * orig size odd (n+1)/2 n/2
73 predicted_size = (orig_size + !color) / 2;
74 if (predicted_size != new_size) {
76 printf("Predicted size = %d but found %d for %s (%d,%d)\n",
77 predicted_size, new_size, MTestGetIntercommName(), orig_size, orig_rsize);
80 predicted_size = (orig_rsize + !color) / 2;
81 if (predicted_size != new_rsize) {
83 printf("Predicted remote size = %d but found %d for %s (%d,%d)\n",
84 predicted_size, new_rsize, MTestGetIntercommName(), orig_size, orig_rsize);
89 errs += MTestTestComm(newcomm);
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 */
99 MPI_Comm_remote_size(intercomm, &orig_rsize);
100 if (orig_rsize == 1) {
103 printf("Returned null intercomm when non-null expected\n");
107 if (newcomm != MPI_COMM_NULL)
108 MPI_Comm_free(&newcomm);
109 MPI_Comm_free(&intercomm);
111 MTest_Finalize(errs);