File: testconnect.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 (138 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/* Test from Edric Ellis
   To launch,

   Run multiple copies of "testconnect" should be run something like this:

   Path/to/testconnect /full/path/to/shared/file N

   where each instance has one process and N instances are run
*/

#include "mpi.h"
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>

char *fname;
int cachedRank = -1;
MPI_Comm comm;

void term_handler(int);
void segv_handler(int);

void term_handler(int sig)
{
    if (sig) {
        printf("Saw signal %d\n", sig);
    }
    printf("removing file: %s\n", fname);
    fflush(stdout);
    unlink(fname);
    if (sig != 0) {
        MPI_Abort(comm, 42);
        exit(0);
    }
}

void segv_handler(int sig)
{
    printf("SEGV detected!\n");
    fflush(stdout);
}

int main(int argc, char **argv)
{
    MPI_Comm tmp;
    int size = 0;
    char portName[MPI_MAX_PORT_NAME];
    char *port = &portName[0];
    int doPrint = 1;
    int totalSize = 0;
    int myNum = -1;
    FILE *fh;

    if (argc < 4) {
        printf("Please call with a filename for the port\n");
        exit(1);
    }
    fname = argv[1];
    totalSize = atoi(argv[2]);
    if (argv[3])
        myNum = atoi(argv[3]);
    printf("[%d] Waiting for: %d\n", myNum, totalSize);

    MPI_Init(0, 0);

    /* the existence of the file is used to decide which processes
     * first do a connect to the root process.  */
    fh = fopen(fname, "rt");
    if (fh == NULL) {
        fh = fopen(fname, "wt");
        MPI_Open_port(MPI_INFO_NULL, portName);
        port = portName;
        fprintf(fh, "%s\n", portName);
        fclose(fh);

        if (doPrint) {
            printf("[%d] Wrote port %s to %s\n", myNum, port, fname);
            fflush(stdout);
        }
        comm = MPI_COMM_WORLD;
    } else {
        char *s = fgets(port, MPI_MAX_PORT_NAME, fh);
        assert(s != NULL);
        fclose(fh);
        if (doPrint) {
            printf("[%d] about to connect: Port from %s is: %s\n", myNum, fname, port);
            fflush(stdout);
        }
        MPI_Comm_connect(port, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &tmp);
        if (doPrint) {
            printf("[%d] connect-side: about to perform intercomm merge\n", myNum);
            fflush(stdout);
        }
        MPI_Intercomm_merge(tmp, 1, &comm);
        if (doPrint) {
            printf("[%d] connect-side: intercomm merge complete\n", myNum);
            fflush(stdout);
        }
        MPI_Comm_free(&tmp);
    }

    MPI_Comm_size(comm, &size);

    while (size < totalSize) {
        MPI_Comm_accept(port, MPI_INFO_NULL, 0, comm, &tmp);
        if (doPrint) {
            printf("[%d] accept-side: about to perform intercomm merge\n", myNum);
            fflush(stdout);
        }
        MPI_Intercomm_merge(tmp, 0, &comm);
        if (doPrint) {
            printf("[%d] accept-side: about to perform intercomm merge\n", myNum);
            fflush(stdout);
        }
        MPI_Comm_rank(comm, &cachedRank);
        MPI_Comm_free(&tmp);
        MPI_Comm_size(comm, &size);
        if (doPrint) {
            printf("[%d] Size of comm is %d\n", myNum, size);
            fflush(stdout);
        }
    }

    printf("[%d] All done.\n", myNum);
    fflush(stdout);
    term_handler(0);

    MPI_Finalize();

    return 0;
}