1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "mpitest.h"
11
12 void check_error(int, const char *);
check_error(int error,const char * fcname)13 void check_error(int error, const char *fcname)
14 {
15 char err_string[MPI_MAX_ERROR_STRING] = "";
16 int length;
17 if (error != MPI_SUCCESS) {
18 MPI_Error_string(error, err_string, &length);
19 printf("%s failed: %s\n", fcname, err_string);
20 fflush(stdout);
21 MPI_Abort(MPI_COMM_WORLD, error);
22 }
23 }
24
main(int argc,char * argv[])25 int main(int argc, char *argv[])
26 {
27 int error;
28 int rank, size;
29 char port[MPI_MAX_PORT_NAME];
30 MPI_Status status;
31 MPI_Comm comm;
32 int verbose = 0;
33
34 MTEST_VG_MEM_INIT(port, MPI_MAX_PORT_NAME * sizeof(char));
35
36 if (getenv("MPITEST_VERBOSE")) {
37 verbose = 1;
38 }
39
40 if (verbose) {
41 printf("init.\n");
42 fflush(stdout);
43 }
44 MTest_Init(&argc, &argv);
45
46 /* To improve reporting of problems about operations, we
47 * change the error handler to errors return */
48 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
49 MPI_Comm_set_errhandler(MPI_COMM_SELF, MPI_ERRORS_RETURN);
50
51 if (verbose) {
52 printf("size.\n");
53 fflush(stdout);
54 }
55 error = MPI_Comm_size(MPI_COMM_WORLD, &size);
56 check_error(error, "MPI_Comm_size");
57
58 if (verbose) {
59 printf("rank.\n");
60 fflush(stdout);
61 }
62 error = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
63 check_error(error, "MPI_Comm_rank");
64
65 if (size < 2) {
66 printf("Two processes needed.\n");
67 MPI_Finalize();
68 return 1;
69 }
70
71 if (rank == 0) {
72 if (verbose) {
73 printf("open_port.\n");
74 fflush(stdout);
75 }
76 error = MPI_Open_port(MPI_INFO_NULL, port);
77 check_error(error, "MPI_Open_port");
78
79 if (verbose) {
80 printf("0: opened port: <%s>\n", port);
81 fflush(stdout);
82 }
83 if (verbose) {
84 printf("send.\n");
85 fflush(stdout);
86 }
87 error = MPI_Send(port, MPI_MAX_PORT_NAME, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
88 check_error(error, "MPI_Send");
89
90 if (verbose) {
91 printf("accept.\n");
92 fflush(stdout);
93 }
94 error = MPI_Comm_accept(port, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm);
95 check_error(error, "MPI_Comm_accept");
96
97 if (verbose) {
98 printf("close_port.\n");
99 fflush(stdout);
100 }
101 error = MPI_Close_port(port);
102 check_error(error, "MPI_Close_port");
103
104 if (verbose) {
105 printf("disconnect.\n");
106 fflush(stdout);
107 }
108 error = MPI_Comm_disconnect(&comm);
109 check_error(error, "MPI_Comm_disconnect");
110 } else if (rank == 1) {
111 if (verbose) {
112 printf("recv.\n");
113 fflush(stdout);
114 }
115 error = MPI_Recv(port, MPI_MAX_PORT_NAME, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
116 check_error(error, "MPI_Recv");
117
118 if (verbose) {
119 printf("1: received port: <%s>\n", port);
120 fflush(stdout);
121 }
122 if (verbose) {
123 printf("connect.\n");
124 fflush(stdout);
125 }
126 error = MPI_Comm_connect(port, MPI_INFO_NULL, 0, MPI_COMM_SELF, &comm);
127 check_error(error, "MPI_Comm_connect");
128
129 MPI_Comm_set_errhandler(comm, MPI_ERRORS_RETURN);
130
131 if (verbose) {
132 printf("disconnect.\n");
133 fflush(stdout);
134 }
135 error = MPI_Comm_disconnect(&comm);
136 check_error(error, "MPI_Comm_disconnect");
137 }
138
139 error = MPI_Barrier(MPI_COMM_WORLD);
140 check_error(error, "MPI_Barrier");
141
142 MTest_Finalize(0);
143 return MTestReturnValue(0);
144 }
145