change
[IRC.git] / Robust / src / Runtime / multicoregarbage.h
1 #ifndef MULTICORE_GARBAGE_H
2 #define MULTICORE_GARBAGE_H
3 #include "multicoregc.h"
4 #include "structdefs.h"
5
6 #ifndef bool
7 #define bool int
8 #endif
9
10 // data structures for GC
11 #ifdef GC_DEBUG
12 #define BAMBOO_SMEM_SIZE_L (BAMBOO_SMEM_SIZE)
13 #else
14 #define BAMBOO_SMEM_SIZE_L (32 * BAMBOO_SMEM_SIZE)
15 #endif
16 #define BAMBOO_LARGE_SMEM_BOUND (BAMBOO_SMEM_SIZE_L*NUMCORES) // NUMCORES=62
17
18 #define NUMPTRS 100
19
20 typedef enum {
21         INITPHASE = 0x0,   // 0x0
22         MARKPHASE,         // 0x1
23         COMPACTPHASE,      // 0x2
24         SUBTLECOMPACTPHASE,// 0x3
25         FLUSHPHASE,        // 0x4
26         FINISHPHASE        // 0x5
27 } GCPHASETYPE;
28
29 volatile bool gcflag;
30 volatile bool gcprocessing;
31 volatile GCPHASETYPE gcphase; // indicating GC phase
32
33 int gccurr_heaptop;
34 // for mark phase termination
35 int gccorestatus[NUMCORES]; // records status of each core
36                             // 1: running gc
37                             // 0: stall
38 int gcnumsendobjs[NUMCORES]; // records how many objects sent out
39 int gcnumreceiveobjs[NUMCORES]; // records how many objects received
40 bool gcbusystatus;
41 int gcself_numsendobjs;
42 int gcself_numreceiveobjs;
43
44 // for load balancing
45 INTPTR gcheaptop;
46 int gcloads[NUMCORES];
47 int gctopcore; // the core host the top of the heap
48 bool gcheapdirection; // 0: decrease; 1: increase
49
50 int gcnumlobjs;
51
52 // compact instruction
53 INTPTR gcmarkedptrbound;
54 int gcblock2fill;
55 int gcstopblock[NUMCORES]; // indicate when to stop compact phase
56 int gcfilledblocks[NUMCORES]; //indicate how many blocks have been fulfilled
57 // move instruction;
58 INTPTR gcmovestartaddr;
59 int gcdstcore;
60 volatile bool gctomove;
61 int gcrequiredmems[NUMCORES]; //record pending mem requests
62 volatile int gcmovepending;
63
64 // mapping of old address to new address
65 struct RuntimeHash * gcpointertbl;
66 int gcobj2map;
67 int gcmappedobj;
68 volatile bool gcismapped;
69
70 // table recording the starting address of each small block
71 // (size is BAMBOO_SMEM_SIZE)
72 // Note: 1. this table always resides on the very bottom of the shared memory
73 //       2. the first two blocks are reserved for this table, would never be
74 //          moved or garbage collected.
75 INTPTR * gcsbstarttbl;
76 int gcreservedsb;  // number of reserved sblock for sbstarttbl
77 int gcbaseva; // base va for shared memory without reserved sblocks
78
79 #define ISSHAREDOBJ(p) \
80         (((p)>gcbaseva)&&((p)<(gcbaseva+(BAMBOO_SHARED_MEM_SIZE))))
81
82 #define ALIGNSIZE(s, as) \
83         (*((int*)as)) = (((s) & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE))
84
85 #define BLOCKINDEX(p, b) \
86   { \
87                 int t = (p) - gcbaseva; \
88                 if(t < (BAMBOO_LARGE_SMEM_BOUND)) { \
89                         (*((int*)b)) = t / (BAMBOO_SMEM_SIZE_L); \
90                 } else { \
91                         (*((int*)b)) = NUMCORES+((t-(BAMBOO_LARGE_SMEM_BOUND))/(BAMBOO_SMEM_SIZE));\
92                 } \
93         }
94
95 #define RESIDECORE(p, x, y) \
96         { \
97                 if(1 == (NUMCORES)) { \
98                         (*((int*)x)) = 0; \
99                         (*((int*)y)) = 0; \
100                 } else { \
101                         int b; \
102                         BLOCKINDEX((p), &b); \
103                         bool reverse = (b / (NUMCORES)) % 2; \
104                         int l = b % (NUMCORES); \
105                         if(reverse) { \
106                                 if(62 == (NUMCORES)) { \
107                                         if(l < 14) { \
108                                                 l += 1; \
109                                         } else { \
110                                                 l += 2; \
111                                         } \
112                                 } \
113                                 (*((int*)y)) = bamboo_height - 1 - (l / bamboo_width); \
114                         } else { \
115                                 if(62 == (NUMCORES)) {\
116                                         if (l > 47) {\
117                                                 l += 1; \
118                                         } \
119                                 } \
120                                 (*((int*)y)) = l / bamboo_width; \
121                         } \
122                         if(((!reverse)&&(*((int*)y))%2) || ((reverse)&&((*((int*)y))%2==0))){ \
123                                 (*((int*)x)) = bamboo_width - 1 - (l % bamboo_width); \
124                         } else { \
125                                 (*((int*)x)) = (l % bamboo_width); \
126                         } \
127                 } \
128         }
129
130 // NOTE: n starts from 0
131 #define NUMBLOCKS(s, n) \
132         if(s < (BAMBOO_SMEM_SIZE_L)) { \
133                 (*((int*)(n))) = 0; \
134         } else { \
135                 (*((int*)(n))) = 1 + ((s) - (BAMBOO_SMEM_SIZE_L)) / (BAMBOO_SMEM_SIZE); \
136         }
137
138 #define OFFSET(s, o) \
139         if(s < BAMBOO_SMEM_SIZE_L) { \
140                 (*((int*)(o))) = (s); \
141         } else { \
142                 (*((int*)(o))) = ((s) - (BAMBOO_SMEM_SIZE_L)) % (BAMBOO_SMEM_SIZE); \
143         }
144
145 #define BLOCKINDEX2(c, n, b) \
146   { \
147                 int x; \
148                 int y; \
149                 int t; \
150                 int cc = c; \
151                 if((62 == (NUMCORES)) && (cc > 5)) cc += 2; \
152                 x = cc / bamboo_height; \
153                 y = cc % bamboo_height; \
154                 if((n) % 2) { \
155                         if(y % 2) { \
156                                 t = x + (bamboo_width - 1 - y) * bamboo_width; \
157                         } else { \
158                                 t = bamboo_width - 1 - x + (bamboo_width - 1 - y) * bamboo_width; \
159                         } \
160                         if(62 == (NUMCORES)) {\
161                                 if(y>5) { \
162                                         t--; \
163                                 } else { \
164                                         t -= 2; \
165                                 } \
166                         } \
167                 } else { \
168                         if(y % 2) { \
169                                 t = bamboo_width - 1 - x + y * bamboo_width; \
170                         } else { \
171                                 t = x + y * bamboo_width; \
172                         } \
173                         if(62 == (NUMCORES)) { \
174                                 if(y > 5) { \
175                                         t--; \
176                                 } \
177                         } \
178                 } \
179                 t += (NUMCORES) * (n); \
180                 (*((int*)b)) = t; \
181         }
182
183
184 #define BASEPTR(c, n, p) \
185   { \
186                 int b; \
187                 BLOCKINDEX2(c, n, &b); \
188                 if(b < (NUMCORES)) { \
189                         (*((int*)p)) = gcbaseva + b * (BAMBOO_SMEM_SIZE_L); \
190                 } else { \
191                         (*((int*)p)) = gcbaseva+(BAMBOO_LARGE_SMEM_BOUND)+(b-(NUMCORES))*(BAMBOO_SMEM_SIZE); \
192                 } \
193         }
194
195 inline void gc(struct garbagelist * stackptr); // core coordinator routine
196 inline void gc_collect(struct garbagelist* stackptr);//core collector routine
197 inline void transferMarkResults_I();
198 inline void gc_enqueue_I(void *ptr);
199 inline void gc_lobjenqueue_I(void *ptr, int length, int host);
200 inline bool gcfindSpareMem_I(int * startaddr, 
201                                        int * tomove,
202                                                                                                  int * dstcore,
203                                                                                                  int requiredmem,
204                                                                                                  int requiredcore);
205
206 #endif
207