Cope with libraries in libraries/foo/bar rather than just libraries/foo
[ghc-hetmet.git] / includes / GranSim.h
1 /*
2   Headers for GranSim specific objects.
3   
4   Note that in GranSim we have one run-queue and blocking-queue for each
5   processor. Therefore, this header file redefines variables like
6   run_queue_hd to be relative to CurrentProc. The main arrays of runnable
7   and blocking queues are defined in Schedule.c.  The important STG-called
8   GranSim macros (e.g. for fetching nodes) are at the end of this
9   file. Usually they are just wrappers to proper C functions in GranSim.c.  
10 */
11
12 #ifndef GRANSIM_H
13 #define GRANSIM_H
14
15 #if !defined(GRAN)
16
17 /* Dummy definitions for basic GranSim macros called from STG land */
18 #define DO_GRAN_ALLOCATE(n)                               /* nothing */
19 #define DO_GRAN_UNALLOCATE(n)                             /* nothing */
20 #define DO_GRAN_FETCH(node)                               /* nothing */
21 #define DO_GRAN_EXEC(arith,branch,load,store,floats)      /* nothing */
22 #define GRAN_FETCH_AND_RESCHEDULE(liveness_mask,reenter)  /* nothing */
23 #define GRAN_RESCHEDULE(liveness_mask,reenter)            /* nothing */
24
25 #endif
26
27 #if defined(GRAN)  /* whole file */
28
29 extern StgTSO *CurrentTSO;
30
31 /*
32  * @node Headers for GranSim specific objects, , ,
33  * @section Headers for GranSim specific objects
34  *
35  * @menu
36  * * Externs and prototypes::   
37  * * Run and blocking queues::  
38  * * Spark queues::             
39  * * Processor related stuff::  
40  * * GranSim costs::            
41  * * STG called GranSim functions::  
42  * * STG-called routines::      
43  * @end menu
44  *
45  * @node Externs and prototypes, Run and blocking queues, Includes, Headers for GranSim specific objects
46  * @subsection Externs and prototypes
47  */
48
49 /* Global constants */
50 extern char *gran_event_names[];
51 extern char *proc_status_names[];
52 extern char *event_names[];
53
54 /* Vars checked from within STG land */
55 extern rtsBool  NeedToReSchedule, IgnoreEvents, IgnoreYields; 
56
57 extern rtsTime  TimeOfNextEvent, TimeOfLastEvent, EndOfTimeSlice;
58
59 /* costs for basic operations (copied from RTS flags) */
60 extern nat gran_arith_cost, gran_branch_cost, gran_load_cost, gran_store_cost, gran_float_cost;
61
62 extern nat SparksAvail;     /* How many sparks are available */
63 extern nat SurplusThreads;  /* How many excess threads are there */
64 extern nat sparksIgnored, sparksCreated;
65
66 /*
67  * @node Run and blocking queues, Spark queues, Externs and prototypes, Headers for GranSim specific objects
68  * @subsection Run and blocking queues
69  */
70
71 /* declared in Schedule.c */
72 extern StgTSO *run_queue_hds[], *run_queue_tls[];
73 extern StgTSO *blocked_queue_hds[], *blocked_queue_tls[];
74 extern StgTSO *ccalling_threadss[];
75
76 #define run_queue_hd         run_queue_hds[CurrentProc]
77 #define run_queue_tl         run_queue_tls[CurrentProc]
78 #define blocked_queue_hd     blocked_queue_hds[CurrentProc]
79 #define blocked_queue_tl     blocked_queue_tls[CurrentProc]
80 #define pending_sparks_hd    pending_sparks_hds[CurrentProc]
81 #define pending_sparks_tl    pending_sparks_tls[CurrentProc]
82 #define ccalling_threads     ccalling_threadss[CurrentProc]
83
84 /*
85  * @node Spark queues, Processor related stuff, Run and blocking queues, Headers for GranSim specific objects
86  * @subsection Spark queues
87  */
88
89 /*
90   In GranSim we use a double linked list to represent spark queues.
91   
92   This is more flexible, but slower, than the array of pointers
93   representation used in GUM. We use the flexibility to define new fields in
94   the rtsSpark structure, representing e.g. granularity info (see HWL's PhD
95   thesis), or info about the parent of a spark.
96 */
97
98 /* Sparks and spark queues */
99 typedef struct rtsSpark_
100 {
101   StgClosure    *node;
102   nat            name, global;
103   nat            gran_info;      /* for granularity improvement mechanisms */
104   PEs            creator;        /* PE that created this spark (unused) */
105   struct rtsSpark_  *prev, *next;
106 } rtsSpark;
107 typedef rtsSpark *rtsSparkQ;
108
109 /* The spark queues, proper */
110 /* In GranSim this is a globally visible array of spark queues */
111 extern rtsSparkQ pending_sparks_hds[];
112 extern rtsSparkQ pending_sparks_tls[];
113
114 /* Prototypes of those spark routines visible to compiler generated .hc */
115 /* Routines only used inside the RTS are defined in rts/parallel GranSimRts.h */
116 rtsSpark    *newSpark(StgClosure *node, 
117                       nat name, nat gran_info, nat size_info, 
118                       nat par_info, nat local);
119 /* void         add_to_spark_queue(rtsSpark *spark); */
120
121 /*
122  * @node Processor related stuff, GranSim costs, Spark queues, Headers for GranSim specific objects
123  * @subsection Processor related stuff
124  */
125
126 extern PEs CurrentProc;
127 extern rtsTime CurrentTime[];  
128
129 /* Maximum number of PEs that can be simulated */
130 #define MAX_PROC             32 /* (BITS_IN(StgWord))  */ /* ToDo: fix this!! */
131 /*
132 #if MAX_PROC==16 
133 #else 
134 #error MAX_PROC should be 32 on this architecture 
135 #endif
136 */
137
138 /* #define CurrentTSO           CurrentTSOs[CurrentProc] */
139
140 /* Processor numbers to bitmasks and vice-versa */
141 #define MainProc             0           /* Id of main processor */
142 #define NO_PRI               0           /* dummy priority */
143 #define MAX_PRI              10000       /* max possible priority */
144 #define MAIN_PRI             MAX_PRI     /* priority of main thread */ 
145
146 /* GrAnSim uses IdleProcs as bitmask to indicate which procs are idle */
147 #define PE_NUMBER(n)          (1l << (long)n)
148 #define ThisPE                PE_NUMBER(CurrentProc)
149 #define MainPE                PE_NUMBER(MainProc)
150 #define Everywhere            (~0l)
151 #define Nowhere               (0l)
152 #define Now                   CurrentTime[CurrentProc]
153
154 #define IS_LOCAL_TO(ga,proc)  ((1l << (PEs) proc) & ga)
155
156 #define GRAN_TIME_SLICE       1000        /* max time between 2 ReSchedules */
157
158 /*
159  * @node GranSim costs, STG called GranSim functions, Processor related stuff, Headers for GranSim specific objects
160  * @subsection GranSim costs
161  */
162
163 /* Default constants for communication (see RtsFlags on how to change them) */
164
165 #define LATENCY                    1000 /* Latency for single packet */
166 #define ADDITIONAL_LATENCY          100 /* Latency for additional packets */
167 #define BASICBLOCKTIME               10
168 #define FETCHTIME               (LATENCY*2+MSGUNPACKTIME)
169 #define LOCALUNBLOCKTIME             10
170 #define GLOBALUNBLOCKTIME       (LATENCY+MSGUNPACKTIME)
171
172 #define MSGPACKTIME                  0  /* Cost of creating a packet */
173 #define MSGUNPACKTIME                0  /* Cost of receiving a packet */
174 #define MSGTIDYTIME                  0  /* Cost of cleaning up after send */
175
176 /* How much to increase GrAnSims internal packet size if an overflow 
177    occurs.
178    NB: This is a GrAnSim internal variable and is independent of the
179    simulated packet buffer size.
180 */
181
182 #define GRANSIM_DEFAULT_PACK_BUFFER_SIZE     400
183 #define REALLOC_SZ                           200
184
185 /* extern W_ gran_mpacktime, gran_mtidytime, gran_munpacktime; */
186
187 /* Thread cost model */
188 #define THREADCREATETIME           (25+THREADSCHEDULETIME)
189 #define THREADQUEUETIME             12  /* Cost of adding a thread to the running/runnable queue */
190 #define THREADDESCHEDULETIME        75  /* Cost of descheduling a thread */
191 #define THREADSCHEDULETIME          75  /* Cost of scheduling a thread */
192 #define THREADCONTEXTSWITCHTIME     (THREADDESCHEDULETIME+THREADSCHEDULETIME)
193
194 /* Instruction Cost model (SPARC, including cache misses) */
195 #define ARITH_COST                 1
196 #define BRANCH_COST                2
197 #define LOAD_COST                  4
198 #define STORE_COST                 4
199 #define FLOAT_COST                 1 /* ? */
200
201 #define HEAPALLOC_COST             11
202
203 #define PRI_SPARK_OVERHEAD    5
204 #define PRI_SCHED_OVERHEAD    5
205
206 /*
207  * @node STG called GranSim functions, STG-called routines, GranSim costs, Headers for GranSim specific objects
208  * @subsection STG called GranSim functions
209  */
210
211 /* STG called GranSim functions */
212 void GranSimAllocate(StgInt n);
213 void GranSimUnallocate(StgInt n);
214 void GranSimExec(StgWord ariths, StgWord branches, StgWord loads, StgWord stores, StgWord floats);
215 StgInt GranSimFetch(StgClosure *node);
216 void GranSimSpark(StgInt local, StgClosure *node);
217 void GranSimSparkAt(rtsSpark *spark, StgClosure *where,StgInt identifier);
218 void GranSimSparkAtAbs(rtsSpark *spark, PEs proc, StgInt identifier);
219 void GranSimBlock(StgTSO *tso, PEs proc, StgClosure *node);
220
221
222 /*
223  * @node STG-called routines,  , STG called GranSim functions, Headers for GranSim specific objects
224  * @subsection STG-called routines
225  */
226
227 /* Wrapped version of calls to GranSim-specific STG routines */
228
229 /*
230 #define DO_PERFORM_RESCHEDULE(liveness, always_reenter_node) PerformReschedule_wrapper(liveness, always_reenter_node)
231 */
232 #define DO_GRAN_ALLOCATE(n)     STGCALL1(GranSimAllocate, n)
233 #define DO_GRAN_UNALLOCATE(n)   STGCALL1(GranSimUnallocate, n)
234 #define DO_GRAN_FETCH(node)     STGCALL1(GranSimFetch, node)
235 #define DO_GRAN_EXEC(arith,branch,load,store,floats) GranSimExec(arith,branch,load,store,floats)
236
237 /* 
238    ToDo: Clean up this mess of GRAN macros!!! -- HWL
239 */
240 /* DO_GRAN_FETCH((StgClosure*)R1.p); */
241 #define GRAN_FETCH()            /* nothing */
242
243 #define GRAN_FETCH_AND_RESCHEDULE(liveness,reenter)     \
244           DO_GRAN_FETCH((StgClosure*)R1.p);                             \
245           DO_GRAN_YIELD(liveness,ENTRY_CODE((D_)(*R1.p))); 
246 /* RESTORE_EVERYTHING is done implicitly before entering threaded world again */
247
248 /*
249   This is the only macro currently enabled;
250   It should check whether it is time for the current thread to yield
251   (e.g. if there is a more recent event in the queue) and it should check
252   whether node is local, via a call to GranSimFetch.
253   ToDo: split this in 2 routines:
254          - GRAN_YIELD (as it is below)
255          - GRAN_FETCH (the rest of this macro)
256         emit only these 2 macros based on node's liveness
257           node alive: emit both macros
258           node not alive: do only a GRAN_YIELD
259           
260         replace gran_yield_? with gran_block_? (they really block the current
261         thread)
262 */
263 #define GRAN_RESCHEDULE(liveness,ptrs)  \
264           if (RET_STGCALL1(StgInt, GranSimFetch, (StgClosure*)R1.p)) {\
265             EXTFUN_RTS(gran_block_##ptrs); \
266             JMP_(gran_block_##ptrs);       \
267           } else {                         \
268             if (TimeOfLastEvent < CurrentTime[CurrentProc] && \
269                 HEAP_ALLOCED((StgClosure *)R1.p) && \
270                 LOOKS_LIKE_GHC_INFO(get_itbl((StgClosure *)R1.p))) { \
271                                   EXTFUN_RTS(gran_yield_##ptrs); \
272                                   JMP_(gran_yield_##ptrs); \
273                 } \
274             /* GRAN_YIELD(ptrs)  */             \
275           }
276
277
278 /*                                                   YIELD(liveness,reenter) */
279
280 /* GRAN_YIELD(liveness_mask); */
281
282 /* GRAN_FETCH_AND_RESCHEDULE(liveness_mask,reenter) */
283
284 #define THREAD_CONTEXT_SWITCH(liveness_mask,reenter)    \
285         do { \
286         if (context_switch /* OR_INTERVAL_EXPIRED */) { \
287           GRAN_RESCHEDULE(liveness_mask,reenter); \
288         } }while(0)
289
290 #define GRAN_EXEC(arith,branch,load,store,floats)       \
291         { \
292           W_ cost = gran_arith_cost*arith +   \
293                     gran_branch_cost*branch + \
294                     gran_load_cost*load +   \
295                     gran_store_cost*store +   \
296                     gran_float_cost*floats;   \
297           CurrentTSO->gran.exectime += cost;                      \
298           CurrentTime[CurrentProc] += cost;                      \
299         }
300
301 /* In GranSim we first check whether there is an event to handle; only if
302    this is the case (or the time slice is over in case of fair scheduling)
303    we do a yield, which is very similar to that in the concurrent world 
304    ToDo: check whether gran_yield_? can be merged with other yielding codes
305 */
306
307 #define DO_GRAN_YIELD(ptrs)     if (!IgnoreYields && \
308                                     TimeOfLastEvent < CurrentTime[CurrentProc] && \
309                                     HEAP_ALLOCED((StgClosure *)R1.p) && \
310                                     LOOKS_LIKE_GHC_INFO(get_itbl((StgClosure *)R1.p))) { \
311                                   EXTFUN_RTS(gran_yield_##ptrs); \
312                                   JMP_(gran_yield_##ptrs); \
313                                 }
314
315 #define GRAN_YIELD(ptrs)                                   \
316         {                                                   \
317           extern int context_switch;                          \
318           if ( (CurrentTime[CurrentProc]>=EndOfTimeSlice) ||   \
319                ((CurrentTime[CurrentProc]>=TimeOfNextEvent) && \
320                 (TimeOfNextEvent!=0) && !IgnoreEvents )) {     \
321             /* context_switch = 1; */                          \
322             DO_GRAN_YIELD(ptrs);   \
323           }                                                    \
324         }
325
326 #define ADD_TO_SPARK_QUEUE(spark)             \
327    STGCALL1(add_to_spark_queue,spark) \
328
329 #endif /* GRAN */
330
331 #endif /* GRANSIM_H */