add Outputable instance for OccIfaceEq
[ghc-hetmet.git] / rts / parallel / 0Parallel.h
1 /*
2   Time-stamp: <Mon Oct 04 1999 14:50:28 Stardate: [-30]3692.88 hwloidl>
3  
4   Definitions for parallel machines.
5
6 This section contains definitions applicable only to programs compiled
7 to run on a parallel machine, i.e. on GUM. Some of these definitions
8 are also used when simulating parallel execution, i.e. on GranSim.
9   */
10
11 /*
12   ToDo: Check the PAR specfic part of this file 
13         Move stuff into Closures.h and ClosureMacros.h 
14         Clean-up GRAN specific code
15   -- HWL
16   */
17
18 #ifndef PARALLEL_H
19 #define PARALLEL_H
20
21 #if defined(PAR) || defined(GRAN)        /* whole file */
22
23 #include "Rts.h"
24 #include "GranSim.h"
25 //#include "ClosureTypes.h"
26
27 //@menu
28 //* Basic definitions::         
29 //* Externs and types::         
30 //* Dummy defs::                
31 //* Par specific fixed headers::  
32 //* Parallel only heap objects::  
33 //* Packing definitions::       
34 //* End of File::               
35 //@end menu
36 //*/
37
38 //@node Basic definitions, Externs and types
39 //@section Basic definitions
40
41 /* SET_PAR_HDR and SET_STATIC_PAR_HDR now live in ClosureMacros.h */
42
43 /* Needed for dumping routines */
44 #if defined(PAR)
45 # define TIME                      ullong
46 # define CURRENT_TIME              msTime()
47 # define TIME_ON_PROC(p)           msTime()
48 # define CURRENT_PROC              thisPE
49 # define BINARY_STATS              RtsFlags.ParFlags.granSimStats_Binary
50 #elif defined(GRAN)
51 # define TIME                      rtsTime
52 # define CURRENT_TIME              CurrentTime[CurrentProc]
53 # define TIME_ON_PROC(p)           CurrentTime[p]
54 # define CURRENT_PROC              CurrentProc
55 # define BINARY_STATS              RtsFlags.GranFlags.granSimStats_Binary
56 #endif
57
58 #if defined(PAR)
59 #  define MAX_PES       256             /* Maximum number of processors */
60         /* MAX_PES is enforced by SysMan, which does not
61            allow more than this many "processors".
62            This is important because PackGA [GlobAddr.lc]
63            **assumes** that a PE# can fit in 8+ bits.
64         */
65 #endif
66
67 //@node Externs and types, Dummy defs, Basic definitions
68 //@section Externs and types
69
70 #if defined(PAR)
71 /* GUM: one spark queue on each PE, and each PE sees only its own spark queue */
72 extern rtsSparkQ pending_sparks_hd;
73 extern rtsSparkQ pending_sparks_tl;
74 #elif defined(GRAN)
75 /* GranSim: a globally visible array of spark queues */
76 extern rtsSparkQ pending_sparks_hds[];
77 extern rtsSparkQ pending_sparks_tls[];
78 #endif
79 extern unsigned int /* nat */ spark_queue_len(PEs proc);
80
81 extern StgInt SparksAvail;     /* How many sparks are available */
82
83 /* prototypes of spark routines */
84 /* ToDo: check whether all have to be visible -- HWL */
85 #if defined(GRAN)
86 rtsSpark *newSpark(StgClosure *node, StgInt name, StgInt gran_info, StgInt size_info, StgInt par_info, StgInt local);
87 void disposeSpark(rtsSpark *spark);
88 void disposeSparkQ(rtsSparkQ spark);
89 void add_to_spark_queue(rtsSpark *spark);
90 void delete_from_spark_queue (rtsSpark *spark);
91 #endif
92
93 #define STATS_FILENAME_MAXLEN   128
94
95 /* Where to write the log file */
96 //extern FILE *gr_file;
97 extern char gr_filename[STATS_FILENAME_MAXLEN];
98
99 #if defined(GRAN)
100 int init_gr_simulation(char *rts_argv[], int rts_argc, char *prog_argv[], int prog_argc);
101 void end_gr_simulation(void);
102 #endif 
103
104 #if defined(PAR)
105 extern I_ do_sp_profile;
106
107 extern P_ PendingFetches;
108 extern GLOBAL_TASK_ID *PEs;
109
110 extern rtsBool IAmMainThread, GlobalStopPending;
111 extern rtsBool fishing;
112 extern GLOBAL_TASK_ID SysManTask;
113 extern int seed;                        /*pseudo-random-number generator seed:*/
114                                         /*Initialised in ParInit*/
115 extern I_ threadId;                     /*Number of Threads that have existed on a PE*/
116 extern GLOBAL_TASK_ID mytid;
117
118 extern int  nPEs;
119
120 extern rtsBool InGlobalGC;      /* Are we in the midst of performing global GC */
121
122 extern HashTable *pGAtoGALAtable;
123 extern HashTable *LAtoGALAtable;
124 extern GALA *freeIndirections;
125 extern GALA *liveIndirections;
126 extern GALA *freeGALAList;
127 extern GALA *liveRemoteGAs;
128 extern int thisPE;
129
130 void RunParallelSystem (StgPtr program_closure);
131 void initParallelSystem();
132 void SynchroniseSystem();
133
134 void registerTask (GLOBAL_TASK_ID gtid);
135 globalAddr *LAGAlookup (P_ addr);
136 P_ GALAlookup (globalAddr *ga);
137 globalAddr *MakeGlobal (P_ addr, rtsBool preferred);
138 globalAddr *setRemoteGA (P_ addr, globalAddr *ga, rtsBool preferred);
139 void splitWeight (globalAddr *to, globalAddr *from);
140 globalAddr *addWeight (globalAddr *ga);
141 void initGAtables();
142 W_ taskIDtoPE (GLOBAL_TASK_ID gtid);
143 void RebuildLAGAtable();
144
145 void *lookupHashTable (HashTable *table, StgWord key);
146 void insertHashTable (HashTable *table, StgWord key, void *data);
147 void freeHashTable (HashTable *table, void (*freeDataFun) ((void *data)));
148 HashTable *allocHashTable();
149 void *removeHashTable (HashTable *table, StgWord key, void *data);
150 #endif /* PAR */
151
152 /* Interface for dumping routines (i.e. writing to log file) */
153 void DumpGranEvent(GranEventType name, StgTSO *tso);
154 void DumpRawGranEvent(PEs proc, PEs p, GranEventType name, 
155                       StgTSO *tso, StgClosure *node, StgInt sparkname, StgInt len);
156 //void DumpEndEvent(PEs proc, StgTSO *tso, rtsBool mandatory_thread);
157
158 //@node Dummy defs, Par specific fixed headers, Externs and types
159 //@section Dummy defs
160
161 /*
162 Get this out of the way.  These are all null definitions.
163 */
164
165
166 //#  define GA_HDR_SIZE                 0 
167 //#  define GA(closure)                 /*nothing */ 
168   
169 //#  define SET_GA(closure,ga)          /* nothing */ 
170 //#  define SET_STATIC_GA(closure)      /* nothing */ 
171 //#  define SET_GRAN_HDR(closure,pe)      /* nothing */ 
172 //#  define SET_STATIC_PROCS(closure)   /* nothing */ 
173   
174 //#  define SET_TASK_ACTIVITY(act)      /* nothing */ 
175
176 #if defined(GRAN)
177
178 #  define GA_HDR_SIZE                   1
179
180 #  define PROCS_HDR_POSN                PAR_HDR_POSN
181 #  define PROCS_HDR_SIZE                1
182
183 /* Accessing components of the field */
184 #  define PROCS(closure)                ((closure)->header.gran.procs)
185 /* SET_PROCS is now SET_GRAN_HEADER in ClosureMacros.h. */
186 #endif
187
188
189 //@node Par specific fixed headers, Parallel only heap objects, Dummy defs
190 //@section Par specific fixed headers
191
192 /*
193 Definitions relating to the entire parallel-only fixed-header field.
194
195 On GUM, the global addresses for each local closure are stored in a separate
196 hash table, rather then with the closure in the heap.  We call @getGA@ to
197 look up the global address associated with a local closure (0 is returned
198 for local closures that have no global address), and @setGA@ to store a new
199 global address for a local closure which did not previously have one.
200 */
201
202 #if defined(PAR) 
203
204 #  define GA_HDR_SIZE                   0
205   
206 #  define GA(closure)                   getGA(closure)
207   
208 #  define SET_GA(closure, ga)             setGA(closure,ga)
209 #  define SET_STATIC_GA(closure)
210 #  define SET_GRAN_HDR(closure,pe)
211 #  define SET_STATIC_PROCS(closure)
212   
213 #  define MAX_GA_WEIGHT                 0       /* Treat as 2^n */
214   
215 W_ PackGA ((W_, int));
216    /* There was a PACK_GA macro here; but we turned it into the PackGA
217       routine [GlobAddr.lc] (because it needs to do quite a bit of
218       paranoia checking.  Phil & Will (95/08)
219    */
220
221 /* At the moment, there is no activity profiling for GUM.  This may change. */
222 #  define SET_TASK_ACTIVITY(act)        /* nothing */
223 #endif
224
225 //@node Parallel only heap objects, Packing definitions, Par specific fixed headers
226 //@section Parallel only heap objects
227
228 // NB: The following definitons are BOTH for GUM and GrAnSim -- HWL
229
230 /*   All in Closures.h and CLosureMacros.h */
231
232 //@node Packing definitions, End of File, Parallel only heap objects
233 //@section Packing definitions
234
235 //@menu
236 //* GUM::                       
237 //* GranSim::                   
238 //@end menu
239 //*/
240
241 //@node GUM, GranSim, Packing definitions, Packing definitions
242 //@subsection GUM
243
244 #if defined(PAR) 
245 /*
246 Symbolic constants for the packing code.
247
248 This constant defines how many words of data we can pack into a single
249 packet in the parallel (GUM) system.
250 */
251
252 //@menu
253 //* Externs::                   
254 //* Prototypes::                
255 //* Macros::                    
256 //@end menu
257 //*/
258
259 //@node Externs, Prototypes, GUM, GUM
260 //@subsubsection Externs
261
262 extern W_      *PackBuffer;      /* size: can be set via option */
263 extern long *buffer;             /* HWL_ */
264 extern W_ *freeBuffer;           /* HWL_ */
265 extern W_ *packBuffer;           /* HWL_ */
266
267 extern void    InitPackBuffer(STG_NO_ARGS);
268 extern void    InitMoreBuffers(STG_NO_ARGS);
269 extern void    InitPendingGABuffer(W_ size); 
270 extern void    AllocClosureQueue(W_ size);
271
272 //@node Prototypes, Macros, Externs, GUM
273 //@subsubsection Prototypes
274
275 void    InitPackBuffer();
276 P_      PackTSO (P_ tso, W_ *size);
277 P_      PackStkO (P_ stko, W_ *size);
278 P_      AllocateHeap (W_ size);          /* Doesn't belong */
279
280 void    InitClosureQueue ();
281 P_      DeQueueClosure();
282 void    QueueClosure (P_ closure);
283 rtsBool QueueEmpty();
284 void    PrintPacket (P_ buffer);
285
286 P_      get_closure_info (P_ closure, W_ *size, W_ *ptrs, W_ *nonptrs, W_ *vhs, char *type);
287
288 rtsBool isOffset (globalAddr *ga),
289         isFixed (globalAddr *ga);
290
291 void    doGlobalGC();
292
293 P_      PackNearbyGraph (P_ closure,W_ *size);
294 P_      UnpackGraph (W_ *buffer, globalAddr **gamap, W_ *nGAs);
295
296
297 //@node Macros,  , Prototypes, GUM
298 //@subsubsection Macros
299
300 #    define PACK_HEAP_REQUIRED  \
301       ((RtsFlags.ParFlags.packBufferSize - PACK_HDR_SIZE) / (PACK_GA_SIZE + _FHS) * (SPEC_HS + 2))
302
303 #  define MAX_GAS       (RtsFlags.ParFlags.packBufferSize / PACK_GA_SIZE)
304
305
306 #  define PACK_GA_SIZE  3       /* Size of a packed GA in words */
307                                 /* Size of a packed fetch-me in words */
308 #  define PACK_FETCHME_SIZE (PACK_GA_SIZE + FIXED_HS)
309
310 #  define PACK_HDR_SIZE 1       /* Words of header in a packet */
311
312 #  define PACK_PLC_SIZE 2       /* Size of a packed PLC in words */
313
314 #endif /* PAR */
315
316 //@node GranSim,  , GUM, Packing definitions
317 //@subsection GranSim
318
319 #if defined(GRAN)
320 /* ToDo: Check which of the PAR routines are needed in GranSim -- HWL */
321
322 //@menu
323 //* Types::                     
324 //* Prototypes::                
325 //* Macros::                    
326 //@end menu
327 //*/
328
329 //@node Types, Prototypes, GranSim, GranSim
330 //@subsubsection Types
331
332 typedef struct rtsPackBuffer_ {
333   StgInt /* nat */           size;
334   StgInt /* nat */           unpacked_size;
335   StgTSO       *tso;
336   StgClosure  **buffer;  
337 } rtsPackBuffer;
338
339 //@node Prototypes, Macros, Types, GranSim
340 //@subsubsection Prototypes
341
342
343 /* main packing functions */
344 /*
345 rtsPackBuffer *PackNearbyGraph(StgClosure* closure, StgTSO* tso, nat *packbuffersize);
346 rtsPackBuffer *PackOneNode(StgClosure* closure, StgTSO* tso, nat *packbuffersize);
347 void PrintPacket(rtsPackBuffer *buffer);
348 StgClosure *UnpackGraph(rtsPackBuffer* buffer);
349 */
350 /* important auxiliary functions */
351
352 //StgInfoTable *get_closure_info(StgClosure* node, nat *size, nat *ptrs, nat *nonptrs, nat *vhs, char *info_hdr_ty);
353 int IS_BLACK_HOLE(StgClosure* node);
354 StgClosure *IS_INDIRECTION(StgClosure* node);
355 int IS_THUNK(StgClosure* closure);
356 char *display_info_type(StgClosure* closure, char *str);
357
358 /* 
359 OLD CODE -- HWL
360 void  InitPackBuffer(void);
361 P_    AllocateHeap (W_ size);
362 P_    PackNearbyGraph (P_ closure, P_ tso, W_ *packbuffersize);
363 P_    PackOneNode (P_ closure, P_ tso, W_ *packbuffersize);
364 P_    UnpackGraph (P_ buffer);
365
366 void    InitClosureQueue (void);
367 P_      DeQueueClosure(void);
368 void    QueueClosure (P_ closure);
369 // rtsBool QueueEmpty();
370 void    PrintPacket (P_ buffer);
371 */
372
373 // StgInfoTable *get_closure_info(StgClosure* node, unsigned int /* nat */ *size, unsigned int /* nat */ *ptrs, unsigned int /* nat */ *nonptrs, unsigned int /* nat */ *vhs, char *info_hdr_ty);
374 // int /* rtsBool */ IS_BLACK_HOLE(StgClosure* node)          ;
375
376 //@node Macros,  , Prototypes, GranSim
377 //@subsubsection Macros
378
379 /* These are needed in the packing code to get the size of the packet
380    right. The closures itself are never built in GrAnSim. */
381 #  define FETCHME_VHS                           IND_VHS
382 #  define FETCHME_HS                            IND_HS
383   
384 #  define FETCHME_GA_LOCN                       FETCHME_HS
385   
386 #  define FETCHME_CLOSURE_SIZE(closure)         IND_CLOSURE_SIZE(closure)
387 #  define FETCHME_CLOSURE_NoPTRS(closure)               0L
388 #  define FETCHME_CLOSURE_NoNONPTRS(closure)    (IND_CLOSURE_SIZE(closure)-IND_VHS)
389   
390 #  define MAX_GAS       (RtsFlags.GranFlags.packBufferSize / PACK_GA_SIZE)
391 #  define PACK_GA_SIZE  3       /* Size of a packed GA in words */
392                                 /* Size of a packed fetch-me in words */
393 #  define PACK_FETCHME_SIZE (PACK_GA_SIZE + FIXED_HS)
394 #  define PACK_HDR_SIZE 4       /* Words of header in a packet */
395
396 #    define PACK_HEAP_REQUIRED  \
397       (RtsFlags.GranFlags.packBufferSize * sizeofW(StgClosure*) + \
398       2 * sizeofW(StgInt) + sizeofW(StgTSO*))
399
400 #    define PACK_FLAG_LOCN           0  
401 #    define PACK_TSO_LOCN            1
402 #    define PACK_UNPACKED_SIZE_LOCN  2
403 #    define PACK_SIZE_LOCN           3
404 #    define MAGIC_PACK_FLAG          0xfabc
405
406 #endif   /* GRAN */
407
408 //@node End of File,  , Packing definitions
409 //@section End of File
410
411 #endif /* defined(PAR) || defined(GRAN)         whole file */
412 #endif /* Parallel_H */
413
414