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