1 #ifndef MULTICORE_RUNTIME
2 #define MULTICORE_RUNTIME
5 #define INLINE inline __attribute__((always_inline))
8 ////////////////////////////////////////////////////////////////
10 ///////////////////////////////////////////////////////////////
12 // record the starting time
13 unsigned long long bamboo_start_time;
15 // data structures for msgs
16 #define BAMBOO_OUT_BUF_LENGTH 3000
17 #define BAMBOO_MSG_BUF_LENGTH 3000
18 int msgdata[BAMBOO_MSG_BUF_LENGTH];
22 volatile bool msgdatafull;
23 int outmsgdata[BAMBOO_OUT_BUF_LENGTH];
27 volatile bool isMsgHanging;
28 volatile bool isMsgSending;
30 #define MSG_INDEXINC_I() \
31 msgdataindex = (msgdataindex + 1) % (BAMBOO_MSG_BUF_LENGTH)
33 #define MSG_LASTINDEXINC_I() \
34 msgdatalast = (msgdatalast + 1) % (BAMBOO_MSG_BUF_LENGTH)
36 #define MSG_CACHE_I(n) \
37 msgdata[msgdatalast] = (n); \
40 // NOTE: if msgdataindex == msgdatalast, it always means that the buffer if
41 // full. In the case that the buffer is empty, should never call this
43 #define MSG_REMAINSIZE_I(s) \
44 if(msgdataindex < msgdatalast) { \
45 (*(int*)s) = msgdatalast - msgdataindex; \
46 } else if((msgdataindex == msgdatalast) && (!msgdatafull)) {\
49 (*(int*)s) = (BAMBOO_MSG_BUF_LENGTH) - msgdataindex + msgdatalast; \
52 #define OUTMSG_INDEXINC() \
53 outmsgindex = (outmsgindex + 1) % (BAMBOO_OUT_BUF_LENGTH)
55 #define OUTMSG_LASTINDEXINC() \
56 outmsglast = (outmsglast + 1) % (BAMBOO_OUT_BUF_LENGTH); \
57 if(outmsglast == outmsgindex) { \
58 BAMBOO_EXIT(0xdd01); \
61 #define OUTMSG_CACHE(n) \
62 outmsgdata[outmsglast] = (n); \
63 OUTMSG_LASTINDEXINC();
65 #define MAX_PACKET_WORDS 5
69 * type: 1 -- transfer object
70 * 2 -- transfer stall msg
75 * // add for profile info
76 * 7 -- transfer profile output msg
77 * 8 -- transfer profile output finish msg
78 * // add for alias lock strategy
79 * 9 -- redirect lock request
80 * a -- lock grant with redirect info
81 * b -- lock deny with redirect info
82 * c -- lock release with redirect info
83 * d -- status confirm request
84 * e -- status report msg
86 * 10 -- requiring for new memory
87 * 11 -- response for new memory request
88 * 12 -- GC init phase start
90 * 14 -- compact phase start
91 * 15 -- flush phase start
92 * 16 -- init phase finish
93 * 17 -- mark phase finish
94 * 18 -- compact phase finish
95 * 19 -- flush phase finish
97 * 1b -- marked phase finish confirm request
98 * 1c -- marked phase finish confirm response
100 * 1e -- start moving objs msg
101 * 1f -- ask for mapping info of a markedObj
102 * 20 -- mapping info of a markedObj
103 * 21 -- large objs info request
104 * 22 -- large objs info response
105 * 23 -- large objs mapping info
107 * ObjMsg: 1 + size of msg + obj's address + (task index + param index)+
108 * StallMsg: 2 + corenum + sendobjs + receiveobjs
109 * (size is always 4 * sizeof(int))
110 * LockMsg: 3 + lock type + obj pointer + lock + request core
111 * (size is always 5 * sizeof(int))
112 * 4/5/6 + lock type + obj pointer + lock
113 * (size is always 4 * sizeof(int))
114 * 9 + lock type + obj pointer + redirect lock + root request core
116 * (size is always 6 * sizeof(int))
117 * a/b + lock type + obj pointer + redirect lock
118 * (size is always 4 * sizeof(int))
119 * c + lock type + lock + redirect lock
120 * (size is always 4 * sizeof(int))
121 * lock type: 0 -- read; 1 -- write
122 * ProfileMsg: 7 + totalexetime
123 * (size is always 2 * sizeof(int))
125 * (size is always 2 * sizeof(int))
126 * StatusMsg: d (size is always 1 * sizeof(int))
127 * e + status + corenum + sendobjs + receiveobjs
128 * (size is always 5 * sizeof(int))
129 * status: 0 -- stall; 1 -- busy
130 * TerminateMsg: f (size is always 1 * sizeof(int)
131 * MemoryMsg: 10 + size + corenum
132 * (size is always 3 * sizeof(int))
133 * 11 + base_va + size
134 * (size is always 3 * sizeof(int))
135 * GCMsg: 12/13 (size is always 1 * sizeof(int))
136 * 14 + size of msg + (num of objs to move + (start address
137 * + end address + dst core + start dst)+)?
138 * + (num of incoming objs + (start dst + orig core)+)?
139 * + (num of large obj lists + (start address + lenght
141 * 15 (size is always 1 * sizeof(int))
143 * (size is always 2 * sizeof(int))
144 * 17 + corenum + gcsendobjs + gcreceiveobjs
145 * (size if always 4 * sizeof(int))
146 * 18 + corenum + fulfilled blocks num + (finish compact(1) + current
147 * heap top)/(need mem(0) + mem need)
148 * size is always 5 * sizeof(int))
150 * (size is always 2 * sizeof(int))
151 * 1a (size is always 1 * sizeof(int))
152 * 1b (size if always 1 * sizeof(int))
153 * 1c + size of msg + corenum + gcsendobjs + gcreceiveobjs
154 * (size is always 5 * sizeof(int))
156 * (size is always 2 * sizeof(int))
157 * 1e + corenum + start addr + end addr
158 * (size if always 4 * sizeof(int))
159 * 1f + obj's address + corenum
160 * (size is always 3 * sizeof(int))
161 * 20 + obj's address + dst address
162 * (size if always 3 * sizeof(int))
163 * 21 (size is always 1 * sizeof(int))
164 * 22 + size of msg + corenum + current heap size
165 * + (num of large obj lists + (start address + length)+)?
166 * 23 + orig large obj ptr + new large obj ptr
167 * (size is always 3 * sizeof(int))
170 MSGSTART = 0xD0, // 0xD0
177 PROFILEOUTPUT, // 0xD7
178 PROFILEFINISH, // 0xD8
179 REDIRECTLOCK, // 0xD9
180 REDIRECTGROUNT, // 0xDa
181 REDIRECTDENY, // 0xDb
182 REDIRECTRELEASE, // 0xDc
183 STATUSCONFIRM, // 0xDd
184 STATUSREPORT, // 0xDe
191 GCSTARTCOMPACT, // 0xE4
192 GCSTARTFLUSH, // 0xE5
193 GCFINISHINIT, // 0xE6
194 GCFINISHMARK, // 0xE7
195 GCFINISHCOMPACT, // 0xE8
196 GCFINISHFLUSH, // 0xE9
198 GCMARKCONFIRM, // 0xEb
199 GCMARKREPORT, // 0xEc
202 GCMAPREQUEST, // 0xEf
204 GCLOBJREQUEST, // 0xF1
206 GCLOBJMAPPING, // 0xF3
211 /////////////////////////////////////////////////////////////////////////////////
212 // NOTE: BAMBOO_TOTALCORE -- number of the available cores in the processor.
213 // No greater than the number of all the cores in
215 // NUMCORES -- number of cores chosen to deploy the application. It can
216 // be greater than that required to fully parallelize the
217 // application. The same as NUMCORES.
218 // NUMCORESACTIVE -- number of cores that really execute the
219 // application. No greater than NUMCORES
220 // NUMCORES4GC -- number of cores for gc. No greater than NUMCORES.
221 // NOTE: currently only support ontinuous cores as gc
222 // cores, i.e. 0~NUMCORES4GC-1
223 ////////////////////////////////////////////////////////////////////////////////
224 // data structures of status for termination
225 // only check working cores
226 volatile int corestatus[NUMCORESACTIVE]; // records status of each core
229 volatile int numsendobjs[NUMCORESACTIVE]; // records how many objects a core
231 volatile int numreceiveobjs[NUMCORESACTIVE]; // records how many objects a
233 volatile int numconfirm;
234 volatile bool waitconfirm;
236 int self_numsendobjs;
237 int self_numreceiveobjs;
239 // get rid of lock msgs for GC version
241 // data structures for locking
242 struct RuntimeHash locktable;
243 static struct RuntimeHash* locktbl = &locktable;
244 struct RuntimeHash * lockRedirectTbl;
245 struct RuntimeHash * objRedirectLockTbl;
256 // data structures for waiting objs
257 struct Queue objqueue;
258 struct Queue * totransobjqueue; // queue to hold objs to be transferred
259 // should be cleared whenever enter a task
261 // data structures for shared memory allocation
262 #define BAMBOO_BASE_VA 0xd000000
264 #include "structdefs.h"
265 #define BAMBOO_NUM_PAGES (NUMCORES4GC*(2+1)+3)
266 #define BAMBOO_PAGE_SIZE (64 * 64)
267 #define BAMBOO_SMEM_SIZE (64 * 64) // (BAMBOO_PAGE_SIZE)
268 #define BAMBOO_SHARED_MEM_SIZE ((BAMBOO_PAGE_SIZE) * (BAMBOO_NUM_PAGES))
270 #define BAMBOO_NUM_PAGES (15 * 1024) //(64 * 4 * 0.75) //(1024 * 1024 * 3.5) 3G
271 #define BAMBOO_PAGE_SIZE (16 * 1024)// * 1024) // (4096)
272 #define BAMBOO_SMEM_SIZE (16 * 1024)
273 #define BAMBOO_SHARED_MEM_SIZE (1024 * 1024 * 240) //(1024 * 1024 * 1024)
274 //(3.0 * 1024 * 1024 * 1024) // 3G// ((BAMBOO_PAGE_SIZE) * (BAMBOO_NUM_PAGES))
278 #include "multicoregarbage.h"
281 SMEMLOCAL = 0x0, // 0x0, using local mem only
282 SMEMFIXED, // 0x1, use local mem in lower address space(1 block only)
283 // and global mem in higher address space
284 SMEMMIXED, // 0x2, like FIXED mode but use a threshold to control
285 SMEMGLOBAL, // 0x3, using global mem only
289 SMEMSTRATEGY bamboo_smem_mode; //-DSMEML: LOCAL; -DSMEMF: FIXED;
290 //-DSMEMM: MIXED; -DSMEMG: GLOBAL;
297 struct freeMemItem * next;
301 struct freeMemItem * head;
302 struct freeMemItem * backuplist; // hold removed freeMemItem for reuse;
303 // only maintain 1 fremmMemItem
306 // table recording the number of allocated bytes on each block
307 // Note: this table resides on the bottom of the shared heap for all cores
309 volatile int * bamboo_smemtbl;
310 volatile int bamboo_free_block;
311 //bool bamboo_smem_flushed;
312 //struct freeMemList * bamboo_free_mem_list;
313 int bamboo_reserved_smem; // reserved blocks on the top of the shared heap
314 // e.g. 20% of the heap and should not be allocated
315 // otherwise gc is invoked
317 volatile mspace bamboo_free_msp;
319 volatile bool smemflag;
320 volatile INTPTR bamboo_cur_msp;
321 volatile int bamboo_smem_size;
326 // data structures for profile mode
329 #define TASKINFOLENGTH 30000
330 //#define INTERRUPTINFOLENGTH 500
336 typedef struct task_info {
338 unsigned long long startTime;
339 unsigned long long endTime;
340 unsigned long long exitIndex;
341 struct Queue * newObjs;
344 /*typedef struct interrupt_info {
349 TaskInfo * taskInfoArray[TASKINFOLENGTH];
351 bool taskInfoOverflow;
352 /*InterruptInfo * interruptInfoArray[INTERRUPTINFOLENGTH];
353 int interruptInfoIndex;
354 bool interruptInfoOverflow;*/
355 volatile int profilestatus[NUMCORESACTIVE]; // records status of each core
358 #endif // #ifdef PROFILE
363 /////////////////////////////////////////////////////////////
365 ////////////////////////////////////////////////////////////
366 // these are functions should be implemented in //
367 // multicore runtime for any multicore processors //
368 ////////////////////////////////////////////////////////////
371 INLINE void initialization(void);
372 INLINE void initCommunication(void);
373 INLINE void fakeExecution(void);
374 INLINE void terminate(void);
375 INLINE void initlock(struct ___Object___ * v);
377 // lock related functions
378 bool getreadlock(void* ptr);
379 void releasereadlock(void* ptr);
380 bool getwritelock(void* ptr);
381 void releasewritelock(void* ptr);
382 bool getwritelock_I(void* ptr);
383 void releasewritelock_I(void * ptr);
385 void releasewritelock_r(void * lock, void * redirectlock);
387 /* this function is to process lock requests.
388 * can only be invoked in receiveObject() */
389 // if return -1: the lock request is redirected
390 // 0: the lock request is approved
391 // 1: the lock request is denied
392 INLINE int processlockrequest(int locktype,
398 INLINE void processlockrelease(int locktype,
403 // msg related functions
404 INLINE void send_hanging_msg();
405 INLINE void send_msg_1(int targetcore,
407 INLINE void send_msg_2(int targetcore,
410 INLINE void send_msg_3(int targetcore,
414 INLINE void send_msg_4(int targetcore,
419 INLINE void send_msg_5(int targetcore,
425 INLINE void send_msg_6(int targetcore,
432 INLINE void send_msg_3_I(int targetcore,
436 INLINE void cache_msg_1(int targetcore,
438 INLINE void cache_msg_2(int targetcore,
441 INLINE void cache_msg_3(int targetcore,
445 INLINE void cache_msg_4(int targetcore,
450 INLINE void cache_msg_5(int targetcore,
456 INLINE void cache_msg_6(int targetcore,
463 INLINE void transferObject(struct transObjInfo * transObj);
464 INLINE int receiveMsg(uint32_t send_port_pending);
467 INLINE void transferMarkResults();
471 INLINE void profileTaskStart(char * taskname);
472 INLINE void profileTaskEnd(void);
473 void outputProfileData();
474 #endif // #ifdef PROFILE
475 ///////////////////////////////////////////////////////////
477 /////////////////////////////////////////////////////////////////////////////
478 // For each version of BAMBOO runtime, there should be a header file named //
479 // runtim_arch.h defining following MARCOS: //
480 // BAMBOO_NUM_OF_CORE: the # of current residing core //
481 // BAMBOO_GET_NUM_OF_CORE(): compute the # of current residing core //
482 // BAMBOO_COORDS(c, x, y): convert the cpu # to coords (*x, *y) //
483 // BAMBOO_DEBUGPRINT(x): print out integer x //
484 // BAMBOO_DEBUGPRINT_REG(x): print out value of variable x //
485 // BAMBOO_EXIT(x): exit routine //
486 // BAMBOO_DIE(x): error exit routine //
487 // BAMBOO_GET_EXE_TIME(): rountine to get current clock cycle number //
488 // BAMBOO_MSG_AVAIL(): checking if there are msgs coming in //
489 // BAMBOO_GCMSG_AVAIL(): checking if there are gcmsgs coming in //
490 // BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT(): change to runtime mode from //
492 // BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME(): change to client mode from //
494 // BAMBOO_ENTER_SEND_MODE_FROM_CLIENT(): change to send mode from //
496 // BAMBOO_ENTER_CLIENT_MODE_FROM_SEND(): change to client mode from //
498 // BAMBOO_ENTER_RUNTIME_MODE_FROM_SEND(): change to runtime mode from //
500 // BAMBOO_ENTER_SEND_MODE_FROM_RUNTIME(): change to send mode from //
502 // BAMBOO_WAITING_FOR_LOCK(): routine executed while waiting for lock //
503 // request response //
504 // BAMBOO_LOCAL_MEM_CALLOC(x, y): allocate an array of x elements each of //
505 // whose size in bytes is y on local memory //
506 // BAMBOO_LOCAL_MEM_FREE(x): free space with ptr x on local memory //
507 // BAMBOO_LOCAL_MEM_CLOSE(): close the local heap //
508 // BAMBOO_SHARE_MEM_CALLOC_I(x, y): allocate an array of x elements each of//
509 // whose size in bytes is y on shared memory//
510 // BAMBOO_SHARE_MEM_CLOSE(): close the shared heap //
511 // BAMBOO_CACHE_LINE_SIZE: the cache line size //
512 // BAMBOO_CACHE_LINE_MASK: mask for a cache line //
513 // BAMBOO_CACHE_FLUSH_RANGE(x, y): flush cache lines started at x with //
515 // BAMBOO_CACHE_FLUSH_ALL(): flush the whole cache of a core if necessary //
516 // BAMBOO_MEMSET_WH(x, y, z): memset the specified region of memory (start //
517 // address x, size z) to value y with write //
518 // hint, the processor will not fetch the //
519 // current content of the memory and directly //
521 /////////////////////////////////////////////////////////////////////////////
523 #endif // #ifdef MULTICORE
524 #endif // #ifdef TASK
525 #endif // #ifndef MULTICORE_RUNTIME