1 #ifndef MULTICORE_RUNTIME
2 #define MULTICORE_RUNTIME
5 #define INLINE inline __attribute__((always_inline))
14 ////////////////////////////////////////////////////////////////
15 // global variables //
16 ///////////////////////////////////////////////////////////////
18 // record the starting time
19 unsigned long long bamboo_start_time;
21 // data structures for msgs
22 #define BAMBOO_OUT_BUF_LENGTH 3000
23 #define BAMBOO_MSG_BUF_LENGTH 3000
24 int msgdata[BAMBOO_MSG_BUF_LENGTH];
25 volatile int msgdataindex;
26 volatile int msgdatalast;
28 volatile bool msgdatafull;
29 int outmsgdata[BAMBOO_OUT_BUF_LENGTH];
33 volatile bool isMsgHanging;
34 //volatile bool isMsgSending;
36 #define MSG_INDEXINC_I() \
37 msgdataindex = (msgdataindex + 1) % (BAMBOO_MSG_BUF_LENGTH)
39 #define MSG_LASTINDEXINC_I() \
40 msgdatalast = (msgdatalast + 1) % (BAMBOO_MSG_BUF_LENGTH)
42 #define MSG_CACHE_I(n) \
43 msgdata[msgdatalast] = (n); \
46 // NOTE: if msgdataindex == msgdatalast, it always means that the buffer if
47 // full. In the case that the buffer is empty, should never call this
49 #define MSG_REMAINSIZE_I(s) \
50 if(msgdataindex < msgdatalast) { \
51 (*(int*)s) = msgdatalast - msgdataindex; \
52 } else if((msgdataindex == msgdatalast) && (!msgdatafull)) { \
55 (*(int*)s) = (BAMBOO_MSG_BUF_LENGTH) - msgdataindex + msgdatalast; \
58 #define OUTMSG_INDEXINC() \
59 outmsgindex = (outmsgindex + 1) % (BAMBOO_OUT_BUF_LENGTH)
61 #define OUTMSG_LASTINDEXINC() \
62 outmsglast = (outmsglast + 1) % (BAMBOO_OUT_BUF_LENGTH); \
63 if(outmsglast == outmsgindex) { \
64 BAMBOO_EXIT(0xdd01); \
67 #define OUTMSG_CACHE(n) \
68 outmsgdata[outmsglast] = (n); \
69 OUTMSG_LASTINDEXINC();
71 #define MAX_PACKET_WORDS 5
75 * type: 1 -- transfer object
76 * 2 -- transfer stall msg
81 * // add for profile info
82 * 7 -- transfer profile output msg
83 * 8 -- transfer profile output finish msg
84 * // add for alias lock strategy
85 * 9 -- redirect lock request
86 * a -- lock grant with redirect info
87 * b -- lock deny with redirect info
88 * c -- lock release with redirect info
89 * d -- status confirm request
90 * e -- status report msg
92 * 10 -- requiring for new memory
93 * 11 -- response for new memory request
94 * 12 -- GC init phase start
96 * 14 -- compact phase start
97 * 15 -- flush phase start
98 * 16 -- init phase finish
99 * 17 -- mark phase finish
100 * 18 -- compact phase finish
101 * 19 -- flush phase finish
103 * 1b -- marked phase finish confirm request
104 * 1c -- marked phase finish confirm response
105 * 1d -- markedObj msg
106 * 1e -- start moving objs msg
107 * 1f -- ask for mapping info of a markedObj
108 * 20 -- mapping info of a markedObj
109 * 21 -- large objs info request
110 * 22 -- large objs info response
111 * 23 -- large objs mapping info
113 * ObjMsg: 1 + size of msg + obj's address + (task index + param index)+
114 * StallMsg: 2 + corenum + sendobjs + receiveobjs
115 * (size is always 4 * sizeof(int))
116 * LockMsg: 3 + lock type + obj pointer + lock + request core
117 * (size is always 5 * sizeof(int))
118 * 4/5/6 + lock type + obj pointer + lock
119 * (size is always 4 * sizeof(int))
120 * 9 + lock type + obj pointer + redirect lock + root request core
122 * (size is always 6 * sizeof(int))
123 * a/b + lock type + obj pointer + redirect lock
124 * (size is always 4 * sizeof(int))
125 * c + lock type + lock + redirect lock
126 * (size is always 4 * sizeof(int))
127 * lock type: 0 -- read; 1 -- write
128 * ProfileMsg: 7 + totalexetime
129 * (size is always 2 * sizeof(int))
131 * (size is always 2 * sizeof(int))
132 * StatusMsg: d (size is always 1 * sizeof(int))
133 * e + status + corenum + sendobjs + receiveobjs
134 * (size is always 5 * sizeof(int))
135 * status: 0 -- stall; 1 -- busy
136 * TerminateMsg: f (size is always 1 * sizeof(int)
137 * MemoryMsg: 10 + size + corenum
138 * (size is always 3 * sizeof(int))
139 * 11 + base_va + size
140 * (size is always 3 * sizeof(int))
141 * GCMsg: 12/13 (size is always 1 * sizeof(int))
142 * 14 + size of msg + (num of objs to move + (start address
143 * + end address + dst core + start dst)+)?
144 * + (num of incoming objs + (start dst + orig core)+)?
145 * + (num of large obj lists + (start address + lenght
147 * 15 (size is always 1 * sizeof(int))
149 * (size is always 2 * sizeof(int))
150 * 17 + corenum + gcsendobjs + gcreceiveobjs
151 * (size if always 4 * sizeof(int))
152 * 18 + corenum + fulfilled blocks num + (finish compact(1) + current
153 * heap top)/(need mem(0) + mem need)
154 * size is always 5 * sizeof(int))
156 * (size is always 2 * sizeof(int))
157 * 1a (size is always 1 * sizeof(int))
158 * 1b (size if always 1 * sizeof(int))
159 * 1c + size of msg + corenum + gcsendobjs + gcreceiveobjs
160 * (size is always 5 * sizeof(int))
161 * 1d + obj's address + request core
162 * (size is always 3 * sizeof(int))
163 * 1e + corenum + start addr + end addr
164 * (size if always 4 * sizeof(int))
165 * 1f + obj's address + corenum
166 * (size is always 3 * sizeof(int))
167 * 20 + obj's address + dst address
168 * (size if always 3 * sizeof(int))
169 * 21 (size is always 1 * sizeof(int))
170 * 22 + size of msg + corenum + current heap size
171 * + (num of large obj lists + (start address + length)+)?
172 * 23 + orig large obj ptr + new large obj ptr
173 * (size is always 3 * sizeof(int))
176 MSGSTART = 0xD0, // 0xD0
183 PROFILEOUTPUT, // 0xD7
184 PROFILEFINISH, // 0xD8
185 REDIRECTLOCK, // 0xD9
186 REDIRECTGROUNT, // 0xDa
187 REDIRECTDENY, // 0xDb
188 REDIRECTRELEASE, // 0xDc
189 STATUSCONFIRM, // 0xDd
190 STATUSREPORT, // 0xDe
198 GCSTARTCOMPACT, // 0xE5
199 GCSTARTMAPINFO, // 0xE6
200 GCSTARTFLUSH, // 0xE7
202 GCFINISHINIT, // 0xE9
203 GCFINISHMARK, // 0xEa
204 GCFINISHCOMPACT, // 0xEb
205 GCFINISHMAPINFO, // 0xEc
206 GCFINISHFLUSH, // 0xEd
208 GCMARKCONFIRM, // 0xEf
209 GCMARKREPORT, // 0xF0
212 GCMAPREQUEST, // 0xF3
215 GCLOBJREQUEST, // 0xF6
217 GCLOBJMAPPING, // 0xF8
221 #ifdef GC_CACHE_ADAPT
222 GCSTARTPOSTINIT, // 0xFa
224 GCFINISHPOSTINIT, // 0xFc
225 GCFINISHPREF, // 0xFd
226 #endif // GC_CACHE_ADAPT
231 /////////////////////////////////////////////////////////////////////////////////
232 // NOTE: BAMBOO_TOTALCORE -- number of the available cores in the processor.
233 // No greater than the number of all the cores in
235 // NUMCORES -- number of cores chosen to deploy the application. It can
236 // be greater than that required to fully parallelize the
237 // application. The same as NUMCORES.
238 // NUMCORESACTIVE -- number of cores that really execute the
239 // application. No greater than NUMCORES
240 // NUMCORES4GC -- number of cores for gc. No greater than NUMCORES.
241 // NOTE: currently only support ontinuous cores as gc
242 // cores, i.e. 0~NUMCORES4GC-1
243 ////////////////////////////////////////////////////////////////////////////////
244 // data structures of status for termination
245 // only check working cores
246 volatile int corestatus[NUMCORESACTIVE]; // records status of each core
249 volatile int numsendobjs[NUMCORESACTIVE]; // records how many objects a core
251 volatile int numreceiveobjs[NUMCORESACTIVE]; // records how many objects a
253 volatile int numconfirm;
254 volatile bool waitconfirm;
256 int self_numsendobjs;
257 int self_numreceiveobjs;
259 // get rid of lock msgs for GC version
261 // data structures for locking
262 struct RuntimeHash locktable;
263 static struct RuntimeHash* locktbl = &locktable;
264 struct RuntimeHash * lockRedirectTbl;
265 struct RuntimeHash * objRedirectLockTbl;
276 // data structures for waiting objs
277 struct Queue objqueue;
278 struct Queue * totransobjqueue; // queue to hold objs to be transferred
279 // should be cleared whenever enter a task
281 // data structures for shared memory allocation
283 #define BAMBOO_BASE_VA 0xd000000
284 #elif defined TILERA_ZLINUX
286 #define BAMBOO_BASE_VA 0xd000000
287 #endif // MULTICORE_GC
290 #ifdef BAMBOO_MEMPROF
291 #define GC_BAMBOO_NUMCORES 56
293 #define GC_BAMBOO_NUMCORES 62
297 #include "structdefs.h"
298 #define BAMBOO_NUM_BLOCKS (NUMCORES4GC*(2+1)+3)
299 #define BAMBOO_PAGE_SIZE (64 * 64)
300 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
301 #define BAMBOO_SHARED_MEM_SIZE ((BAMBOO_SMEM_SIZE) *(BAMBOO_NUM_BLOCKS))
303 #elif defined GC_CACHE_ADAPT
304 #define BAMBOO_NUM_BLOCKS ((GC_BAMBOO_NUMCORES)*(2+14))
305 #define BAMBOO_PAGE_SIZE (64 * 1024) // 64K
306 #ifdef GC_LARGEPAGESIZE
307 #define BAMBOO_SMEM_SIZE (16 * (BAMBOO_PAGE_SIZE))
308 #elif defined GC_SMALLPAGESIZE
309 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
310 #elif defined GC_SMALLPAGESIZE2
311 #define BAMBOO_PAGE_SIZE (16 * 1024) // (4096)
312 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
314 #define BAMBOO_SMEM_SIZE (4 * (BAMBOO_PAGE_SIZE))
315 #endif // GC_LARGEPAGESIZE
316 #define BAMBOO_SHARED_MEM_SIZE ((BAMBOO_SMEM_SIZE) * (BAMBOO_NUM_BLOCKS))
319 #ifdef GC_LARGESHAREDHEAP
320 #define BAMBOO_NUM_BLOCKS ((GC_BAMBOO_NUMCORES)*(2+2))
321 #elif defined GC_LARGESHAREDHEAP2
322 #define BAMBOO_NUM_BLOCKS ((GC_BAMBOO_NUMCORES)*(2+2))
324 #define BAMBOO_NUM_BLOCKS ((GC_BAMBOO_NUMCORES)*(2+3)) //(15 * 1024) //(64 * 4 * 0.75) //(1024 * 1024 * 3.5) 3G
326 #ifdef GC_LARGEPAGESIZE
327 #define BAMBOO_PAGE_SIZE (4 * 1024 * 1024) // (4096)
328 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
329 #elif defined GC_SMALLPAGESIZE
330 #define BAMBOO_PAGE_SIZE (256 * 1024) // (4096)
331 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
332 #elif defined GC_SMALLPAGESIZE2
333 #define BAMBOO_PAGE_SIZE (64 * 1024) // (4096)
334 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
336 #define BAMBOO_PAGE_SIZE (1024 * 1024) // (4096)
337 #define BAMBOO_SMEM_SIZE (BAMBOO_PAGE_SIZE)
338 #endif // GC_LARGEPAGESIZE
339 #define BAMBOO_SHARED_MEM_SIZE ((BAMBOO_SMEM_SIZE) * (BAMBOO_NUM_BLOCKS)) //(1024 * 1024 * 240)
340 //((unsigned long long int)(3.0 * 1024 * 1024 * 1024)) // 3G
344 volatile bool gc_localheap_s;
348 #include "multicoregarbage.h"
351 SMEMLOCAL = 0x0,// 0x0, using local mem only
352 SMEMFIXED, // 0x1, use local mem in lower address space(1 block only)
353 // and global mem in higher address space
354 SMEMMIXED, // 0x2, like FIXED mode but use a threshold to control
355 SMEMGLOBAL, // 0x3, using global mem only
359 SMEMSTRATEGY bamboo_smem_mode; //-DSMEML: LOCAL; -DSMEMF: FIXED;
360 //-DSMEMM: MIXED; -DSMEMG: GLOBAL;
367 struct freeMemItem * next;
371 struct freeMemItem * head;
372 struct freeMemItem * backuplist; // hold removed freeMemItem for reuse;
373 // only maintain 1 freemMemItem
376 // table recording the number of allocated bytes on each block
377 // Note: this table resides on the bottom of the shared heap for all cores
379 volatile int * bamboo_smemtbl;
380 volatile int bamboo_free_block;
381 //bool bamboo_smem_flushed;
382 //struct freeMemList * bamboo_free_mem_list;
383 int bamboo_reserved_smem; // reserved blocks on the top of the shared heap
384 // e.g. 20% of the heap and should not be allocated
385 // otherwise gc is invoked
386 volatile INTPTR bamboo_smem_zero_top;
387 #define BAMBOO_SMEM_ZERO_UNIT_SIZE (4 * 1024) // 4KB
389 #ifdef GC_CACHE_ADAPT
396 unsigned int cache_mode : 2;
398 unsigned int __reserved_0 : 6;
399 // Location Override Target Y
400 unsigned int lotar_y : 4;
402 unsigned int __reserved_1 : 4;
403 // Location Override Target X
404 unsigned int lotar_x : 4;
406 unsigned int __reserved_2 : 12;
408 } bamboo_cache_policy_t;
409 #endif // GC_CACHE_ADAPT
411 //volatile mspace bamboo_free_msp;
412 INTPTR bamboo_free_smemp;
413 int bamboo_free_smem_size;
415 volatile bool smemflag;
416 volatile INTPTR bamboo_cur_msp;
417 volatile int bamboo_smem_size;
422 // data structures for profile mode
425 #define TASKINFOLENGTH 3000 // 0
426 #ifdef PROFILE_INTERRUPT
427 #define INTERRUPTINFOLENGTH 50 //0
428 #endif // PROFILE_INTERRUPT
433 //unsigned long long interrupttime;
435 typedef struct task_info {
437 unsigned long long startTime;
438 unsigned long long endTime;
439 unsigned long long exitIndex;
440 struct Queue * newObjs;
443 TaskInfo * taskInfoArray[TASKINFOLENGTH];
445 bool taskInfoOverflow;
446 #ifdef PROFILE_INTERRUPT
447 typedef struct interrupt_info {
448 unsigned long long startTime;
449 unsigned long long endTime;
452 InterruptInfo * interruptInfoArray[INTERRUPTINFOLENGTH];
453 int interruptInfoIndex;
454 bool interruptInfoOverflow;
455 #endif // PROFILE_INTERUPT
456 volatile int profilestatus[NUMCORESACTIVE]; // records status of each core
459 #endif // #ifdef PROFILE
464 /////////////////////////////////////////////////////////////
466 ////////////////////////////////////////////////////////////
467 // these are functions should be implemented in //
468 // multicore runtime for any multicore processors //
469 ////////////////////////////////////////////////////////////
472 INLINE void initialization(void);
473 INLINE void initCommunication(void);
474 INLINE void fakeExecution(void);
475 INLINE void terminate(void);
476 INLINE void initlock(struct ___Object___ * v);
477 #ifdef BAMBOO_MEMPROF
478 INLINE void terminatememprof(void);
481 // lock related functions
482 bool getreadlock(void* ptr);
483 void releasereadlock(void* ptr);
484 bool getwritelock(void* ptr);
485 void releasewritelock(void* ptr);
486 bool getwritelock_I(void* ptr);
487 void releasewritelock_I(void * ptr);
489 void releasewritelock_r(void * lock, void * redirectlock);
491 /* this function is to process lock requests.
492 * can only be invoked in receiveObject() */
493 // if return -1: the lock request is redirected
494 // 0: the lock request is approved
495 // 1: the lock request is denied
496 INLINE int processlockrequest(int locktype,
502 INLINE void processlockrelease(int locktype,
507 // msg related functions
508 INLINE void send_hanging_msg(bool isInterrupt);
509 INLINE void send_msg_1(int targetcore,
512 INLINE void send_msg_2(int targetcore,
516 INLINE void send_msg_3(int targetcore,
521 INLINE void send_msg_4(int targetcore,
527 INLINE void send_msg_5(int targetcore,
534 INLINE void send_msg_6(int targetcore,
542 INLINE void cache_msg_1(int targetcore,
544 INLINE void cache_msg_2(int targetcore,
547 INLINE void cache_msg_3(int targetcore,
551 INLINE void cache_msg_4(int targetcore,
556 INLINE void cache_msg_5(int targetcore,
562 INLINE void cache_msg_6(int targetcore,
569 INLINE void transferObject(struct transObjInfo * transObj);
570 INLINE int receiveMsg(uint32_t send_port_pending);
573 INLINE void transferMarkResults();
577 INLINE void profileTaskStart(char * taskname);
578 INLINE void profileTaskEnd(void);
579 void outputProfileData();
580 #endif // #ifdef PROFILE
581 ///////////////////////////////////////////////////////////
583 /////////////////////////////////////////////////////////////////////////////
584 // For each version of BAMBOO runtime, there should be a header file named //
585 // runtim_arch.h defining following MARCOS: //
586 // BAMBOO_NUM_OF_CORE: the # of current residing core //
587 // BAMBOO_GET_NUM_OF_CORE(): compute the # of current residing core //
588 // BAMBOO_COORDS(c, x, y): convert the cpu # to coords (*x, *y) //
589 // BAMBOO_DEBUGPRINT(x): print out integer x //
590 // BAMBOO_DEBUGPRINT_REG(x): print out value of variable x //
591 // BAMBOO_EXIT_APP(x): exit the whole application //
592 // BAMBOO_EXIT(x): error exit routine with error # //
593 // BAMBOO_DIE(x): error exit routine with error msg //
594 // BAMBOO_GET_EXE_TIME(): rountine to get current clock cycle number //
595 // BAMBOO_MSG_AVAIL(): checking if there are msgs coming in //
596 // BAMBOO_GCMSG_AVAIL(): checking if there are gcmsgs coming in //
597 // BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT(): change to runtime mode from //
599 // BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME(): change to client mode from //
601 // BAMBOO_ENTER_SEND_MODE_FROM_CLIENT(): change to send mode from //
603 // BAMBOO_ENTER_CLIENT_MODE_FROM_SEND(): change to client mode from //
605 // BAMBOO_ENTER_RUNTIME_MODE_FROM_SEND(): change to runtime mode from //
607 // BAMBOO_ENTER_SEND_MODE_FROM_RUNTIME(): change to send mode from //
609 // BAMBOO_WAITING_FOR_LOCK(): routine executed while waiting for lock //
610 // request response //
611 // BAMBOO_LOCAL_MEM_CALLOC(x, y): allocate an array of x elements each of //
612 // whose size in bytes is y on local memory //
613 // which is given by the hypervisor //
614 // BAMBOO_LOCAL_MEM_FREE(x): free space with ptr x on local memory //
615 // BAMBOO_LOCAL_MEM_CLOSE(): close the local heap //
616 // BAMBOO_LOCAL_MEM_CALLOC_S(x, y): allocate an array of x elements each of//
617 // whose size in bytes is y on local //
618 // memory which is not from the hypervisor//
619 // but is allocated from the free memory //
620 // BAMBOO_LOCAL_MEM_FREE_S(x): free space with ptr x on self-allocated //
622 // BAMBOO_LOCAL_MEM_CLOSE_S(): close the self-allocated local heap //
623 // BAMBOO_SHARE_MEM_CALLOC_I(x, y): allocate an array of x elements each of//
624 // whose size in bytes is y on shared memory//
625 // BAMBOO_SHARE_MEM_CLOSE(): close the shared heap //
626 // BAMBOO_CACHE_LINE_SIZE: the cache line size //
627 // BAMBOO_CACHE_LINE_MASK: mask for a cache line //
628 // BAMBOO_CACHE_FLUSH_RANGE(x, y): flush cache lines started at x with //
630 // BAMBOO_CACHE_FLUSH_ALL(): flush the whole cache of a core if necessary //
631 // BAMBOO_MEMSET_WH(x, y, z): memset the specified region of memory (start //
632 // address x, size z) to value y with write //
633 // hint, the processor will not fetch the //
634 // current content of the memory and directly //
636 // BAMBOO_CLEAN_DTLB(): zero-out all the dtlb entries //
637 // BAMBOO_CACHE_FLUSH_L2(): Flush the contents of this tile's L2 back to //
639 // BAMBOO_CACHE_FLUSH_RANGE_NO_FENCE(x, y): flush a range of mem without //
641 // BAMBOO_CACHE_MEM_FENCE_INCOHERENT(): fence to guarantee visibility of //
642 // stores to incoherent memory //
643 /////////////////////////////////////////////////////////////////////////////
645 #endif // #ifdef MULTICORE
646 #endif // #ifdef TASK
647 #endif // #ifndef MULTICORE_RUNTIME