File: selfconacc.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 (144 lines) | stat: -rw-r--r-- 3,789 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
139
140
141
142
143
144
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

void check_error(int, const char *);
void check_error(int error, const char *fcname)
{
    char err_string[MPI_MAX_ERROR_STRING] = "";
    int length;
    if (error != MPI_SUCCESS) {
        MPI_Error_string(error, err_string, &length);
        printf("%s failed: %s\n", fcname, err_string);
        fflush(stdout);
        MPI_Abort(MPI_COMM_WORLD, error);
    }
}

int main(int argc, char *argv[])
{
    int error;
    int rank, size;
    char port[MPI_MAX_PORT_NAME];
    MPI_Status status;
    MPI_Comm comm;
    int verbose = 0;

    MTEST_VG_MEM_INIT(port, MPI_MAX_PORT_NAME * sizeof(char));

    if (getenv("MPITEST_VERBOSE")) {
        verbose = 1;
    }

    if (verbose) {
        printf("init.\n");
        fflush(stdout);
    }
    MTest_Init(&argc, &argv);

    /* To improve reporting of problems about operations, we
     * change the error handler to errors return */
    MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
    MPI_Comm_set_errhandler(MPI_COMM_SELF, MPI_ERRORS_RETURN);

    if (verbose) {
        printf("size.\n");
        fflush(stdout);
    }
    error = MPI_Comm_size(MPI_COMM_WORLD, &size);
    check_error(error, "MPI_Comm_size");

    if (verbose) {
        printf("rank.\n");
        fflush(stdout);
    }
    error = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    check_error(error, "MPI_Comm_rank");

    if (size < 2) {
        printf("Two processes needed.\n");
        MPI_Finalize();
        return 1;
    }

    if (rank == 0) {
        if (verbose) {
            printf("open_port.\n");
            fflush(stdout);
        }
        error = MPI_Open_port(MPI_INFO_NULL, port);
        check_error(error, "MPI_Open_port");

        if (verbose) {
            printf("0: opened port: <%s>\n", port);
            fflush(stdout);
        }
        if (verbose) {
            printf("send.\n");
            fflush(stdout);
        }
        error = MPI_Send(port, MPI_MAX_PORT_NAME, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
        check_error(error, "MPI_Send");

        if (verbose) {
            printf("accept.\n");
            fflush(stdout);
        }
        error = MPI_Comm_accept(port, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm);
        check_error(error, "MPI_Comm_accept");

        if (verbose) {
            printf("close_port.\n");
            fflush(stdout);
        }
        error = MPI_Close_port(port);
        check_error(error, "MPI_Close_port");

        if (verbose) {
            printf("disconnect.\n");
            fflush(stdout);
        }
        error = MPI_Comm_disconnect(&comm);
        check_error(error, "MPI_Comm_disconnect");
    } else if (rank == 1) {
        if (verbose) {
            printf("recv.\n");
            fflush(stdout);
        }
        error = MPI_Recv(port, MPI_MAX_PORT_NAME, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
        check_error(error, "MPI_Recv");

        if (verbose) {
            printf("1: received port: <%s>\n", port);
            fflush(stdout);
        }
        if (verbose) {
            printf("connect.\n");
            fflush(stdout);
        }
        error = MPI_Comm_connect(port, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm);
        check_error(error, "MPI_Comm_connect");

        MPI_Comm_set_errhandler(comm, MPI_ERRORS_RETURN);

        if (verbose) {
            printf("disconnect.\n");
            fflush(stdout);
        }
        error = MPI_Comm_disconnect(&comm);
        check_error(error, "MPI_Comm_disconnect");
    }

    error = MPI_Barrier(MPI_COMM_WORLD);
    check_error(error, "MPI_Barrier");

    MTest_Finalize(0);
    return MTestReturnValue(0);
}