File: join.c

package info (click to toggle)
lam 7.1.4-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 56,404 kB
  • sloc: ansic: 156,541; sh: 9,991; cpp: 7,699; makefile: 5,621; perl: 488; fortran: 260; asm: 83
file content (365 lines) | stat: -rw-r--r-- 8,720 bytes parent folder | download | duplicates (11)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: join.c,v 1.16 2003/10/11 19:15:35 brbarret Exp $
 *
 *	Function:	- join MPI applications
 *	Accepts:	- socket file descriptor
 *			- intercomm between applications (out)
 *	Returns:	- MPI_SUCCESS or error code
 */

#include <lam_config.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_STRINGS_H) && HAVE_STRINGS_H
#include <strings.h>
#endif
#include <sys/time.h>				/* LINUX FD_SET etc. */
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#if defined(HAVE_SYS_SELECT_H) && HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include <app_mgmt.h>
#include <all_list.h>
#include <blktype.h>
#include <mpi.h>
#include <mpisys.h>
#include <portable.h>
#include <lam-ssi-rpi.h>
#include <rpisys.h>
#include <typical.h>


/*
 * private functions
 */
static int exchange_with_peer(int fd, int cid, int *peercid,
                              struct _gps *peergps);
static int readwrite(int fd, int nbytes, char *inbuf, char *outbuf);


/*@
   MPI_Comm_join - Connect two MPI processed joined by a socket

Input Parameter:
. fd - socket file descriptor

Output Parameter:
. newcomm - intercommunicator with client as remote group

Description:

This function only works between two LAM/MPI processes that are
connected by a socket.  They either must have the same endian
orientation, or not have used the homogeneous flag to 'mpirun'(1)
(-O).  Both processes must be in a single LAM universe -- they must
share LAM daemons that are already connected to each other.  That is,
they were either initially lamboot(1)ed together, or a lamgrow(1)
command was given to grow an initial LAM universe such that the
resulting set includes the two hosts in question.

The socket may not be used by the calling application until
'MPI_Comm_join' has completed.  When 'MPI_Comm_join' completes, 'fd'
may be used by the calling application.  Per MPI-2, the socket is only
used for initial handshaking -- it will not be used as the
communications channel between the newly connected processes.

Sample:

The following code shows an abbreviated example using 'MPI_Comm_join'
since no example is provided in the MPI-2 standard.  Note that only
one rank in each MPI application should call this function since each
socket can only have two endpoints (i.e., an endpoint in each
process).  Upon successful completion, the intercommunicator that is
returned will have two members -- the local process and the remote
process.

.vb
  MPI_Comm
  do_join(char* server_name, int port) {
    int sfd, fd = -1;
    unsigned char addr[4];
    MPI_Comm intercomm;

    if (server_name != NULL) {
      if (getinetaddr(server_name, &addr) == 0)
        fd = open_socket_to_server(addr, port);
    } else {
      if ((sfd = open_listening_socket(&port)) >= 0) {
        fd = accept_client_socket(sfd, -1);
        close(sfd);
      }
    }
    if (fd < 0)
      MPI_Abort(MPI_COMM_WORLD, 0);

    MPI_Comm_join(fd, &intercomm);
    return intercomm;
  }
.ve

.N fortran

.N IMPI_MPI2

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_ARG
.N MPI_ERR_INTERN
.N MPI_ERR_OTHER

.seealso lamboot(1), lamgrow(1), mpirun(1), MPI_Finalize(3)

.N WEB
@*/
int
MPI_Comm_join(int fd, MPI_Comm *newcomm)
{
	struct _proc	*p;			/* peer process */
	struct _gps	peergps;		/* peer GPS */
	MPI_Group	peergrp;		/* peer group */
	int		peercid;		/* peer ID */
	int		cid;			/* new context ID */

	lam_initerr_m();
	lam_setfunc_m(BLKMPICOMMJOIN);
/*
 * Check the arguments.
 */
	if (fd < 0) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_ARG, EINVAL,
			"illegal file descriptor"));
	}

	if (newcomm == 0) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_ARG, EINVAL,
			"null pointer"));
	}

	LAM_TRACE(lam_tr_cffstart(BLKMPICOMMJOIN));

	cid = lam_getcid();

	if (cid < 0) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_INTERN, EFULL, ""));
	}
/*
 * Exchange cid and GPS with peer.
 */
	if (exchange_with_peer(fd, cid, &peercid, &peergps)) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_OTHER, errno, ""));
	}
/*
 * The intercomm context ID is the maximium of the local and peer CIDs.
 */
	cid = LAM_max(cid, peercid);
/*
 * Create the remote (client) group.
 */
	peergrp = (MPI_Group) 
		malloc(sizeof(struct _group) + sizeof(struct _proc *));
	if (peergrp == 0) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_OTHER, errno, ""));
	}
	peergrp->g_nprocs = 1;
	peergrp->g_myrank = MPI_UNDEFINED;
	peergrp->g_refcount = 1;
	peergrp->g_f77handle = -1;
	peergrp->g_procs = (struct _proc **)
				((char *) peergrp + sizeof(struct _group));

	p = *peergrp->g_procs = lam_procadd(&peergps);
	if (p == 0) {
		free((char *) peergrp);
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_OTHER, errno, ""));
	}
	if (!(p->p_mode & LAM_PRPIINIT)) {
		p->p_mode |= LAM_PCLIENT;
	}
	p->p_refcount++;
/*
 * Create the server-client intercommunicator.
 */
	*newcomm = 0;
	if (lam_comm_new(cid, MPI_COMM_SELF->c_group,
				peergrp, LAM_CINTER, newcomm)) {

		free((char *) peergrp);
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_OTHER, errno, ""));
	}

	MPI_COMM_SELF->c_group->g_refcount++;
	(*newcomm)->c_errhdl = MPI_COMM_SELF->c_errhdl;
	MPI_COMM_SELF->c_errhdl->eh_refcount++;

	if (!al_insert(lam_comms, newcomm)) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_INTERN, errno, ""));
	}

	if (lam_tr_comm(*newcomm)) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_INTERN, errno, ""));
	}

	lam_setcid(cid);
/*
 * Setup the new processes.
 */
	if (RPI_ADDPROCS(peergrp->g_procs, peergrp->g_nprocs)) {
		return(lam_err_comm(MPI_COMM_SELF, MPI_ERR_OTHER, errno, ""));
	}

	/* Let the collective SSI modules battle over who will do
	   collectives on this new communicator.  */

	if (lam_ssi_coll_base_init_comm(*newcomm) != 0)
	  return(lam_errfunc(MPI_COMM_SELF, BLKMPICOMMJOIN,
			     lam_mkerr(MPI_ERR_INTERN, ENOSYS)));

	LAM_TRACE(lam_tr_cffend(BLKMPICOMMJOIN, 0, 0, 0, 0));

        lam_resetfunc_m(BLKMPICOMMJOIN);
	return(MPI_SUCCESS);
}


/*
 *	exchange_with_peer
 *
 *	Function:	- exchange information with peer over a socket
 *	Accepts:	- socket file descriptor
 *			- local cid
 *			- peer cid (out)
 *			- peer GPS (out)
 *	Returns:	- 0 or LAMERROR
 */
static int
exchange_with_peer(int fd, int cid, int *peercid, struct _gps *peergps)
{
	struct _gps	mygps;
	int4		cid4;
	int4		peercid4;

	mygps = lam_myproc->p_gps;
	mltoti4(&mygps, sizeof(struct _gps) / sizeof(int4));
	cid4 = (int4) cid;
	mltoti4(&cid4, 1);

	if (readwrite(fd, sizeof(struct _gps),
			(char *) peergps, (char *) &mygps)) {
		return(LAMERROR);
	}

	if (readwrite(fd, sizeof(int4),
			(char *) &peercid4, (char *) &cid4)) {
		return(LAMERROR);
	}

	mttoli4(peergps, sizeof(struct _gps) / sizeof(int4));
	mttoli4(&peercid4, 1);
	*peercid = (int) peercid4;
	return(0);
}


/*
 *	readwrite
 *
 *	Function:	- exchange bytes over a socket
 *	Accepts:	- socket file descriptor
 *			- number of bytes to exchange
 *			- incoming data buffer
 *			- outgoing data buffer (filled)
 *	Returns:	- 0 or LAMERROR
 */
static int
readwrite(int fd, int nbytes, char *inbuf, char *outbuf)
{
	fd_set		rfd;
	fd_set		wfd;
	int		ntowrite;		/* # bytes left to write */
	int		ntoread;		/* # bytes left to read */
	int		nready;			/* # ready file descriptors */
	int		r;

	FD_ZERO(&rfd);
	FD_ZERO(&wfd);
	ntoread = nbytes;
	ntowrite = nbytes;
/*
 * Loop writing and reading until all is done.  Write first to avoid
 * deadlock.
 */
	while (ntowrite > 0 || ntoread > 0) {

		if (ntowrite > 0) {
			FD_SET(fd, &wfd);
		} else {
			FD_CLR(fd, &wfd);
		}

		if (ntoread > 0) {
			FD_SET(fd, &rfd);
		} else {
			FD_CLR(fd, &rfd);
		}

		do {
			nready = select(fd + 1, &rfd, &wfd,
					(fd_set *) 0, (struct timeval *) 0);

			if (nready < 0 && errno != EINTR) {
				return(LAMERROR);
			}
		} while (nready < 0);

		if (FD_ISSET(fd, &wfd)) {
			r = write(fd, outbuf, ntowrite);

			if (r < 0) {
				if (errno != EINTR) {
					return(LAMERROR);
				}	
			} else if (r == 0) {
				errno = EEOF;
			} else {
				ntowrite -= r;
				outbuf += r;
			}
		}

		if (FD_ISSET(fd, &rfd)) {
			r = read(fd, inbuf, ntoread);

			if (r < 0) {
				if (errno != EINTR) {
					return(LAMERROR);
				}	
			} else if (r == 0) {
				errno = EEOF;
			} else {
				ntoread -= r;
				inbuf += r;
			}
		}
	}

	return(0);
}