File: concurrent_spawns.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 (128 lines) | stat: -rw-r--r-- 4,002 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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include "mpitest.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

/* #include <fcntl.h> */

/* This test tests the ability to handle multiple simultaneous calls to
 * MPI_Comm_spawn.
 * The first process spawns MAX_NUM_SPAWNS processes and tells them to spawn
 * MAX_NUM_SPAWNS -1 processes.
 * This repeats until no processes are spawned.  For MAX_NUM_SPAWNS = 4 this
 * results in 64 processes
 * created.  Hopefully this will result in concurrent handling of
 * MPI_Comm_spawn calls from multiple processes.
 */

#define IF_VERBOSE(a) if (verbose) { printf a ; fflush(stdout); }

/*
static char MTEST_Descrip[] = "A test of concurrent MPI_Comm_spawn calls";
*/

#define MAX_NUM_SPAWNS 4

int main(int argc, char *argv[])
{
    int errs = 0;
    int child_errs = 0;
    int size, rsize, i, num_spawns = 0;
    char description[100];
    char child_spawns[10];
    char *argv1[3] = { child_spawns, description, NULL };
    MPI_Comm parentcomm, intercomm[MAX_NUM_SPAWNS];
    MPI_Status status;
    int verbose = 0;
    char *env;
    int can_spawn;

    env = getenv("MPITEST_VERBOSE");
    if (env) {
        if (*env != '0')
            verbose = 1;
    }

    MTest_Init(&argc, &argv);

    errs += MTestSpawnPossible(&can_spawn);

    if (can_spawn) {
        /* Set the num_spawns for the first process to MAX_NUM_SPAWNS */
        MPI_Comm_get_parent(&parentcomm);
        if (parentcomm == MPI_COMM_NULL) {
            num_spawns = MAX_NUM_SPAWNS;
        }

        /* If an argument is passed in use it for num_spawns */
        /* This is the case for all spawned processes and optionally the first
         * process as well */
        if (argc > 1) {
            num_spawns = atoi(argv[1]);
            if (num_spawns < 0)
                num_spawns = 0;
            if (num_spawns > MAX_NUM_SPAWNS)
                num_spawns = MAX_NUM_SPAWNS;
        }

        /* Send num_spawns - 1 on the command line to the spawned children */
        sprintf(child_spawns, "%d", num_spawns - 1 > 0 ? num_spawns - 1 : 0);

        /* Spawn the children */
        IF_VERBOSE(("spawning %d\n", num_spawns));
        for (i = 0; i < num_spawns; i++) {
            if (argc > 2) {
                sprintf(description, "%s:%d", argv[2], i);
            } else {
                sprintf(description, "%d", i);
            }
            IF_VERBOSE(("spawning %s\n", description));
            MPI_Comm_spawn((char *) "./concurrent_spawns", argv1, 1,
                           MPI_INFO_NULL, 0, MPI_COMM_WORLD, &intercomm[i], MPI_ERRCODES_IGNORE);

            MPI_Comm_remote_size(intercomm[i], &rsize);
            MPI_Comm_size(intercomm[i], &size);
            if (rsize != 1) {
                errs++;
                printf("Did not create 1 process (got %d)\n", rsize);
                fflush(stdout);
            }
        }

        /* Receive the error count from each of your children and add it to your
         * error count */
        for (i = 0; i < num_spawns; i++) {
            MPI_Recv(&child_errs, 1, MPI_INT, 0, 0, intercomm[i], &status);
            errs += child_errs;
            MPI_Comm_disconnect(&intercomm[i]);
        }

        /* If you are a spawned process send your errors to your parent */
        if (parentcomm != MPI_COMM_NULL) {
            MPI_Send(&errs, 1, MPI_INT, 0, 0, parentcomm);
            MPI_Comm_disconnect(&parentcomm);
            MPI_Finalize();
        } else {
            /* 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 */
            MTest_Finalize(errs);
        }
    } else {
        MTest_Finalize(errs);
    }

    IF_VERBOSE(("calling finalize\n"));
    return MTestReturnValue(errs);
}