File: spawnintra.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (195 lines) | stat: -rw-r--r-- 6,556 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
#include "mpitest.h"

/*
static char MTEST_Descrip[] = "A simple test of Comm_spawn, followed by intercomm merge";
*/

int main(int argc, char *argv[])
{
    int errs = 0, err;
    int rank, size, rsize, i;
    int np = 2;
    int errcodes[2];
    MPI_Comm parentcomm, intercomm, intracomm, intracomm2, intracomm3;
    int isChild = 0;
    MPI_Status status;
    int can_spawn;

    MTest_Init(&argc, &argv);

    errs += MTestSpawnPossible(&can_spawn);

    if (can_spawn) {
        MPI_Comm_get_parent(&parentcomm);

        if (parentcomm == MPI_COMM_NULL) {
            /* Create 2 more processes */
            MPI_Comm_spawn((char *) "./spawnintra", MPI_ARGV_NULL, np,
                           MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm, errcodes);
        } else
            intercomm = parentcomm;

        /* We now have a valid intercomm */

        MPI_Comm_remote_size(intercomm, &rsize);
        MPI_Comm_size(intercomm, &size);
        MPI_Comm_rank(intercomm, &rank);

        if (parentcomm == MPI_COMM_NULL) {
            /* Parent */
            if (rsize != np) {
                errs++;
                printf("Did not create %d processes (got %d)\n", np, rsize);
            }
            if (rank == 0) {
                for (i = 0; i < rsize; i++) {
                    MPI_Send(&i, 1, MPI_INT, i, 0, intercomm);
                }
            }
        } else {
            /* Child */
            isChild = 1;
            if (size != np) {
                errs++;
                printf("(Child) Did not create %d processes (got %d)\n", np, size);
            }
            MPI_Recv(&i, 1, MPI_INT, 0, 0, intercomm, &status);
            if (i != rank) {
                errs++;
                printf("Unexpected rank on child %d (%d)\n", rank, i);
            }
        }

        /* At this point, try to form the intracommunicator */
        MPI_Intercomm_merge(intercomm, isChild, &intracomm);

        /* Check on the intra comm */
        {
            int icsize, icrank, wrank;

            MPI_Comm_size(intracomm, &icsize);
            MPI_Comm_rank(intracomm, &icrank);
            MPI_Comm_rank(MPI_COMM_WORLD, &wrank);

            if (icsize != rsize + size) {
                errs++;
                printf("Intracomm rank %d thinks size is %d, not %d\n",
                       icrank, icsize, rsize + size);
            }
            /* Make sure that the processes are ordered correctly */
            if (isChild) {
                int psize;
                MPI_Comm_remote_size(parentcomm, &psize);
                if (icrank != psize + wrank) {
                    errs++;
                    printf("Intracomm rank %d (from child) should have rank %d\n",
                           icrank, psize + wrank);
                }
            } else {
                if (icrank != wrank) {
                    errs++;
                    printf("Intracomm rank %d (from parent) should have rank %d\n", icrank, wrank);
                }
            }
        }

        /* At this point, try to form the intracommunicator, with the other
         * processes first */
        MPI_Intercomm_merge(intercomm, !isChild, &intracomm2);

        /* Check on the intra comm */
        {
            int icsize, icrank, wrank;

            MPI_Comm_size(intracomm2, &icsize);
            MPI_Comm_rank(intracomm2, &icrank);
            MPI_Comm_rank(MPI_COMM_WORLD, &wrank);

            if (icsize != rsize + size) {
                errs++;
                printf("(2)Intracomm rank %d thinks size is %d, not %d\n",
                       icrank, icsize, rsize + size);
            }
            /* Make sure that the processes are ordered correctly */
            if (isChild) {
                if (icrank != wrank) {
                    errs++;
                    printf("(2)Intracomm rank %d (from child) should have rank %d\n",
                           icrank, wrank);
                }
            } else {
                int csize;
                MPI_Comm_remote_size(intercomm, &csize);
                if (icrank != wrank + csize) {
                    errs++;
                    printf("(2)Intracomm rank %d (from parent) should have rank %d\n",
                           icrank, wrank + csize);
                }
            }
        }

        /* At this point, try to form the intracommunicator, with an
         * arbitrary choice for the first group of processes */
        MPI_Intercomm_merge(intercomm, 0, &intracomm3);
        /* Check on the intra comm */
        {
            int icsize, icrank, wrank;

            MPI_Comm_size(intracomm3, &icsize);
            MPI_Comm_rank(intracomm3, &icrank);
            MPI_Comm_rank(MPI_COMM_WORLD, &wrank);

            if (icsize != rsize + size) {
                errs++;
                printf("(3)Intracomm rank %d thinks size is %d, not %d\n",
                       icrank, icsize, rsize + size);
            }
            /* Eventually, we should test that the processes are ordered
             * correctly, by groups (must be one of the two cases above) */
        }

        /* Update error count */
        if (isChild) {
            /* Send the errs back to the parent process */
            MPI_Ssend(&errs, 1, MPI_INT, 0, 1, intercomm);
        } else {
            if (rank == 0) {
                /* We could use intercomm reduce to get the errors from the
                 * children, but we'll use a simpler loop to make sure that
                 * we get valid data */
                for (i = 0; i < rsize; i++) {
                    MPI_Recv(&err, 1, MPI_INT, i, 1, intercomm, MPI_STATUS_IGNORE);
                    errs += err;
                }
            }
        }

        /* It isn't necessary to free the intracomms, but it should not hurt */
        MPI_Comm_free(&intracomm);
        MPI_Comm_free(&intracomm2);
        MPI_Comm_free(&intracomm3);

        /* It isn't necessary to free the intercomm, but it should not hurt */
        MPI_Comm_free(&intercomm);

        /* Note that the MTest_Finalize get errs only over COMM_WORLD */
        /* Note also that both the parent and child will generate "No Errors"
         * if both call MTest_Finalize */
        if (parentcomm == MPI_COMM_NULL) {
            MTest_Finalize(errs);
        } else {
            MPI_Finalize();
        }
    } else {
        MTest_Finalize(errs);
    }

    return MTestReturnValue(errs);
}