summaryrefslogtreecommitdiff
path: root/ghc/rts/parallel/0Unpack.c
blob: fc4a8e50c37338a5437882ee93b033df2b8a4568 (plain)
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
/*
  Time-stamp: <Wed Jan 12 2000 13:29:08 Stardate: [-30]4193.85 hwloidl>

  Unpacking closures which have been exported to remote processors

  This module defines routines for unpacking closures in the parallel
  runtime system (GUM).

  In the case of GrAnSim, this module defines routines for *simulating* the
  unpacking of closures as it is done in the parallel runtime system.
*/

/* 
   Code in this file has been merged with Pack.c 
*/

#if 0

//@node Unpacking closures, , ,
//@section Unpacking closures

//@menu
//* Includes::			
//* Prototypes::		
//* GUM code::			
//* GranSim Code::		
//* Index::			
//@end menu
//*/

//@node Includes, Prototypes, Unpacking closures, Unpacking closures
//@subsection Includes

#include "Rts.h"
#include "RtsFlags.h"
#include "GranSimRts.h"
#include "ParallelRts.h"
#include "ParallelDebug.h"
#include "FetchMe.h"
#include "Storage.h"

//@node Prototypes, GUM code, Includes, Unpacking closures
//@subsection Prototypes

void     InitPacking(void);
# if defined(PAR)
void            InitPackBuffer(void);
# endif
/* Interface for ADT of closure queues */
void    	  AllocClosureQueue(nat size);
void    	  InitClosureQueue(void);
rtsBool 	  QueueEmpty(void);
void    	  QueueClosure(StgClosure *closure);
StgClosure *DeQueueClosure(void);

StgPtr AllocateHeap(nat size);

//@node GUM code, GranSim Code, Prototypes, Unpacking closures
//@subsection GUM code

#if defined(PAR) 

//@node Local Definitions,  , GUM code, GUM code
//@subsubsection Local Definitions

//@cindex PendingGABuffer
static globalAddr *PendingGABuffer;  
/* is initialised in main; */

//@cindex InitPendingGABuffer
void
InitPendingGABuffer(size)
nat size; 
{
  PendingGABuffer = (globalAddr *) 
                      stgMallocBytes((size-PACK_HDR_SIZE)*2*sizeof(globalAddr),
				     "InitPendingGABuffer");
}

/*
  @CommonUp@ commons up two closures which we have discovered to be
  variants of the same object.  One is made an indirection to the other.  */

//@cindex CommonUp
void
CommonUp(StgClosure *src, StgClosure *dst)
{
  StgBlockingQueueElement *bqe;

  ASSERT(src != dst);
  switch (get_itbl(src)->type) {
  case BLACKHOLE_BQ:
    bqe = ((StgBlockingQueue *)src)->blocking_queue;
    break;

  case FETCH_ME_BQ:
    bqe = ((StgFetchMeBlockingQueue *)src)->blocking_queue;
    break;
    
  case RBH:
    bqe = ((StgRBH *)src)->blocking_queue;
    break;
    
  case BLACKHOLE:
  case FETCH_ME:
    bqe = END_BQ_QUEUE;
    break;

  default:
    /* Don't common up anything else */
    return;
  }
  /* We do not use UPD_IND because that would awaken the bq, too */
  // UPD_IND(src, dst);
  updateWithIndirection(get_itbl(src), src, dst);
  //ASSERT(!IS_BIG_MOTHER(INFO_PTR(dst)));
  if (bqe != END_BQ_QUEUE)
    awaken_blocked_queue(bqe, src);
}

/*
  @UnpackGraph@ unpacks the graph contained in a message buffer.  It
  returns a pointer to the new graph.  The @gamap@ parameter is set to
  point to an array of (oldGA,newGA) pairs which were created as a result
  of unpacking the buffer; @nGAs@ is set to the number of GA pairs which
  were created.

  The format of graph in the pack buffer is as defined in @Pack.lc@.  */

//@cindex UnpackGraph
StgClosure *
UnpackGraph(packBuffer, gamap, nGAs)
rtsPackBuffer *packBuffer;
globalAddr **gamap;
nat *nGAs;
{
  nat size, ptrs, nonptrs, vhs;
  StgWord **buffer, **bufptr, **slotptr;
  globalAddr ga, *gaga;
  StgClosure *closure, *existing,
             *graphroot, *graph, *parent;
  StgInfoTable *ip, *oldip;
  nat bufsize, i,
      pptr = 0, pptrs = 0, pvhs;
  char str[80];

  InitPackBuffer();                  /* in case it isn't already init'd */
  graphroot = (StgClosure *)NULL;

  gaga = PendingGABuffer;

  InitClosureQueue();

  /* Unpack the header */
  bufsize = packBuffer->size;
  buffer = packBuffer->buffer;
  bufptr = buffer;

  /* allocate heap */
  if (bufsize > 0) {
    graph = allocate(bufsize);
    ASSERT(graph != NULL);
  }

  parent = (StgClosure *)NULL;

  do {
    /* This is where we will ultimately save the closure's address */
    slotptr = bufptr;

    /* First, unpack the next GA or PLC */
    ga.weight = (rtsWeight) *bufptr++;

    if (ga.weight > 0) {
      ga.payload.gc.gtid = (GlobalTaskId) *bufptr++;
      ga.payload.gc.slot = (int) *bufptr++;
    } else
      ga.payload.plc = (StgPtr) *bufptr++;
    
    /* Now unpack the closure body, if there is one */
    if (isFixed(&ga)) {
      /* No more to unpack; just set closure to local address */
      IF_PAR_DEBUG(pack,
		   belch("Unpacked PLC at %x", ga.payload.plc)); 
      closure = ga.payload.plc;
    } else if (isOffset(&ga)) {
      /* No more to unpack; just set closure to cached address */
      ASSERT(parent != (StgClosure *)NULL);
      closure = (StgClosure *) buffer[ga.payload.gc.slot];
    } else {
      /* Now we have to build something. */

      ASSERT(bufsize > 0);

      /*
       * Close your eyes.  You don't want to see where we're looking. You
       * can't get closure info until you've unpacked the variable header,
       * but you don't know how big it is until you've got closure info.
       * So...we trust that the closure in the buffer is organized the
       * same way as they will be in the heap...at least up through the
       * end of the variable header.
       */
      ip = get_closure_info(bufptr, &size, &ptrs, &nonptrs, &vhs, str);
	  
      /* 
	 Remember, the generic closure layout is as follows:
	 +-------------------------------------------------+
	 | FIXED HEADER | VARIABLE HEADER | PTRS | NON-PRS |
	 +-------------------------------------------------+
      */
      /* Fill in the fixed header */
      for (i = 0; i < FIXED_HS; i++)
	((StgPtr)graph)[i] = *bufptr++;

      if (ip->type == FETCH_ME)
	size = ptrs = nonptrs = vhs = 0;

      /* Fill in the packed variable header */
      for (i = 0; i < vhs; i++)
	((StgPtr)graph)[FIXED_HS + i] = *bufptr++;

      /* Pointers will be filled in later */

      /* Fill in the packed non-pointers */
      for (i = 0; i < nonptrs; i++)
	((StgPtr)graph)[FIXED_HS + i + vhs + ptrs] = *bufptr++;
                
      /* Indirections are never packed */
      // ASSERT(INFO_PTR(graph) != (W_) Ind_info_TO_USE);

      /* Add to queue for processing */
      QueueClosure(graph);
	
      /*
       * Common up the new closure with any existing closure having the same
       * GA
       */

      if ((existing = GALAlookup(&ga)) == NULL) {
	globalAddr *newGA;
	/* Just keep the new object */
	IF_PAR_DEBUG(pack,
		     belch("Unpacking new (%x, %d, %x)\n", 
			   ga.payload.gc.gtid, ga.payload.gc.slot, ga.weight));

	closure = graph;
	newGA = setRemoteGA(graph, &ga, rtsTrue);
	if (ip->type == FETCH_ME)
	  // FETCHME_GA(closure) = newGA;
	  ((StgFetchMe *)closure)->ga = newGA;
      } else {
	/* Two closures, one global name.  Someone loses */
	oldip = get_itbl(existing);

	if ((oldip->type == FETCH_ME || IS_BLACK_HOLE(existing)) &&
	    ip->type != FETCH_ME) {

	  /* What we had wasn't worth keeping */
	  closure = graph;
	  CommonUp(existing, graph);
	} else {

	  /*
	   * Either we already had something worthwhile by this name or
	   * the new thing is just another FetchMe.  However, the thing we
	   * just unpacked has to be left as-is, or the child unpacking
	   * code will fail.  Remember that the way pointer words are
	   * filled in depends on the info pointers of the parents being
	   * the same as when they were packed.
	   */
	  IF_PAR_DEBUG(pack,
		       belch("Unpacking old (%x, %d, %x), keeping %#lx", 
			     ga.payload.gc.gtid, ga.payload.gc.slot, ga.weight,
			     existing));

	  closure = existing;
	}
	/* Pool the total weight in the stored ga */
	(void) addWeight(&ga);
      }

      /* Sort out the global address mapping */
      if ((ip_THUNK(ip) && !ip_UNPOINTED(ip)) || 
	  (ip_MUTABLE(ip) && ip->type != FETCH_ME)) {
	/* Make up new GAs for single-copy closures */
	globalAddr *newGA = makeGlobal(closure, rtsTrue);
	
	ASSERT(closure == graph);

	/* Create an old GA to new GA mapping */
	*gaga++ = ga;
	splitWeight(gaga, newGA);
	ASSERT(gaga->weight == 1L << (BITS_IN(unsigned) - 1));
	gaga++;
      }
      graph += FIXED_HS + (size < MIN_UPD_SIZE ? MIN_UPD_SIZE : size);
    }

    /*
     * Set parent pointer to point to chosen closure.  If we're at the top of
     * the graph (our parent is NULL), then we want to arrange to return the
     * chosen closure to our caller (possibly in place of the allocated graph
     * root.)
     */
    if (parent == NULL)
      graphroot = closure;
    else
      ((StgPtr)parent)[FIXED_HS + pvhs + pptr] = (StgWord) closure;

    /* Save closure pointer for resolving offsets */
    *slotptr = (StgWord) closure;

    /* Locate next parent pointer */
    pptr++;
    while (pptr + 1 > pptrs) {
      parent = DeQueueClosure();

      if (parent == NULL)
	break;
      else {
	(void) get_closure_info(parent, &size, &pptrs, &nonptrs,
					&pvhs, str);
	pptr = 0;
      }
    }
  } while (parent != NULL);

  ASSERT(bufsize == 0 || graph - 1 <= SAVE_Hp);

  *gamap = PendingGABuffer;
  *nGAs = (gaga - PendingGABuffer) / 2;

  /* ToDo: are we *certain* graphroot has been set??? WDP 95/07 */
  ASSERT(graphroot!=NULL);
  return (graphroot);
}
#endif  /* PAR */

//@node GranSim Code, Index, GUM code, Unpacking closures
//@subsection GranSim Code

/*
   For GrAnSim: In general no actual unpacking should be necessary. We just
   have to walk over the graph and set the bitmasks appropriately. -- HWL */

//@node Unpacking,  , GranSim Code, GranSim Code
//@subsubsection Unpacking

#if defined(GRAN)
void
CommonUp(StgClosure *src, StgClosure *dst)
{
  barf("CommonUp: should never be entered in a GranSim setup");
}

/* This code fakes the unpacking of a somewhat virtual buffer */
StgClosure*
UnpackGraph(buffer)
rtsPackBuffer* buffer;
{
  nat size, ptrs, nonptrs, vhs,
      bufptr = 0;
  StgClosure *closure, *graphroot, *graph;
  StgInfoTable *ip;
  StgWord bufsize, unpackedsize,
          pptr = 0, pptrs = 0, pvhs;
  StgTSO* tso;
  char str[240], str1[80];
  int i;

  bufptr = 0;
  graphroot = buffer->buffer[0];

  tso = buffer->tso;

  /* Unpack the header */
  unpackedsize = buffer->unpacked_size;
  bufsize = buffer->size;

  IF_GRAN_DEBUG(pack,
		belch("<<< Unpacking <<%d>> (buffer @ %p):\n    (root @ %p, PE %d,size=%d), demanded by TSO %d (%p)[PE %d]",
		      buffer->id, buffer, graphroot, where_is(graphroot), 
		      bufsize, tso->id, tso, 
		      where_is((StgClosure *)tso)));

  do {
    closure = buffer->buffer[bufptr++]; /* that's all we need for GrAnSim -- HWL */
      
    /* Actually only ip is needed; rest is useful for TESTING -- HWL */
    ip = get_closure_info(closure, 
			  &size, &ptrs, &nonptrs, &vhs, str);
      
    IF_GRAN_DEBUG(pack,
		  sprintf(str, "**    (%p): Changing bitmask[%s]: 0x%x ",
			  closure, (closure_HNF(closure) ? "NF" : "__"),
			  PROCS(closure)));

    if (ip->type == RBH) {
      closure->header.gran.procs = PE_NUMBER(CurrentProc);    /* Move node */
      
      IF_GRAN_DEBUG(pack,
		    strcat(str, " (converting RBH) ")); 

      convertFromRBH(closure);   /* In GUM that's done by convertToFetchMe */
    } else if (IS_BLACK_HOLE(closure)) {
      closure->header.gran.procs |= PE_NUMBER(CurrentProc); /* Copy node */
    } else if ( closure->header.gran.procs & PE_NUMBER(CurrentProc) == 0 ) {
      if (closure_HNF(closure))
	closure->header.gran.procs |= PE_NUMBER(CurrentProc); /* Copy node */
      else
	closure->header.gran.procs = PE_NUMBER(CurrentProc);  /* Move node */
    }

    IF_GRAN_DEBUG(pack,
		  sprintf(str1, "0x%x",   PROCS(closure)); strcat(str, str1));
    IF_GRAN_DEBUG(pack, belch(str));
    
  } while (bufptr<buffer->size) ;   /*  (parent != NULL);  */

  /* In GrAnSim we allocate pack buffers dynamically! -- HWL */
  free(buffer->buffer);
  free(buffer);

  IF_GRAN_DEBUG(pack,
		belch("PrintGraph of %p is:", graphroot); PrintGraph(graphroot,0));

  return (graphroot);
}
#endif  /* GRAN */
#endif

//@node Index,  , GranSim Code, Unpacking closures
//@subsection Index

//@index
//* CommonUp::  @cindex\s-+CommonUp
//* InitPendingGABuffer::  @cindex\s-+InitPendingGABuffer
//* PendingGABuffer::  @cindex\s-+PendingGABuffer
//* UnpackGraph::  @cindex\s-+UnpackGraph
//@end index