File: clog_merge.c

package info (click to toggle)
mpich 1.1.0-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 22,116 kB
  • ctags: 27,349
  • sloc: ansic: 193,435; sh: 11,172; fortran: 6,545; makefile: 5,801; cpp: 5,020; tcl: 3,548; asm: 3,536; csh: 1,079; java: 614; perl: 183; awk: 168; sed: 70; f90: 62
file content (412 lines) | stat: -rw-r--r-- 12,683 bytes parent folder | download | duplicates (2)
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
#if defined(HAVE_CONFIG_H) && !defined(MPICHCONF_INC)
/* This includes the definitions found by configure, and can be found in
   the library directory (lib/$ARCH/$COMM) corresponding to this configuration
 */
#define MPICHCONF_INC
#include "mpichconf.h"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

#if defined(NEEDS_STDLIB_PROTOTYPES) 
#include "protofix.h"
#endif

#include "clog_merge.h"
#include "clogimpl.h"
#include "mpi.h"

/*********************** global variables for merge functions ***************/

static int    me, nprocs, parent, lchild, rchild;	/* tree info */
static double *mybuf, *lbuf, *rbuf, *outbuf; /* buffers for log records */
static double *myptr, *lptr, *rptr, *outptr; /* pointers into each buffer */
static double *outend;			     /* end of output buffer */
static int    inputs;			    /* number of inputs to the merge */
static double timediffs[CMERGE_MAXPROCS];  /* array of time shifts, averaged */
static double newdiffs[CMERGE_MAXPROCS];   /* array of time shifts, once  */
static int    logfd;			   /* the log file */

/*@
    CLOG_mergelogs - merge individual logfiles into one via messages

first argument says whether to do time-shifiting or not
second arg is filename

On process 0 in MPI_COMM_WORLD, collect logs from other processes and merge
them with own log.  Timestamps are assumed to be already adjusted on both
incoming logs and the master''s.  On the other processes, fill in length and
process id''s and send them, a block at a time, to the master.  The master 
writes out the merged log.

@*/
void CLOG_mergelogs( shift, execfilename, logtype )
int shift;
char *execfilename;
int logtype;
{
    MPI_Status logstatus;
    double maxtime = CLOG_MAXTIME;  /* place to hold maxtime, dummy record */
    char logfilename[256];
    int i;
    
    /* set up tree */
    PMPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    PMPI_Comm_rank(MPI_COMM_WORLD, &me);
    CLOG_treesetup(me, nprocs, &parent, &lchild, &rchild);
    /* printf("merging on %d at time %f\n", me, MPI_Wtime()); */

    if (parent == -1) {		/* open output logfile at root */
	strncpy(logfilename, execfilename, 256);
	strcat(logfilename, ".clog"); 
	if ((logfd = open(logfilename, O_CREAT|O_WRONLY|O_TRUNC, 0664)) == -1)
	{
	    printf("could not open file %s for logging\n",logfilename);
	    exit(-1);
	}
    }

    for (i = 0; i < nprocs; i++)
	timediffs[i] = 0;
    
    /* calculate time shifts if required */
    if (shift == CMERGE_SHIFT) {
	CLOG_csync(0, timediffs);
	/* CLOG_printdiffs(timediffs); */
    }
    CLOG_LOGTIMESHIFT( timediffs[me] );

    /* set up buffers, send if a leaf */
    CLOG_currbuff = CLOG_first;	/* reinitialize pointer to first local buff */
    mybuf  = (double *) CLOG_first->data;
    myptr  = mybuf;
    CLOG_procbuf(mybuf);	/* do postprocessing (procids, lengths) */
    inputs = 1;			/* always have at least own buffer */

    outptr = outbuf = (double *) MALLOC ( CLOG_BLOCK_SIZE );
    outend = (void *) ((char *) outptr + CLOG_BLOCK_SIZE);

    if (lchild != -1) {
	inputs++;
	lptr = lbuf = (double *) MALLOC ( CLOG_BLOCK_SIZE );
	PMPI_Recv(lbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE, 
		 lchild, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD, &logstatus);
    }
    else
	lptr = &maxtime;

    if (rchild != -1) {
	inputs++;
	rptr = rbuf = (double *) MALLOC ( CLOG_BLOCK_SIZE );
	PMPI_Recv(rbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE, 
		 rchild, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD, &logstatus);
    }
    else
	rptr = &maxtime;

    /* Do the merge.  Abstractly, we do this one record at a time, letting
       the CLOG_cput routine buffer input from children and output
       to the parent.  We decrement the number of inputs when we hit the end
       of log record in a buffer, and we are done when the number of inputs
       reaches 0.  The pointers mybuf, lbuf, and rbuf are pointing to the
       timestamp of the next record in each buffer, and outbuf is pointing
       at the place to put the next output record.
       */

    while (inputs > 0) {
	if (*lptr <= *rptr)
	    if (*lptr <= *myptr)
		CLOG_cput(&lptr);
            else
		CLOG_cput(&myptr);
	else
	    if (*rptr <= *myptr)
		CLOG_cput(&rptr);
            else
		CLOG_cput(&myptr);
    }
    CLOG_mergend();  /* add trailer (CLOG_ENDLOG) and flush output buffer */
    
    /* now we have a clog file;  if it should be an alog file, convert it and
       delete the clog file. */
    if (logtype == ALOG_LOG) {
	if (parent == -1) {
	    clog2alog( execfilename );
	}
	unlink(logfilename);
    }
}

/*@
    CLOG_treesetup - locally determine parent and children in binary tree

Input parameters

.  self - calling process''s id
.  np   - total number of processes in tree

Output parameters

.  parent - parent in binary tree (or -1 if root)
.  lchild - left child in binary tree (or -1 if none)
.  rchild - right child in binary tree (or -1 if none)

@*/
void CLOG_treesetup( self, nprocs, parent, lchild, rchild)
int self, nprocs, *parent, *lchild, *rchild;
{
    if (self == 0)
	*parent = -1;
    else
	*parent = (self - 1) / 2;

    if ((*lchild = (2 * self) + 1) > nprocs - 1)
	*lchild = -1;

    if ((*rchild = (2 * self) + 2) > nprocs - 1)
	*rchild = -1;
}


/*@
    CLOG_procbuf - postprocess a buffer of log records before merging

This function fills in fields in log records that were left out during
actual logging to save memory accesses.  Typical fields are the process
id and the lengths of records that are known by predefined type.  This is
also where we will adjust timestamps.

Input parameter

.  address of the buffer to be processed

@*/
void CLOG_procbuf( buf )
double *buf;
{
    CLOG_HEADER *h;

    h = (CLOG_HEADER *) buf;
    while (h->rectype != CLOG_ENDBLOCK && h->rectype != CLOG_ENDLOG) {
	h->procid = me;
	h->timestamp = h->timestamp + timediffs[me];
	h->length = CLOG_reclen(h->rectype);  /* in doubles */
	buf = (double *) h + h->length; 
	h = (CLOG_HEADER *) buf;
    }	
    /* fix trailer record, either block or log */
    h->procid = me;
    h->length = CLOG_reclen(h->rectype);  /* in doubles */
    h->timestamp = h->timestamp + timediffs[me];
}

/*@
    CLOG_mergend - finish log processing
@*/
void CLOG_mergend()
{
    /* put on end-of-log record */
    ((CLOG_HEADER *) outptr)->timestamp = CLOG_MAXTIME;
    ((CLOG_HEADER *) outptr)->rectype   = CLOG_ENDLOG;
    ((CLOG_HEADER *) outptr)->procid    = me;
    ((CLOG_HEADER *) outptr)->length    = CLOG_reclen(CLOG_ENDLOG);

    if (parent != -1) {
	PMPI_Send(outbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE,
		 parent, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD);
	/* printf("%d sent to %d\n", me, parent); */
    }
    else {
	CLOG_output(outbuf); /* final output of last block at root */
	close(logfd);
    }
    FREE (outbuf);		/* free output buffer */
}

/*@
    CLOG_Output - output a block of the log
@*/
void CLOG_output( buf )
double *buf;
{
    int rc;
    /* CLOG_dumpblock(buf);	 temporary print for debugging*/
    rc = write( logfd, buf, CLOG_BLOCK_SIZE );	/* write block to file */
    if ( rc != CLOG_BLOCK_SIZE ) {
	printf( stderr, "write failed for clog logging, rc = %d\n", rc );
	exit (-1);
    }
}

/*@
    CLOG_cput - move a log record from one of the input buffers to the output

This function moves records from one of the three buffers being merged into
the output buffer.  When the output buffer is filled, it is sent to the
parent.  A separate output routine handles output on the root.  If the input
buffer is emptied (endblock record is read) and corresponds to a child, a new
buffer is received from the child.  When an endlog record is read on the input
buffer, the number of sources is decremented and the time is set to positive
infinity so that the empty input source will never have the lowest time.

At entry we assume that *p is pointing to a log record that is not an
end-of-block record, and that outbuf is pointing to a buffer that has
room in it for the record.  We ensure that these conditions are met on
exit as well, by sending (or writing, if we are the root) and receiving
blocks as necessary.

Input parameters

.  pointer to the record to be moved into the output buffer

@*/
void CLOG_cput( ptr )
double **ptr;
{
    double *p;
    CLOG_HEADER *h;
    MPI_Status logstatus;

    p = *ptr;
    h = (CLOG_HEADER *) p;

    if (h->rectype == CLOG_ENDLOG) {
	h->timestamp = CLOG_MAXTIME;
	inputs--;
	return;
    }
    
    /* printf("putting record with timestamp= %f\n",*p); */
    memcpy(outptr, p, h->length * sizeof(double)); 
    outptr += h->length; /* skip to next output record */

    if (((char *) outptr + CLOG_MAX_REC_LEN) >= (char *) outend ) {
	/* put on end-of-block-record */
	((CLOG_HEADER *) outptr)->timestamp = h->timestamp; /* use prev rec. */
	((CLOG_HEADER *) outptr)->rectype   = CLOG_ENDBLOCK;
	((CLOG_HEADER *) outptr)->procid    = me;
	((CLOG_HEADER *) outptr)->length    = CLOG_reclen(CLOG_ENDBLOCK);

        if (parent != -1) {
	    PMPI_Send(outbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE,
		     parent, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD);
	    /* printf("%d sent to %d\n", me, parent); */
	}
	else {
	    CLOG_output(outbuf); /* final output of block */
	}
	outptr = outbuf;
    }

    p += h->length;		/* skip to next input record */
    *ptr = p;

    h = (CLOG_HEADER *) p;
    if (h->rectype == CLOG_ENDBLOCK) {
	if (ptr == &myptr) {
	    if (!CLOG_currbuff->next) { /* if no next buffer, endlog follows */
		p += CLOG_reclen(CLOG_ENDBLOCK);
		printf("[%d] length of endblock = %d\n", me, CLOG_reclen(CLOG_ENDBLOCK));
		*ptr = p;
	    }
	    else {
		p = (double *) CLOG_currbuff;
		CLOG_currbuff = CLOG_currbuff->next;
		FREE( p );	        /* free local block just processed */
		*ptr = (double *) CLOG_currbuff->data;
		CLOG_procbuf(*ptr);	/* process next local block */
	    }
	}
	if (ptr == &lptr) {
	    PMPI_Recv(lbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE, 
		     lchild, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD, &logstatus);
	    *ptr = lbuf;
	}
	if (ptr == &rptr) {
	    PMPI_Recv(rbuf, (CLOG_BLOCK_SIZE / sizeof (double)), MPI_DOUBLE, 
		     rchild, CMERGE_LOGBUFTYPE, MPI_COMM_WORLD, &logstatus);
	    *ptr = rbuf;
	}
    }
}

/*@
    CLOG_csync - synchronize clocks for adjusting times in merge

This version is sequential and non-scalable.  The root process serially
synchronizes with each slave, using the first algorithm in Gropp, "Scalable
clock synchronization on distributed processors without a common clock".
The array is calculated on the root but broadcast and returned on all
processes.

Inout Parameters:

. root      - process to serve as master
. timediffs - array of doubles to be filled in

@*/

void CLOG_csync( root, diffs )
int root;
double diffs[];
{
    int myrank, numprocs, numtests = 3, i, j;
    double time_1, time_2, time_i, bestgap, bestshift;
    int dummy;
    MPI_Status status;

    PMPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    PMPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    PMPI_Barrier(MPI_COMM_WORLD);
    PMPI_Barrier(MPI_COMM_WORLD); /* approximate common starting point */

    if (root == myrank) {	/* I am the master, but not nec. 0 */
	for (i = 0; i < numprocs; i++) {
	    if (i != myrank) {
		bestgap = 1000000.0; /* infinity, fastest turnaround so far */
		for (j = 0; j < numtests; j++) {
		    PMPI_Send(&dummy, 0, MPI_INT, i, MASTER_READY, MPI_COMM_WORLD);
		    PMPI_Recv(&dummy, 0, MPI_INT, i, SLAVE_READY,  MPI_COMM_WORLD,
			     &status); 
		    time_1 = CLOG_timestamp();
		    PMPI_Send(&dummy,  0, MPI_INT, i, TIME_QUERY,  MPI_COMM_WORLD);
		    PMPI_Recv(&time_i, 1, MPI_DOUBLE, i, TIME_ANSWER,
			     MPI_COMM_WORLD, &status);
		    time_2 = CLOG_timestamp();
		    if ((time_2 - time_1) < bestgap) {
			bestgap   = time_2 - time_1;
			bestshift =  0.5 * (time_2 + time_1) - time_i;
		    }
		}
		timediffs[i] = bestshift;
	    }
	    else 		/* i = root */
		timediffs[i] = 0;
	}
    }
    else {			/* not the root */
	for (j = 0; j < numtests; j++) {
	    PMPI_Recv(&dummy, 0, MPI_INT, root, MASTER_READY, MPI_COMM_WORLD, &status); 
	    PMPI_Send(&dummy, 0, MPI_INT, root, SLAVE_READY, MPI_COMM_WORLD);
	    PMPI_Recv(&dummy, 0, MPI_INT, root, TIME_QUERY, MPI_COMM_WORLD, &status);
	    time_i = CLOG_timestamp();
	    PMPI_Send(&time_i, 1, MPI_DOUBLE, root, TIME_ANSWER, MPI_COMM_WORLD);
	}
    }
    PMPI_Bcast(timediffs, CMERGE_MAXPROCS, MPI_DOUBLE, root, MPI_COMM_WORLD);
}

void CLOG_printdiffs( diffs )
double diffs[];
{
    int i, numprocs, me;

    PMPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    PMPI_Comm_rank(MPI_COMM_WORLD, &me);
    printf("[%d] time shift array:  ", me);
    for (i = 0; i < numprocs; i++)
	printf("%f ", diffs[i]);
    printf("\n");
}