File: lamtr_util.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 (415 lines) | stat: -rw-r--r-- 8,226 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
/*
 * 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: lamtr_util.c,v 1.3 1999/11/11 05:31:19 arodrig6 Exp $
 *
 *	Function:	- trace parsing utilities
 */

#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#include "app_mgmt.h"
#include "lam.h"
#include "lamtrace.h"
#include "mpitrace.h"

/*
 * local variables
 */
static FILE		*trace_stream = 0;

/*
 *	lamtr_set_stream
 *
 *	Function:	- set trace input stream
 *	Accepts:	- open stream
 */
void
lamtr_set_stream(traces)

FILE			*traces;

{
	if (trace_stream) {
		fclose(trace_stream);
	}

	trace_stream = traces;
}

/*
 *	lamtr_open_file
 *
 *	Function:	- open a trace file
 *	Accepts:	- file name
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_open_file(tracefile)

char			*tracefile;

{
	FILE		*traces;

	if ((traces = fopen(tracefile, "r")) == 0) {
		return(LAMERROR);
	}

	lamtr_set_stream(traces);
	return(0);
}

/*
 *	lamtr_read
 *
 *	Function:	- read from the trace stream
 *	Accepts:	- buffer to put data read
 *			- size of an element
 *			- number of elements to read
 *	Returns:	- 0 for success, 1 on eof, else -1
 */
int
lamtr_read(ptr, size, count)

void			*ptr;
size_t			size;
size_t			count;

{
	size_t		nread;			/* number of elements read */

	if (trace_stream == 0) {
		return(-1);
	}

	nread = fread((char *) ptr, size, count, trace_stream);

	if (nread == 0 && ferror(trace_stream)) {
		return(LAMERROR);
	}
	else if (nread != count) {
		return(1);
	}
	else {
		return(0);
	}
}

/*
 *	lamtr_parse_world
 *
 *	Function:	- parse world trace
 *			- storage is allocated to hold the GPS array
 *			- values are returned in local byte order
 *	Accepts:	- size of world (out)
 *			- world GPS array (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_parse_world(np, world)

int4			*np;
struct _gps		**world;

{
	int		magic;			/* magic number */
/*
 * Check the magic number.
 */
	if (lamtr_read(&magic, sizeof(int4), 1)) {
		return(LAMERROR);
	}

	ttoli4((int4 *) &magic, (int4 *) &magic);
	if (magic != LAM_TRMAGIC) {
		errno = EMAGIC;
		return(LAMERROR);
	}
/*
 * Get the size of the world.
 */
	if (lamtr_read(np, sizeof(int4), 1)) {
		return(LAMERROR);
	}
	ttoli4(np, np);
	if (*np <= 0) {
		errno = EINVAL;
		return(LAMERROR);
	}
/*
 * Allocate and read in GPS array.
 */
	*world = (struct _gps *) malloc((*np) * sizeof(struct _gps));
	if (*world == 0) return(LAMERROR);

	if (lamtr_read(*world, sizeof(struct _gps), (size_t) *np)) {
		return(LAMERROR);
	}

	mttoli4((int4 *) *world, (*np) * sizeof(struct _gps) / sizeof(int4));
	return(0);
}

/*
 *	lamtr_raw_world
 *
 *	Function:	- get raw world trace
 *			- storage is allocated to hold the GPS array
 *			- values are returned in LAM byte order
 *	Accepts:	- size of world (out)
 *			- world GPS array (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_raw_world(np, world)

int4			*np;
char			**world;

{
	int4		n;			/* size of world */

	if (lamtr_read(np, sizeof(int4), 1)) {
		return(LAMERROR);
	}

	ttoli4(np, &n);

	*world = (char *) malloc(n * sizeof(struct _gps));
	if (*world == 0) return(LAMERROR);

	if (lamtr_read(*world, sizeof(struct _gps), (size_t) n)) {
		return(LAMERROR);
	}
	return(0);
}

/*
 *	lamtr_parse_comm
 *
 *	Function:	- parse a communicator trace
 *			- storage is allocated to hold the GPS array
 *	Accepts:	- context ID (out)
 *			- size local group (out)
 *			- size remote group (out)
 *			- groups GPS array (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_parse_comm(cid, nlocal, nremote, grps)

int4			*cid;
int4			*nlocal;
int4			*nremote;
struct _gps		**grps;

{
	struct trcid	cid_hdr;		/* cid trace head */
	size_t 		nbytes;			/* size of GPS array */
/*
 * Read the trace head.
 */
	if (lamtr_read(&cid_hdr, sizeof(struct trcid), 1)) {
		return(LAMERROR);
	}
	mttoli4((int4 *) &cid_hdr, sizeof(struct trcid) / sizeof(int4));
/*
 * Extract communicator id and group sizes.
 */
	*cid = cid_hdr.trc_cid;
	*nlocal = cid_hdr.trc_nlg;
	*nremote = cid_hdr.trc_nrg;

	nbytes = (*nlocal + *nremote) * sizeof(struct _gps);
/*
 * Allocate the group GPS array and read it in.
 */
	if ((*grps = (struct _gps *) malloc(nbytes)) == 0) {
		return(LAMERROR);
	}

	if (lamtr_read(*grps, nbytes, 1)) {
		free(*grps);
		return(LAMERROR);
	}

	mttoli4(*grps, nbytes / sizeof(int4));
	return(0);
}

/*
 *	lamtr_raw_comm
 *
 *	Function:	- get raw communicator trace
 *			- storage is allocated for the groups GPS array
 *			- values are unconverted
 *	Accepts:	- communicator trace header (out)
 *			- groups GPS array (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_raw_comm(cid, grps)

char			*cid;
struct _gps		**grps;

{
	size_t		nbytes;			/* size of GPS array */
	int4		nlocal;			/* local group size */
	int4		nremote;		/* remote group size */
/*
 * Read the communicator trace header.
 */
	if (lamtr_raw_cid_m(*cid)) {
		return(LAMERROR);
	}
/*
 * Extract the group sizes.
 */
	ttoli4(&((struct trcid *) cid)->trc_nlg,  &nlocal);
	ttoli4(&((struct trcid *) cid)->trc_nrg,  &nremote);

	nbytes = (nlocal + nremote) * sizeof(struct _gps);
/*
 * Allocate the group GPS array and read it in.
 */
	if ((*grps = (struct _gps *) malloc((size_t) nbytes)) == 0) {
		return(LAMERROR);
	}

	if (lamtr_read(*grps, nbytes, 1)) {
		free(*grps);
		return(LAMERROR);
	}

	return(0);
}

/*
 *	lamtr_raw_dtype
 *
 *	Function:	- get raw datatype trace
 *			- values are unconverted
 *			- storage is allocated for the trace
 *	Accepts:	- raw datatype trace (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_raw_dtype(dtype)

char			**dtype;

{
	struct trdtype	dttr;			/* datatype trace head */
	int4		len;			/* trace length */
/*
 * Read the trace head.
 */
	if (lamtr_read(&dttr, sizeof(struct trdtype), 1)) {
		return(LAMERROR);
	}
	ttoli4(&dttr.trd_length, &len);
/*
 * Allocate the datatype buffer (flattened type map) and read it in.
 */
	if ((*dtype = (char *) malloc((size_t) len)) == 0) {
		return(LAMERROR);
	}

	if (lamtr_read((*dtype) + sizeof(struct trdtype),
				len - sizeof(struct trdtype), 1)) {
		free(*dtype);
		return(LAMERROR);
	}

	memcpy(*dtype, (char *) &dttr, sizeof(struct trdtype));
	return(0);
}

/*
 *	lamtr_parse_dtype
 *
 *	Function:	- parse a datatype trace
 *			- values are returned in local byte order
 *			- storage is allocated for the trace
 *	Accepts:	- datatype label (out)
 *			- datatype trace (out)
 *	Returns:	- 0 or LAMERROR
 */
int
lamtr_parse_dtype(dtype, dtbuf)

int4			*dtype;
char			**dtbuf;

{
	int		len;			/* trace length */

	if (lamtr_raw_dtype(dtbuf)) {
		return(LAMERROR);
	}

	ttoli4(&((struct trdtype *) *dtbuf)->trd_length, &len);
	mttoli4((int4 *) *dtbuf, len / sizeof(int4));
	len=len;
	*dtype = ((struct trdtype *) *dtbuf)->trd_dtype;
	return(0);
}

/*
 *	lamtr_find_rank
 *
 *	Function:	- finds global rank corresponding to node and pid
 *	Accepts:	- size of world
 *			- world GPS array
 *			- node
 *			- pid
 *	Returns:	- global rank corresponding to node and pid, else (-1)
 */
int
lamtr_find_rank(np, world, node, pid)

int			np;
struct _gps		*world;
int			node;
int			pid;

{
	int		i;

	for (i = 0; i < np; i++, world++) {
		if (world->gps_node == node && world->gps_pid == pid) {
			return(i);
		}
	}

	return(-1);
}