File: lambuf.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 (421 lines) | stat: -rw-r--r-- 8,741 bytes parent folder | download | duplicates (10)
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
/*
 * 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: lambuf.c,v 6.7 2003/04/10 15:40:01 jsquyres Exp $
 *
 *	Function:	- buffer management functions
 */

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

#include <blktype.h>
#include <mpi.h>
#include <mpisys.h>
#include <rpisys.h>
#include <t_types.h>
#include <terror.h>


/*
 * macros
 */
#define HDRSIZE		(sizeof(struct _bsndhdr))


/*
 * local variables
 */
static void		*userbuf = 0;
static int		bufsize = 0;
static struct _bsndhdr	*buftop = 0;
static struct _bsndhdr	*bufend = 0;
static struct _bsndhdr	*buffree = 0;


/*
 * local functions
 */
static void buf_append(struct _bsndhdr *pbuf);
static void buf_free(struct _bsndhdr *pchunk);
static void buf_unlink(struct _bsndhdr *pbuf);
static struct _bsndhdr *buf_alloc(int size);


/*
 *	lam_bufinitbsend_
 *
 *	Function:	- setup bsend buffer
 *	Accepts:	- request
 *	Returns:	- MPI_SUCCESS or error code
 */
int
lam_bufinitbsend_(MPI_Request rq)
{
/*
 * Handle zero length messages.
 */
	if ((rq->rq_count == 0) || (rq->rq_dtype->dt_size == 0)) {
		rq->rq_packbuf = (char *) rq->rq_buf;
		rq->rq_packsize = 0;
		return(MPI_SUCCESS);
	}

	rq->rq_packsize = rq->rq_count * rq->rq_dtype->dt_size;
/*
 * Allocate a chunk of buffer for the message.
 */
	rq->rq_bsend = buf_alloc(rq->rq_packsize);
	if (rq->rq_bsend == 0) return(lam_mkerr(MPI_ERR_OTHER, ENOBUFS));

	rq->rq_bsend->bsh_req = rq;

	buf_append(rq->rq_bsend);
/*
 * Setup the request for a typical packing buffer.
 * The message will be packed in and out by _mpi_req_start() and
 * _mpi_req_end().  We should optimize with a memcpy() and Trollius
 * data conversion if the message is homogenous and contiguous.
 */
	rq->rq_packbuf = ((char *) rq->rq_bsend) + HDRSIZE;

	return(MPI_SUCCESS);
}


/*
 *	lam_bufinit
 *
 *	Function:	- setup send/recv message buffer
 *	Accepts:	- request
 *	Returns:	- MPI_SUCCESS or error code
 */
int
lam_bufinit(MPI_Request rq)
{
	MPI_Datatype	dtype;			/* datatype */
	double		local_rep;		/* local data representation */
	double		net_rep;		/* net data representation */

	dtype = rq->rq_dtype;
/*
 * Handle zero length messages.
 */
	if ((rq->rq_count == 0) || (dtype->dt_size == 0)) {
		rq->rq_packbuf = (char *) rq->rq_buf;
		rq->rq_packsize = 0;
		return(MPI_SUCCESS);
	}

	local_rep = 1.1;
	ltotf8(&local_rep, &net_rep);
/*
 * If contiguous, use the caller's buffer.
 */
	rq->rq_packsize = rq->rq_count * dtype->dt_size;

	if ((dtype->dt_flags & LAM_DTNOPACK) &&
            ((dtype->dt_flags & LAM_DTNOXADJ) || rq->rq_count==1) &&
            ((local_rep == net_rep) || lam_homog)) {
          rq->rq_packbuf = (char *) rq->rq_buf;

          /* Check for bad buffer. */

          if (rq->rq_packbuf == 0)
            return(lam_mkerr(MPI_ERR_BUFFER, EINVAL));
	}

        /* Otherwise allocate a buffer. */

	else {
          rq->rq_packbuf = malloc((unsigned) rq->rq_packsize);
          if (rq->rq_packbuf == 0)
            return(lam_mkerr(MPI_ERR_OTHER, errno));
	}

	return(MPI_SUCCESS);
}


/*
 *	lam_buffreebsend_
 *
 *	Function:	- free bsend buffer
 *	Accepts:	- request
 *	Returns:	- MPI_SUCCESS or error code
 */
int
lam_buffreebsend_(MPI_Request req)
{
/*
 * Handle zero length messages.
 */
	if ((req->rq_count == 0) || (req->rq_dtype->dt_size == 0)) {
		return(MPI_SUCCESS);
	}

	buf_unlink(req->rq_bsend);
	buf_free(req->rq_bsend);
	req->rq_packbuf = 0;

	return(MPI_SUCCESS);
}


/*
 *	lam_bufattach
 *
 *	Function:	- set the user buffer space
 *	Accepts:	- buffer
 *			- buffer size
 *	Returns:	- MPI_SUCCESS or error code
 */
int
lam_bufattach(void *buf, int size)
{
	unsigned long	inc;			/* alignment increment */

	if (userbuf) {
		return(lam_mkerr(MPI_ERR_OTHER, EBUSY));
	}
/*
 * Determine round up required for alignment.
 */
	if ((inc = (unsigned long) buf % sizeof(void *)) != 0) {
		inc = sizeof(void *) - inc;
	}

	if ((size > 0) && ((unsigned long) size >= HDRSIZE + inc)) {

		userbuf = buf;
		bufsize = size;

		size -= (int) inc;
		size /= sizeof(void *);
		size *= sizeof(void *);

		buffree = (struct _bsndhdr *) ((char *) buf + inc);
		buffree->bsh_req = 0;
		buffree->bsh_size = size - HDRSIZE;
		buffree->bsh_prev = 0;
		buffree->bsh_next = 0;

		return(MPI_SUCCESS);
	}
	else {
		return(lam_mkerr(MPI_ERR_OTHER, EINVAL));
	}
}


/*
 *	lam_bufdetach
 *
 *	Function:	- detach and return user buffer
 *			- block until all requests are done
 *	Accepts:	- ptr buffer
 *			- ptr buffer size
 *	Returns:	- MPI_SUCCESS or error code
 */
int
lam_bufdetach(void *pbuf, int *psize)
{
	int		err;			/* error code */
	struct _bsndhdr	*p;			/* favourite pointer */
/*
 * Block until all bsend requests are done.
 * Since all bsend requests are orphans, they will be cleaned up
 * by _mpi_req_advance().
 */
	while (buftop) {
/*
 * Mark all bsend requests as blocking and advance the system.
 */
		_mpi_req_blkclr();

		for (p = buftop; p; p = p->bsh_next) {
			_mpi_req_blkset(p->bsh_req);
		}

		err = _mpi_req_advance();
		if (err != MPI_SUCCESS) return(err);
	}
/*
 * Return the buffer to the user.
 */
	*((void **) pbuf) = userbuf;
	*psize = bufsize;

	userbuf = 0;
	bufsize = 0;
	buftop = bufend = buffree = 0;
 
	return(MPI_SUCCESS);
}


/*
 *	buf_alloc
 *
 *	Function:	- allocate a chunk of buffer
 *			- sizes are forced to multiple of pointer size
 *	Accepts:	- buffer size
 *	Returns:	- ptr allocated buffer header or NULL
 */
static struct _bsndhdr *
buf_alloc(int size)
{
	struct _bsndhdr	*pchunk;		/* ptr allocated chunk */
	struct _bsndhdr	*pcur;			/* ptr current block */
	struct _bsndhdr	*pprev;			/* ptr previous block */
/*
 * Round up the size to a multiple of pointer size.
 */
	size = (size + sizeof(void *) - 1) / sizeof(void *);
	size *= sizeof(void *);
/*
 * Loop searching for a big-enough free chunk.
 */
	for (pcur = buffree, pprev = 0; pcur; pcur = pcur->bsh_next) {
		if (pcur->bsh_size >= size) break;
		pprev = pcur;
	}

	if (pcur == 0) return((struct _bsndhdr *) 0);
/*
 * Take a slice from the free chunk.
 */
	if ((pcur->bsh_size - size) >= (2 * HDRSIZE)) {
		pchunk = (struct _bsndhdr *)
				(((char *) pcur) + pcur->bsh_size - size);
		pchunk->bsh_size = size;
		pcur->bsh_size -= size + HDRSIZE;
	}
/*
 * Chunk not worth slicing, take it all.
 */
	else {
		pchunk = pcur;

		if (pprev == 0) {
			buffree = pcur->bsh_next;
		} else {
			pprev->bsh_next = pcur->bsh_next;
		}
	}

	pchunk->bsh_req = 0;
	pchunk->bsh_next = 0;
	pchunk->bsh_prev = 0;

	return(pchunk);
}


/*
 *	buf_free
 *
 *	Function:	- free a chunk of buffer
 *			- add it to the sorted free list
 *			- coalesce with up/down neighbours if possible
 *	Accepts:	- ptr chunk
 */
static void
buf_free(struct _bsndhdr *pchunk)
{
	struct _bsndhdr	*pcur;			/* ptr current chunk */
	struct _bsndhdr	*pprev;			/* ptr previous chunk */
/*
 * Locate the chunk's place in the free list.
 */
	for (pcur = buffree, pprev = 0; pcur; pcur = pcur->bsh_next) {
		if (((long) pcur) > ((long) pchunk)) break;
		pprev = pcur;
	}
/*
 * Link the chunk in the list.
 */
	if (pprev == 0) {
		buffree = pchunk;
	} else {
		pprev->bsh_next = pchunk;
	}

	pchunk->bsh_next = pcur;
/*
 * Coalesce with previous if possible.
 */
	if (pprev) {
		if ( ((long) pchunk) ==
			(((long) pprev) + HDRSIZE + pprev->bsh_size) ) {

			pprev->bsh_next = pchunk->bsh_next;
			pprev->bsh_size += HDRSIZE + pchunk->bsh_size;
			pchunk = pprev;
		}
	}
/*
 * Coalesce with next if possible.
 */
	if (pcur) {
		if (((long) pcur) ==
			(((long) pchunk) + HDRSIZE + pchunk->bsh_size) ) {

			pchunk->bsh_next = pcur->bsh_next;
			pchunk->bsh_size += HDRSIZE + pcur->bsh_size;
		}
	}
}


/*
 *	buf_append
 *
 *	Function:	- append buffer to list of requests
 *	Accepts:	- ptr buffer
 */
static void
buf_append(struct _bsndhdr *pbuf)
{
	pbuf->bsh_prev = bufend;
	pbuf->bsh_next = 0;

	if (bufend == 0) buftop = pbuf;
	else bufend->bsh_next = pbuf;

	bufend = pbuf;
}


/*
 *	buf_unlink
 *
 *	Function:	- unlink buffer from list of requests
 *	Accepts:	- ptr buffer entry
 */
static void
buf_unlink(struct _bsndhdr *pbuf)
{
	struct _bsndhdr	*pprev;			/* ptr previous buffer */
	struct _bsndhdr	*pnext;			/* ptr next buffer */

	pprev = pbuf->bsh_prev;
	pnext = pbuf->bsh_next;

	if (pprev == 0) buftop = pnext;
	else pprev->bsh_next = pnext;

	if (pnext) pnext->bsh_prev = pprev;
	else bufend = pprev;
}