File: xmpi_sys.lam.c

package info (click to toggle)
xmpi 2.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,232 kB
  • ctags: 1,656
  • sloc: ansic: 13,738; sh: 1,799; makefile: 233
file content (716 lines) | stat: -rw-r--r-- 13,628 bytes parent folder | download
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
 * Copyright 1998-1999, University of Notre Dame.
 * Authors: Brian W. Barrett, Arun F. Rodrigues, Jeffrey M. Squyres,
 * 	 and Andrew Lumsdaine
 *
 * This file is part of XMPI
 *
 * You should have received a copy of the License Agreement for XMPI 
 * along with the software; see the file LICENSE.  If not, contact 
 * Office of Research, University of Notre Dame, Notre Dame, IN 46556.
 *
 * Permission to modify the code and to distribute modified code is
 * granted, provided the text of this NOTICE is retained, a notice that
 * the code was modified is included with the above COPYRIGHT NOTICE and
 * with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
 * file is distributed with the modified code.
 *
 * LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
 * By way of example, but not limitation, Licensor MAKES NO
 * REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
 * OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
 * OR OTHER RIGHTS.
 *
 * Additional copyrights may follow.

 *
 *	$Id: xmpi_sys.lam.c,v 1.8 1999/11/11 16:39:25 bbarrett Exp $
 *
 *	Function:	- xmpi -> MPI system interface
 *			- LAM version
 */

#include <lam_config.h>

#include <sys/errno.h>
#include <stdlib.h>
#include <unistd.h>

#include <args.h>
#include <all_list.h>
#include <app_mgmt.h>
#include <app_schema.h>
#include <freq.h>
#include <laminternal.h>
#include <lamnet.h>
#include <kio.h>
#include <portable.h>
#include <priority.h>
#include <terror.h>
#include <trreq.h>
#include <typical.h>

#include "../xmpi.h"

/*
 * public functions
 */
int			xmpi_sys_init();
int			xmpi_sys_kill();
int			xmpi_sys_run();
int			xmpi_sys_trace();
void			xmpi_sys_finalize();
void			*xmpi_sys_comm();
void			*xmpi_sys_dtype();
char			*xmpi_sys_errorstr();
char			**xmpi_sys_hosts();
char			**xmpi_sys_logo();
char			*xmpi_sys_version();

/*
 * private functions
 */
static int		initialize();
static int		set_stdio();
static void		reset_stdio();
static void		report_run_error();
static int		get_mpi_world(int4, struct _gps *, struct _gps *);
static void		error_cleanup(void);

/*
 * external functions
 */
extern int		_lam_few();
extern int		bhostparse();
extern int		mpitr_comget();
extern int		mpitr_dtypeget();
extern int		trdrain_mpi();
extern void		lam_kexit();
extern char		*getworkdir();
extern LIST		*xmpi_asc_parse();

/*
 * private variables
 */
static char		bschema[XMPI_PATHMAX];
static char		version[] = "LAM-libxmpi 6.3-b4";

/*
 * external variables
 */
extern struct kio_t	_kio;			/* kernel I/O block */
extern struct fclient	_ufd[FUMAX];		/* user file desc. */
extern int		xmpi_c2c;
extern int		xmpi_homog;
extern int		xmpi_nger;
extern int		xmpi_trace;
extern int		xmpi_trinit;
extern int		sys_nerr;

/*
 *	xmpi_sys_init
 *
 *	Function:	- initializes the MPI implementation interface
 *	Accepts:	- command line info string
 *	Returns:	- 0 or -1 (error)
 */
int
xmpi_sys_init(info)

char			*info;

{
	FILE		*bootfp;
/*
 * For us the info is a boot schema name.  Error check it.
 */
	if (info) {
		bootfp = fopen(info, "r");

		if (bootfp) {
			strncpy(bschema, info, XMPI_PATHMAX);
			fclose(bootfp);
		} else {
			bschema[0] = 0;
			xmpi_error(0, info);
		}
	}

	return(0);
}

/*
 *	xmpi_sys_finalize
 *
 *	Function:	- finalizes the MPI implementation interface
 */
void
xmpi_sys_finalize()

{
	lam_kexit(0);
}

/*
 *	xmpi_sys_run
 *
 *	Function:	- runs the parsed application schema
 *	Accepts:	- application schema (list)
 *			- application GPS array (returned)
 *			- length of application GPS array (returned)
 *	Returns:	- 0 or -1 (error)
 */
int
xmpi_sys_run(aschema, app_procs, app_nprocs)

char			*aschema;
struct _gps **		app_procs;
int *			app_nprocs;

{
	int4		rtf = 0;		/* runtime flags */
	LIST *		appd;			/* parsed schema */
	LIST *		appd_sched;		/* scheduled app schema */
	struct _gps	*world;			/* world GPS array */
	struct _gps	*mpiworld;		/* MPI world GPS array */
	int		world_n;		/* size of world */

	if (initialize()) return(-1);

	rtf = RTF_MPIRUN | RTF_IO | ((xmpi_trace) ? RTF_TRACE : 0)
			| ((xmpi_trinit) ? RTF_TRSWITCH : 0)
			| ((xmpi_homog) ? RTF_HOMOG : 0)
			| ((!xmpi_nger) ? RTF_MPIGER : 0)
			| ((xmpi_c2c) ? RTF_MPIC2C : 0);

	if ((appd = xmpi_asc_parse(aschema)) == 0) return(-1);

	if ((appd_sched = asc_schedule(appd)) == 0) {
		asc_free(appd);
		xmpi_error(0, "Scheduling application");
		return(-1);
	}

	asc_free(appd);
/*
 * Get the process table, store it for XMPI and send it to MPI processes.
 */
	world_n = al_count(appd_sched);
	mpiworld = (struct _gps *) malloc(world_n * sizeof(struct _gps));
	world = (struct _gps *) malloc(world_n * sizeof(struct _gps));

	if (world == 0 || mpiworld == 0) {
		asc_free(appd_sched);
		xmpi_error(0, "malloc");
		return(-1);
	}

	if (set_stdio()) {
		asc_free(appd_sched);
		xmpi_error(0, "set_stdio");
		return(-1);
	}

	if (asc_run(appd_sched, 0, rtf, 0, 0, world)) {
		reset_stdio();
		report_run_error(appd_sched);
		asc_free(appd_sched);
		free(mpiworld);
		free(world);
		return(-1);
	}

	reset_stdio();
	asc_free(appd_sched);
/*
 * Read pids and indices from MPI processes.
 */
	if (get_mpi_world(world_n, world, mpiworld)) {
		app_doom(world_n, mpiworld, SIGUDIE);
		app_doom(world_n, world, -15);
		error_cleanup();
		xmpi_error(0, "get_mpi_world");
		return(-1);
	}
/*
 * Send process table to all processes.
 */
	if (app_sendprocs(world_n, mpiworld)) {
		app_doom(world_n, mpiworld, SIGUDIE);
		app_doom(world_n, world, -15);
		error_cleanup();
		xmpi_error(0, "app_sendprocs");
		return(-1);
	}

	*app_nprocs = world_n;
	*app_procs = mpiworld;

	free(world);

	return(0);
}

/*
 *	xmpi_sys_kill
 *
 *	Function:	- kills the running application
 *	Accepts:	- GPS array (unused)
 *			- GPS array length (unused)
 *	Returns:	- 0 or -1 (error)
 */
int
xmpi_sys_kill(app_procs, app_nprocs)

struct _gps *		app_procs;
int			app_nprocs;

{
	char		**av;			/* cmd line args */
	int		ac;			/* # cmd line args */
	int		r;			/* result of lamclean */

	av = 0;
	ac = 0;
	argvadd(&ac, &av, "lamclean");

	r = _lam_few(av);
	argvfree(av);

	if (r < 0) {
		return(-1);
	} else if (r > 0) {
		errno = r;
		return(-1);
	}

	return(0);
}

/*
 *	xmpi_sys_hosts
 *
 *	Function:	- Who are the participating hosts and what
 *			  is the resulting node list?
 *			- no boot schema -> no nodes
 *	Accepts:	- boot schema filename
 *	Returns:	- node name vector
 */
char **
xmpi_sys_hosts()

{
	FILE		*bootfp;
	int		i;
	int		lamsize = 0;
	int		nodec = 0;
	struct lamnode	*lam = 0;
	char		**nodev = 0;

	if (!bschema[0]) return(0);

	bootfp = fopen(bschema, "r");
	if (!bootfp) return(0);

	if (bhostparse(bootfp, &lam, &lamsize)) return(0);

	for (i = 0; i < lamsize; ++i) {

		if (sfh_argv_add(&nodec, &nodev, lam[i].lnd_hname)) return(0);
	}

	free((char *) lam);
	return(nodev);
}

/*
 *	xmpi_sys_comm
 *
 *	Function:	- fetch MPI communicator trace specified by member
 *			  process and context ID
 *			- space is allocated to hold the trace
 *	Accepts:	- process GPS
 *			- context ID
 *	Returns:	- trace or 0
 */
void *
xmpi_sys_comm(proc, cid)

struct _gps		*proc;
int			cid;

{
	void		*trace;

	if (mpitr_comget(proc->gps_node, proc->gps_pid, cid, &trace)) {
		return(0);
	} else {
		return(trace);
	}
}

/*
 *	xmpi_sys_dtype
 *
 *	Function:	- fetch MPI datatype trace specified by creator
 *			  process and datatype label
 *			- space is allocated to hold the trace
 *			- null trace may be returned for basic datatypes
 *	Accepts:	- process GPS
 *			- datatype label
 *	Returns:	- trace or -1 (error)
 */
void *
xmpi_sys_dtype(proc, dtype)

struct _gps		*proc;
int			dtype;

{
	char		*trace = 0;

	if (mpitr_dtypeget(proc->gps_node, proc->gps_pid, dtype, &trace)) {
		return((void *) -1);
	} else {
		return(trace);
	}
}

/*
 *	xmpi_sys_trace
 *
 *	Function:	- fetches communication traces from MPI implementation
 *	Accepts:	- open file handle
 *			- GPS array
 *			- GPS array length
 *	Returns:	- 0 or -1 (error)
 */
int
xmpi_sys_trace(fd, app_procs, app_nprocs)

int			fd;
struct _gps *		app_procs;
int			app_nprocs;

{
	struct _gps	*proc;			/* first process in world */
/*
 * We already have the GPS array but go and get the world trace anyway
 * as a sanity check.
 */
	proc = app_procs;

	if (lam_rtrfget(proc->gps_node, TRWORLD, proc->gps_pid, fd) <= 0) {
		return(-1);
	}

	if (trdrain_mpi(fd, app_procs, app_nprocs, 1, XMPI_FLUSHDELAY)) {
		return(-1);
	}

	return(0);
}

/*
 *	xmpi_sys_errorstr
 *
 *	Function:	- format system error message
 *	Accepts:	- error number
 *	Returns:	- pointer to static system error string
 *			  else 0
 */
char *
xmpi_sys_errorstr(err)

int			err;

{
	static char	errstring[256];		/* error string */
	int		errsave;		/* save value of errno */

	errstring[0] = 0;

	if (err == LAM_EEXIT) {
		strcpy(errstring, "LAM error");
	}
	else if ((err > 0 && err < sys_nerr) ||
			(err >= ELOW && err <= EHIGH)) {

		errsave = errno;
		errno = err;

		lam_errorstr(errstring, 255);

		errno = errsave;
	} else {
		return(0);
	}

	return(errstring);
}

/*
 *	xmpi_sys_version
 *
 *	Function:	- get library version
 *	Returns:	- library version string
 */
char *
xmpi_sys_version()

{
	return(version);
}

/*
 *	xmpi_sys_logo
 *
 *	Function:	- get vendor specific logo
 *	Returns:	- XPM format vendor logo
 */
char **
xmpi_sys_logo()

{
	return(0);
}

/*
 *	initialize
 *
 *	Function:	- one time initialization
 *	Returns:	- 0 or -1 (error)
 */
static int
initialize()

{
	char		*cwd;			/* current working directory */
/*
 * Become a LAM process if not already one.
 */
	if (_kio.ki_rtf & RTF_KERNEL) return(0);

	if (kinit(PRCMD)) {

		if (errno == ENOKERNEL) {
			xmpi_error(0, "Please boot LAM");
		} else {
			xmpi_error(0, "Attaching to daemon");
		}

		return(-1);
	}
/*
 * Change local working directory.
 */
	if ((cwd = getworkdir()) == 0) {
		xmpi_error(0, "getworkdir");
		return(-1);
	}

	if (lam_rfchdir(LOCAL, cwd)) {
		xmpi_error(0, "lam_rfchdir");
		free(cwd);
		return(-1);
	}

	free(cwd);
/*
 * Set job identifier to be inherited by the applications.
 */
	_kio.ki_jobid.jid_node = getnodeid();
	_kio.ki_jobid.jid_pid = getpid();

	return(0);
}

/*
 *	set_stdio
 *
 *	Function:	- set up application stdio
 *	Returns:	- 0 or -1 (error)
 */
static int
set_stdio()

{
#if (LAM_HAVE_BSD43_FD_PASSING || LAM_HAVE_BSD44_FD_PASSING || LAM_HAVE_SYSV_FD_PASSING)

	char		server[LAM_PATH_MAX];	/* fd server socket name */
/*
 * Pass stdin, stdout and stderr to filed.
 */
	if (lam_mktmpid((int) getpid(), server, sizeof(server))) {
		return(-1);
	}

	if (lam_lfopenfd(server)) {
		return(-1);
	}
/*
 * Set LAM file descriptors to the passed file descriptors.  The call to
 * lam_lfopenfd() takes care of the case when stdin is a tty.
 */
	_kio.ki_stdin = _ufd[0].fu_tfd;
	_kio.ki_stdout = _ufd[1].fu_tfd;
	_kio.ki_stderr = _ufd[2].fu_tfd;
#endif
	return(0);
}

/*
 *	reset_stdio
 *
 *	Function:	- reset stdio so rfatexit will clean it up
 *	Returns:	- 0 or -1 (error)
 */
static void
reset_stdio()

{
	_kio.ki_stdin = 0;
	_kio.ki_stdout = 1;
	_kio.ki_stderr = 2;
}

/*
 *	report_run_error
 *
 *	Function:	- nice error message when application startup fails
 *	Accepts:	- application descriptor
 */
static void
report_run_error(appd)

LIST			*appd;

{
	int		i;
	struct aschema	*pp;			/* ptr process entry */
	char		buf[512];

	errno = 0;
	pp = (struct aschema *) al_top(appd);

	for (i = 0; pp; ++i, pp = (struct aschema *) al_next(appd, pp)) {
		if (pp->asc_errno) {
			errno = pp->asc_errno;
			break;
		}
	}

	if (errno == ENOENT) {
		sprintf(buf, "Cannot start \"%s\"", pp->asc_args->apa_argv[0]);
		xmpi_error(0, buf);
	} else {
		xmpi_error(0, "Starting application");
	}
}

/*
 *	get_mpi_world
 *
 *	Function:	- get MPI world
 *	Accepts:	- size of world
 *			- initial process world
 *			- MPI process world (out)
 *	Returns:	- 0 or LAMERROR
 */
static int
get_mpi_world(int4 world_n, struct _gps *world, struct _gps *mpiworld)
{
    struct nmsg		msg;
    int			i;
    int			j;

    memcpy(mpiworld, world, world_n * sizeof(struct _gps));
    for (i = 0; i < world_n; i++) {
	mpiworld[i].gps_pid = 0;
    }

    LAM_ZERO_ME(msg);
    msg.nh_event = -getpid() & 0xBFFFFFFF;
    msg.nh_length = 0;
    msg.nh_flags = DINT4DATA;

    for (i = 0; i < world_n; i++) {
	msg.nh_type = 3;
	if (nrecv(&msg)) {
	    return(LAMERROR);
	}

	if (msg.nh_type == 1) {
	    return(LAMERROR);
	}
/*
 * Set the MPI process pid and index.
 */
	j = msg.nh_data[0];
	if (j < 0 || j >= world_n) {
	    errno = EIMPOSSIBLE;
	    return(LAMERROR);
	}

	mpiworld[j].gps_pid = msg.nh_data[1];
	mpiworld[j].gps_idx = msg.nh_data[2];
    }

    return(0);
}

/*
 *	error_cleanup
 *
 *	Function:	- try to clean up init and wait messages
 *			- this is not foolproof but better than nothing
 */
static void
error_cleanup(void)
{
    struct nmsg		msg;
/*
 * Wait a little while.
 */
    sleep(1);

    LAM_ZERO_ME(msg);
    msg.nh_event = (-getpid()) & 0xBFFFFFFF;
    msg.nh_length = 0;
    msg.nh_flags = DINT4DATA;
/*
 * Loop trying to receive init messages and wait messages.
 */
    while (1) {
	msg.nh_type = 3;
	if (ntry_recv(&msg))
	    break;
    }
}

#ifdef HPUX
/*
 * These functions implement the "build and run" and "option setting"
 * dialogs when XMPI is built for running on a HP machine and uses the
 * LAM libxmpi.	 They are to be removed once the HP "bypass" becomes
 * unnecessary.
 */
void
xmpi_hp_run_dialog(parent)

Widget			parent;

{
	xmpi_run_dialog(parent);
}

void
xmpi_hp_options_set(parent)

Widget			parent;

{
	xmpi_options_set(parent);
}
#endif /* HPUX */