[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / includes / Parallel.h
1 /*
2   Time-stamp: <Tue Mar 06 2001 00:09:10 Stardate: [-30]6285.03 hwloidl>
3   $Id: Parallel.h,v 1.4 2001/03/22 03:51:09 hwloidl Exp $
4  
5   Definitions for GUM i.e. running on a parallel machine.
6
7   This section contains definitions applicable only to programs compiled
8   to run on a parallel machine, i.e. on GUM. Some of these definitions
9   are also used when simulating parallel execution, i.e. on GranSim.
10 */
11
12 #ifndef PARALLEL_H
13 #define PARALLEL_H
14
15 #if defined(PAR) || defined(GRAN)        /* whole file */
16
17 //@node Parallel definitions, End of File
18 //@section Parallel definitions
19
20 //@menu
21 //* Basic definitions::         
22 //* GUM::                       
23 //* GranSim::                   
24 //@end menu
25
26 //@node Basic definitions, GUM, Parallel definitions, Parallel definitions
27 //@subsection Basic definitions
28
29 /* This clashes with TICKY, but currently TICKY and PAR hate each other anyway */
30 #define _HS  sizeofW(StgHeader)
31
32 /* SET_PAR_HDR and SET_STATIC_PAR_HDR now live in ClosureMacros.h */
33
34 /* Needed for dumping routines */
35 #if defined(PAR)
36 # define NODE_STR_LEN              20
37 # define TIME_STR_LEN              120
38 # define TIME                      rtsTime
39 # define CURRENT_TIME              (msTime() - startTime)
40 # define TIME_ON_PROC(p)           (msTime() - startTime)
41 # define CURRENT_PROC              thisPE
42 # define BINARY_STATS              RtsFlags.ParFlags.ParStats.Binary
43 #elif defined(GRAN)
44 # define NODE_STR_LEN              20
45 # define TIME_STR_LEN              120
46 # define TIME                      rtsTime
47 # define CURRENT_TIME              CurrentTime[CurrentProc]
48 # define TIME_ON_PROC(p)           CurrentTime[p]
49 # define CURRENT_PROC              CurrentProc
50 # define BINARY_STATS              RtsFlags.GranFlags.GranSimStats.Binary
51 #endif
52
53 #if defined(PAR)
54 #  define MAX_PES       256             /* Maximum number of processors */
55         /* MAX_PES is enforced by SysMan, which does not
56            allow more than this many "processors".
57            This is important because PackGA [GlobAddr.lc]
58            **assumes** that a PE# can fit in 8+ bits.
59         */
60
61 # define SPARK_POOLS    2   /* no. of spark pools */
62 # define REQUIRED_POOL  0   /* idx of pool of mandatory sparks (concurrency) */
63 # define ADVISORY_POOL  1   /* idx of pool of advisory sparks (parallelism) */
64 #endif
65
66 //@menu
67 //* GUM::                       
68 //* GranSim::                   
69 //@end menu
70 //*/
71
72 //@node GUM, GranSim, Basic definitions, Parallel definitions
73 //@subsection GUM
74
75 #if defined(PAR) 
76 /*
77   Symbolic constants for the packing code.
78   
79   This constant defines how many words of data we can pack into a single
80   packet in the parallel (GUM) system.
81 */
82
83 //@menu
84 //* Types::                     
85 //* Externs::                   
86 //* Prototypes::                
87 //* Macros::                    
88 //@end menu
89 //*/
90
91 //@node Types, Externs, GUM, GUM
92 //@subsubsection Types
93
94 /* Sparks and spark queues */
95 typedef StgClosure  *rtsSpark;
96 typedef rtsSpark    *rtsSparkQ;
97
98 typedef struct rtsPackBuffer_ {
99   StgInt /* nat */           id; 
100   StgInt /* nat */           size;
101   StgInt /* nat */           unpacked_size;
102   StgTSO       *tso;
103   StgWord     *buffer[0];  
104 } rtsPackBuffer;
105
106 #define PACK_BUFFER_HDR_SIZE 4
107
108 //@node Externs, Prototypes, Types, GUM
109 //@subsubsection Externs
110
111 // extern rtsBool do_sp_profile;
112
113 extern globalAddr theGlobalFromGA, theGlobalToGA;
114 extern StgBlockedFetch *PendingFetches;
115 extern GlobalTaskId    *allPEs;
116
117 extern rtsBool      IAmMainThread, GlobalStopPending;
118 //extern rtsBool      fishing;
119 extern rtsTime      last_fish_arrived_at;
120 extern nat          outstandingFishes;
121 extern GlobalTaskId SysManTask;
122 extern int          seed;     /* pseudo-random-number generator seed: */
123                               /* Initialised in ParInit */
124 extern StgInt       threadId; /* Number of Threads that have existed on a PE */
125 extern GlobalTaskId mytid;
126
127 extern GlobalTaskId *allPEs;
128 extern nat nPEs;
129 extern nat sparksIgnored, sparksCreated, threadsIgnored, threadsCreated;
130 extern nat advisory_thread_count;
131
132 extern rtsBool InGlobalGC;  /* Are we in the midst of performing global GC */
133
134 extern ullong startTime;    /* start of comp; in RtsStartup.c */
135
136 /* the spark pools proper */
137 extern rtsSpark *pending_sparks_hd[];  /* ptr to start of a spark pool */ 
138 extern rtsSpark *pending_sparks_tl[];  /* ptr to end of a spark pool */ 
139 extern rtsSpark *pending_sparks_lim[]; 
140 extern rtsSpark *pending_sparks_base[]; 
141 extern nat spark_limit[];
142
143 extern rtsPackBuffer *PackBuffer;      /* size: can be set via option */
144 extern rtsPackBuffer *buffer;
145 extern rtsPackBuffer *freeBuffer;
146 extern rtsPackBuffer *packBuffer;
147 extern rtsPackBuffer *gumPackBuffer;
148
149 extern nat thisPE;
150
151 /* From Global.c 
152 extern GALA *freeGALAList;
153 extern GALA *freeIndirections;
154 extern GALA *liveIndirections;
155 extern GALA *liveRemoteGAs;
156 */
157
158 //@node Prototypes, Macros, Externs, GUM
159 //@subsubsection Prototypes
160
161 /* From ParInit.c */
162 void          initParallelSystem(void);
163 void          SynchroniseSystem(void);
164 void          par_exit(StgInt n);
165
166 PEs           taskIDtoPE (GlobalTaskId gtid);
167 void          registerTask (GlobalTaskId gtid);
168 globalAddr   *LAGAlookup (StgClosure *addr);
169 StgClosure   *GALAlookup (globalAddr *ga);
170 //static GALA  *allocIndirection (StgPtr addr);
171 globalAddr   *makeGlobal (StgClosure *addr, rtsBool preferred);
172 globalAddr   *setRemoteGA (StgClosure *addr, globalAddr *ga, rtsBool preferred);
173 void          splitWeight (globalAddr *to, globalAddr *from);
174 globalAddr   *addWeight (globalAddr *ga);
175 void          initGAtables (void);
176 void          RebuildLAGAtable (void);
177 StgWord       PackGA (StgWord pe, int slot);
178
179 # if defined(DEBUG)
180 /* from Global.c */
181 /* highest_slot breaks the abstraction of the slot counter for GAs; it is
182    only used for sanity checking and should used nowhere else */
183 StgInt highest_slot (void); 
184 # endif
185
186 //@node Macros,  , Prototypes, GUM
187 //@subsubsection Macros
188
189 /* delay (in us) between dying fish returning and sending out a new fish */
190 #define FISH_DELAY                   1000
191 /* max no. of outstanding spark steals */
192 #define MAX_FISHES                   1  
193
194 // ToDo: check which of these is actually needed!
195
196 #    define PACK_HEAP_REQUIRED  ((RtsFlags.ParFlags.packBufferSize - PACK_HDR_SIZE) / (PACK_GA_SIZE + _HS) * (MIN_UPD_SIZE + 2))
197
198 #  define MAX_GAS       (RtsFlags.ParFlags.packBufferSize / PACK_GA_SIZE)
199
200
201 #  define PACK_GA_SIZE  3       /* Size of a packed GA in words */
202                                 /* Size of a packed fetch-me in words */
203 #  define PACK_FETCHME_SIZE (PACK_GA_SIZE + _HS)
204
205 #  define PACK_HDR_SIZE 1       /* Words of header in a packet */
206
207 #  define PACK_PLC_SIZE 2       /* Size of a packed PLC in words */
208
209 /*
210   Definitions relating to the entire parallel-only fixed-header field.
211
212   On GUM, the global addresses for each local closure are stored in a
213   separate hash table, rather then with the closure in the heap.  We call
214   @getGA@ to look up the global address associated with a local closure (0
215   is returned for local closures that have no global address), and @setGA@
216   to store a new global address for a local closure which did not
217   previously have one.  */
218
219 #  define GA_HDR_SIZE                   0
220   
221 #  define GA(closure)                   getGA(closure)
222   
223 #  define SET_GA(closure, ga)             setGA(closure,ga)
224 #  define SET_STATIC_GA(closure)
225 #  define SET_GRAN_HDR(closure,pe)
226 #  define SET_STATIC_PROCS(closure)
227   
228 #  define MAX_GA_WEIGHT                 0       /* Treat as 2^n */
229   
230 /* At the moment, there is no activity profiling for GUM.  This may change. */
231 #  define SET_TASK_ACTIVITY(act)        /* nothing */
232
233 /* 
234    The following macros are only needed for sanity checking (see Sanity.c).
235 */
236
237 /* NB: this is PVM specific and should be updated for MPI etc
238        in PVM a task id (tid) is split into 2 parts: the id for the 
239        physical processor it is running on and an index of tasks running
240        on a processor; PVM_PE_MASK indicates which part of a tid holds the 
241        id of the physical processor (the other part of the word holds the 
242        index on that processor)
243        MAX_PVM_PES and MAX_PVM_TIDS are maximal values for these 2 components
244        in GUM we have an upper bound for the total number of PVM PEs allowed:
245        it's MAX_PE defined in Parallel.h
246        to check the slot field of a GA we call a fct highest_slot which just
247        returns the internal counter 
248 */
249 #define PVM_PE_MASK    0xfffc0000
250 #define MAX_PVM_PES    MAX_PES
251 #define MAX_PVM_TIDS   MAX_PES
252
253 #if 0
254 #define LOOKS_LIKE_TID(tid)  (((tid & PVM_PE_MASK) != 0) && \
255                               (((tid & PVM_PE_MASK) + (tid & ~PVM_PE_MASK)) < MAX_PVM_TIDS))
256 #define LOOKS_LIKE_SLOT(slot) (slot<=highest_slot())
257
258 #define LOOKS_LIKE_GA(ga)    (LOOKS_LIKE_TID((ga)->payload.gc.gtid) && \
259                              LOOKS_LIKE_SLOT((ga)->payload.gc.slot))
260 #else
261 rtsBool looks_like_tid(StgInt tid);
262 rtsBool looks_like_slot(StgInt slot);
263 rtsBool looks_like_ga(globalAddr *ga);
264 #define LOOKS_LIKE_TID(tid)  looks_like_tid(tid)
265 #define LOOKS_LIKE_GA(ga)    looks_like_ga(ga)
266 #endif /* 0 */
267
268 #endif /* PAR */
269
270 //@node GranSim,  , GUM, Parallel definitions
271 //@subsection GranSim
272
273 #if defined(GRAN)
274 /* ToDo: Check which of the PAR routines are needed in GranSim -- HWL */
275
276 //@menu
277 //* Types::                     
278 //* Prototypes::                
279 //* Macros::                    
280 //@end menu
281 //*/
282
283 //@node Types, Prototypes, GranSim, GranSim
284 //@subsubsection Types
285
286 typedef StgWord *StgBuffer;
287 typedef struct rtsPackBuffer_ {
288   StgInt /* nat */           id;
289   StgInt /* nat */           size;
290   StgInt /* nat */           unpacked_size;
291   StgTSO       *tso;
292   StgWord      *buffer;  
293 } rtsPackBuffer;
294
295 //@node Macros,  , Prototypes, GranSim
296 //@subsubsection Macros
297
298 /* max no. of outstanding spark steals */
299 #define MAX_FISHES                   1  
300
301 /* These are needed in the packing code to get the size of the packet
302    right. The closures itself are never built in GrAnSim. */
303 #  define FETCHME_VHS                           IND_VHS
304 #  define FETCHME_HS                            IND_HS
305   
306 #  define FETCHME_GA_LOCN                       FETCHME_HS
307   
308 #  define FETCHME_CLOSURE_SIZE(closure)         IND_CLOSURE_SIZE(closure)
309 #  define FETCHME_CLOSURE_NoPTRS(closure)               0L
310 #  define FETCHME_CLOSURE_NoNONPTRS(closure)    (IND_CLOSURE_SIZE(closure)-IND_VHS)
311   
312 #  define MAX_GAS       (RtsFlags.GranFlags.packBufferSize / PACK_GA_SIZE)
313 #  define PACK_GA_SIZE  3       /* Size of a packed GA in words */
314                                 /* Size of a packed fetch-me in words */
315 #  define PACK_FETCHME_SIZE (PACK_GA_SIZE + _HS)
316 #  define PACK_HDR_SIZE 4       /* Words of header in a packet */
317
318 #    define PACK_HEAP_REQUIRED  \
319       (RtsFlags.GranFlags.packBufferSize * sizeofW(StgClosure*) + \
320       2 * sizeofW(StgInt) + sizeofW(StgTSO*))
321
322 #    define PACK_FLAG_LOCN           0  
323 #    define PACK_TSO_LOCN            1
324 #    define PACK_UNPACKED_SIZE_LOCN  2
325 #    define PACK_SIZE_LOCN           3
326 #    define MAGIC_PACK_FLAG          0xfabc
327
328 #  define GA_HDR_SIZE                   1
329
330 #  define PROCS_HDR_POSN                PAR_HDR_POSN
331 #  define PROCS_HDR_SIZE                1
332
333 /* Accessing components of the field */
334 #  define PROCS(closure)                ((closure)->header.gran.procs)
335 /* SET_PROCS is now SET_GRAN_HEADER in ClosureMacros.h. */
336
337 #endif   /* GRAN */
338
339 //@node End of File,  , Parallel definitions
340 //@section End of File
341
342 #endif /* defined(PAR) || defined(GRAN)         whole file */
343
344 #endif /* Parallel_H */
345
346