3 #include "multicoreruntime.h"
4 #include "runtime_arch.h"
5 #include "GenericHashtable.h"
8 extern int injectfailures;
9 extern float failurechance;
14 void * curr_heapbase=0;
15 void * curr_heaptop=0;
18 #include "instrument.h"
20 #endif // if 0: for recovery
22 // data structures for task invocation
23 struct genhashtable * activetasks;
24 //struct genhashtable * failedtasks; // for recovery
25 struct taskparamdescriptor * currtpd;
27 struct RuntimeHash * forward;
28 struct RuntimeHash * reverse;
29 #endif // if 0: for recovery
31 // specific functions used inside critical sections
32 void enqueueObject_I(void * ptr, struct parameterwrapper ** queues, int length);
33 int enqueuetasks_I(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
35 // main function for each core
36 inline void run(void * arg) {
40 bool sendStall = false;
42 bool tocontinue = false;
43 struct transObjInfo * objInfo = NULL;
48 corenum = BAMBOO_GET_NUM_OF_CORE();
50 BAMBOO_DEBUGPRINT(0xeeee);
51 BAMBOO_DEBUGPRINT_REG(corenum);
52 BAMBOO_DEBUGPRINT(STARTUPCORE);
55 // initialize the arrays
56 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
57 // startup core to initialize corestatus[]
58 for(i = 0; i < NUMCORES; ++i) {
60 numsendobjs[i] = 0; // assume all variables are local variables! MAY BE WRONG!!!
61 numreceiveobjs[i] = 0;
66 // initialize the profile data arrays
67 for(i = 0; i < NUMCORES; ++i) {
76 self_numreceiveobjs = 0;
78 for(i = 0; i < 30; ++i) {
84 for(i = 0; i < 30; ++i) {
94 bamboo_cur_msp = NULL;
97 // create the lock table, lockresult table and obj queue
99 locktable.bucket = (struct RuntimeNode **) RUNMALLOC_I(sizeof(struct RuntimeNode *)*20);
100 /* Set allocation blocks*/
101 locktable.listhead=NULL;
102 locktable.listtail=NULL;
104 locktable.numelements = 0;
112 objqueue.head = NULL;
113 objqueue.tail = NULL;
114 lockRedirectTbl = allocateRuntimeHash(20);
115 objRedirectLockTbl = allocateRuntimeHash(20);
119 //isInterrupt = true;
122 taskInfoOverflow = false;
123 /*interruptInfoIndex = 0;
124 interruptInfoOverflow = false;*/
127 // other architecture related initialization
134 GC_init(); // Initialize the garbage collector
140 #endif // #if 0: for recovery and garbage collection
141 initializeexithandler();
143 // main process of the execution module
144 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
145 // non-executing cores, only processing communications
146 //failedtasks = NULL;
149 BAMBOO_DEBUGPRINT(0xee01);
150 BAMBOO_DEBUGPRINT_REG(taskInfoIndex);
151 BAMBOO_DEBUGPRINT_REG(taskInfoOverflow);
152 profileTaskStart("msg handling");
156 //isInterrupt = false;
160 /* Create table for failed tasks */
162 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
163 (int (*)(void *,void *)) &comparetpd);
164 #endif // #if 0: for recovery
165 /* Create queue of active tasks */
166 activetasks=genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
167 (int(*) (void *,void *)) &comparetpd);
169 /* Process task information */
172 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
173 /* Create startup object */
174 createstartupobject(argc, argv);
178 BAMBOO_DEBUGPRINT(0xee00);
182 // check if there are new active tasks can be executed
186 while(receiveObject() != -1) {
191 BAMBOO_DEBUGPRINT(0xee01);
194 // check if there are some pending objects, if yes, enqueue them and executetasks again
197 #ifdef ACCURATEPROFILE
199 bool isChecking = false;
200 if(!isEmpty(&objqueue)) {
201 profileTaskStart("objqueue checking");
206 while(!isEmpty(&objqueue)) {
208 BAMBOO_START_CRITICAL_SECTION_OBJ_QUEUE();
210 BAMBOO_DEBUGPRINT(0xf001);
213 //isInterrupt = false;
216 BAMBOO_DEBUGPRINT(0xeee1);
220 objInfo = (struct transObjInfo *)getItem(&objqueue);
221 obj = objInfo->objptr;
223 BAMBOO_DEBUGPRINT_REG((int)obj);
225 // grab lock and flush the obj
229 BAMBOO_WAITING_FOR_LOCK();
233 BAMBOO_DEBUGPRINT_REG(grount);
248 BAMBOO_CACHE_FLUSH_RANGE((int)obj,sizeof(int));
249 BAMBOO_CACHE_FLUSH_RANGE((int)obj, classsize[((struct ___Object___ *)obj)->type]);
251 // enqueue the object
252 for(k = 0; k < objInfo->length; ++k) {
253 int taskindex = objInfo->queues[2 * k];
254 int paramindex = objInfo->queues[2 * k + 1];
255 struct parameterwrapper ** queues = &(paramqueues[BAMBOO_NUM_OF_CORE][taskindex][paramindex]);
257 BAMBOO_DEBUGPRINT_REG(taskindex);
258 BAMBOO_DEBUGPRINT_REG(paramindex);
259 struct ___Object___ * tmpptr = (struct ___Object___ *)obj;
260 tprintf("Process %x(%d): receive obj %x(%lld), ptrflag %x\n", BAMBOO_NUM_OF_CORE, BAMBOO_NUM_OF_CORE, (int)obj, (long)obj, tmpptr->flag);
262 enqueueObject_I(obj, queues, 1);
264 BAMBOO_DEBUGPRINT_REG(hashsize(activetasks));
267 releasewritelock_I(obj);
268 RUNFREE(objInfo->queues);
272 // put it at the end of the queue if no update version in the queue
273 struct QueueItem * qitem = getHead(&objqueue);
274 struct QueueItem * prev = NULL;
275 while(qitem != NULL) {
276 struct transObjInfo * tmpinfo = (struct transObjInfo *)(qitem->objectptr);
277 if(tmpinfo->objptr == obj) {
278 // the same object in the queue, which should be enqueued
279 // recently. Current one is outdate, do not re-enqueue it
280 RUNFREE(objInfo->queues);
286 qitem = getNextQueueItem(prev);
288 // try to execute active tasks already enqueued first
289 addNewItem_I(&objqueue, objInfo);
291 //isInterrupt = true;
294 BAMBOO_CLOSE_CRITICAL_SECTION_OBJ_QUEUE();
296 BAMBOO_DEBUGPRINT(0xf000);
300 BAMBOO_CLOSE_CRITICAL_SECTION_OBJ_QUEUE();
302 BAMBOO_DEBUGPRINT(0xf000);
306 #ifdef ACCURATEPROFILE
314 BAMBOO_DEBUGPRINT(0xee02);
319 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
322 BAMBOO_DEBUGPRINT(0xee03);
327 (waitconfirm && (numconfirm == 0))) {
329 BAMBOO_DEBUGPRINT(0xee04);
330 BAMBOO_DEBUGPRINT_REG(waitconfirm);
332 BAMBOO_START_CRITICAL_SECTION_STATUS();
334 BAMBOO_DEBUGPRINT(0xf001);
336 corestatus[BAMBOO_NUM_OF_CORE] = 0;
337 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
338 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
339 // check the status of all cores
342 BAMBOO_DEBUGPRINT_REG(NUMCORES);
344 for(i = 0; i < NUMCORES; ++i) {
346 BAMBOO_DEBUGPRINT(0xe000 + corestatus[i]);
348 if(corestatus[i] != 0) {
354 // check if the sum of send objs and receive obj are the same
355 // yes->check if the info is the latest; no->go on executing
357 for(i = 0; i < NUMCORES; ++i) {
358 sumsendobj += numsendobjs[i];
360 BAMBOO_DEBUGPRINT(0xf000 + numsendobjs[i]);
363 for(i = 0; i < NUMCORES; ++i) {
364 sumsendobj -= numreceiveobjs[i];
366 BAMBOO_DEBUGPRINT(0xf000 + numreceiveobjs[i]);
369 if(0 == sumsendobj) {
371 // the first time found all cores stall
372 // send out status confirm msg to all other cores
373 // reset the corestatus array too
375 BAMBOO_DEBUGPRINT(0xee05);
377 corestatus[BAMBOO_NUM_OF_CORE] = 1;
378 for(i = 1; i < NUMCORES; ++i) {
380 // send status confirm msg to core i
384 numconfirm = NUMCORES - 1;
386 // all the core status info are the latest
387 // terminate; for profiling mode, send request to all
388 // other cores to pour out profiling data
390 BAMBOO_DEBUGPRINT(0xee06);
394 totalexetime = BAMBOO_GET_EXE_TIME();
396 BAMBOO_DEBUGPRINT(BAMBOO_GET_EXE_TIME());
397 BAMBOO_DEBUGPRINT_REG(total_num_t6); // TODO for test
398 BAMBOO_DEBUGPRINT(0xbbbbbbbb);
400 // profile mode, send msgs to other cores to request pouring
401 // out progiling data
403 BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
405 BAMBOO_DEBUGPRINT(0xf000);
407 for(i = 1; i < NUMCORES; ++i) {
408 // send profile request msg to core i
409 send_msg_2(i, 6, totalexetime);
411 // pour profiling data on startup core
414 BAMBOO_START_CRITICAL_SECTION_STATUS();
416 BAMBOO_DEBUGPRINT(0xf001);
418 profilestatus[BAMBOO_NUM_OF_CORE] = 0;
419 // check the status of all cores
422 BAMBOO_DEBUGPRINT_REG(NUMCORES);
424 for(i = 0; i < NUMCORES; ++i) {
426 BAMBOO_DEBUGPRINT(0xe000 + profilestatus[i]);
428 if(profilestatus[i] != 0) {
435 BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
437 BAMBOO_DEBUGPRINT(0xf000);
446 terminate(); // All done.
447 } // if-else of line 364: if(!waitconfirm)
449 // still some objects on the fly on the network
450 // reset the waitconfirm and numconfirm
452 BAMBOO_DEBUGPRINT(0xee07);
456 } // if-else of line 363: if(0 == sumsendobj)
458 // not all cores are stall, keep on waiting
460 BAMBOO_DEBUGPRINT(0xee08);
464 } // if-else of line 347: if(allStall)
465 BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
467 BAMBOO_DEBUGPRINT(0xf000);
469 } // if-else of line 320: if((!waitconfirm) ||
473 BAMBOO_DEBUGPRINT(0xee09);
479 // wait for some time
482 BAMBOO_DEBUGPRINT(0xee0a);
488 // send StallMsg to startup core
490 BAMBOO_DEBUGPRINT(0xee0b);
493 send_msg_4(STARTUPCORE, 1, BAMBOO_NUM_OF_CORE, self_numsendobjs, self_numreceiveobjs);
505 BAMBOO_DEBUGPRINT(0xee0c);
507 } // if-else of line 464: if(!sendStall)
508 } // if-else of line 313: if(STARTUPCORE == BAMBOO_NUM_OF_CORE)
509 } // if-else of line 311: if(!tocontinue)
510 } // line 193: while(true)
511 } // right-bracket for if-else of line 153: if(BAMBOO_NUM_OF_CORE > NUMCORES - 1)
515 void createstartupobject(int argc, char ** argv) {
518 /* Allocate startup object */
521 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
522 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
524 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
525 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
527 #endif // #if 0: for garbage collection
528 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
529 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
530 /* Build array of strings */
531 startupobject->___parameters___=stringarray;
532 for(i=1; i<argc; i++) {
533 int length=strlen(argv[i]);
536 struct ___String___ *newstring=NewString(NULL, argv[i],length);
538 struct ___String___ *newstring=NewString(argv[i],length);
540 #endif // #if 0: for garbage collection
541 struct ___String___ *newstring=NewString(argv[i],length);
542 ((void **)(((char *)&stringarray->___length___)+sizeof(int)))[i-1]=newstring;
545 startupobject->version = 0;
546 startupobject->lock = NULL;
548 /* Set initialized flag for startup object */
549 flagorandinit(startupobject,1,0xFFFFFFFF);
550 enqueueObject(startupobject, NULL, 0);
552 BAMBOO_CACHE_FLUSH_ALL();
556 int hashCodetpd(struct taskparamdescriptor *ftd) {
557 int hash=(int)ftd->task;
559 for(i=0; i<ftd->numParameters; i++) {
560 hash^=(int)ftd->parameterArray[i];
565 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
567 if (ftd1->task!=ftd2->task)
569 for(i=0; i<ftd1->numParameters; i++)
570 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
575 /* This function sets a tag. */
578 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
580 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
582 #endif // #if 0: for garbage collection
583 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
584 struct ArrayObject * ao=NULL;
585 struct ___Object___ * tagptr=obj->___tags___;
587 obj->___tags___=(struct ___Object___ *)tagd;
589 /* Have to check if it is already set */
590 if (tagptr->type==TAGTYPE) {
591 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
597 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
598 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
599 obj=(struct ___Object___ *)ptrarray[2];
600 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
601 td=(struct ___TagDescriptor___ *) obj->___tags___;
603 ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
605 #endif // #if 0: for garbage collection
606 ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
608 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
609 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
610 obj->___tags___=(struct ___Object___ *) ao;
611 ao->___cachedCode___=2;
615 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
616 for(i=0; i<ao->___cachedCode___; i++) {
617 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
622 if (ao->___cachedCode___<ao->___length___) {
623 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
624 ao->___cachedCode___++;
628 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
629 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
630 obj=(struct ___Object___ *)ptrarray[2];
631 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
632 ao=(struct ArrayObject *)obj->___tags___;
634 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
636 #endif // #if 0: for garbage collection
637 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
639 aonew->___cachedCode___=ao->___length___+1;
640 for(i=0; i<ao->___length___; i++) {
641 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
643 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
649 struct ___Object___ * tagset=tagd->flagptr;
652 } else if (tagset->type!=OBJECTARRAYTYPE) {
655 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
656 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
657 obj=(struct ___Object___ *)ptrarray[2];
658 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
660 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
662 #endif // #if 0: for garbage collection
663 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
664 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
665 ARRAYSET(ao, struct ___Object___ *, 1, obj);
666 ao->___cachedCode___=2;
667 tagd->flagptr=(struct ___Object___ *)ao;
669 struct ArrayObject *ao=(struct ArrayObject *) tagset;
670 if (ao->___cachedCode___<ao->___length___) {
671 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
676 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
677 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
678 obj=(struct ___Object___ *)ptrarray[2];
679 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
680 ao=(struct ArrayObject *)tagd->flagptr;
682 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
684 #endif // #if 0: for garbage collection
685 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
686 aonew->___cachedCode___=ao->___cachedCode___+1;
687 for(i=0; i<ao->___length___; i++) {
688 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
690 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
691 tagd->flagptr=(struct ___Object___ *) aonew;
697 /* This function clears a tag. */
700 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
702 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
704 #endif // #if 0: for garbage collection
705 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
706 /* We'll assume that tag is alway there.
707 Need to statically check for this of course. */
708 struct ___Object___ * tagptr=obj->___tags___;
710 if (tagptr->type==TAGTYPE) {
711 if ((struct ___TagDescriptor___ *)tagptr==tagd)
712 obj->___tags___=NULL;
715 printf("ERROR 1 in tagclear\n");
720 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
722 for(i=0; i<ao->___cachedCode___; i++) {
723 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
725 ao->___cachedCode___--;
726 if (i<ao->___cachedCode___)
727 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
728 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
729 if (ao->___cachedCode___==0)
730 obj->___tags___=NULL;
735 printf("ERROR 2 in tagclear\n");
740 struct ___Object___ *tagset=tagd->flagptr;
741 if (tagset->type!=OBJECTARRAYTYPE) {
746 printf("ERROR 3 in tagclear\n");
751 struct ArrayObject *ao=(struct ArrayObject *) tagset;
753 for(i=0; i<ao->___cachedCode___; i++) {
754 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
756 ao->___cachedCode___--;
757 if (i<ao->___cachedCode___)
758 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
759 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
760 if (ao->___cachedCode___==0)
766 printf("ERROR 4 in tagclear\n");
775 /* This function allocates a new tag. */
777 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
778 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
780 struct ___TagDescriptor___ * allocate_tag(int index) {
781 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
783 #endif // #if 0: for garbage collection
784 struct ___TagDescriptor___ * allocate_tag(int index) {
785 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
793 /* This function updates the flag for object ptr. It or's the flag
794 with the or mask and and's it with the andmask. */
796 void flagbody(struct ___Object___ *ptr, int flag, struct parameterwrapper ** queues, int length, bool isnew);
798 int flagcomp(const int *val1, const int *val2) {
799 return (*val1)-(*val2);
802 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length) {
804 int oldflag=((int *)ptr)[1];
805 int flag=ormask|oldflag;
807 flagbody(ptr, flag, queues, length, false);
811 bool intflagorand(void * ptr, int ormask, int andmask) {
813 int oldflag=((int *)ptr)[1];
814 int flag=ormask|oldflag;
816 if (flag==oldflag) /* Don't do anything */
819 flagbody(ptr, flag, NULL, 0, false);
825 void flagorandinit(void * ptr, int ormask, int andmask) {
826 int oldflag=((int *)ptr)[1];
827 int flag=ormask|oldflag;
829 flagbody(ptr,flag,NULL,0,true);
832 void flagbody(struct ___Object___ *ptr, int flag, struct parameterwrapper ** vqueues, int vlength, bool isnew) {
833 struct parameterwrapper * flagptr = NULL;
835 struct parameterwrapper ** queues = vqueues;
836 int length = vlength;
839 int * enterflags = NULL;
840 if((!isnew) && (queues == NULL)) {
841 if(BAMBOO_NUM_OF_CORE < NUMCORES) {
842 queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
843 length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
850 /*Remove object from all queues */
851 for(i = 0; i < length; ++i) {
853 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
854 ObjectHashremove(flagptr->objectset, (int)ptr);
855 if (enterflags!=NULL)
860 void enqueueObject(void * vptr, struct parameterwrapper ** vqueues, int vlength) {
861 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
864 //struct QueueItem *tmpptr;
865 struct parameterwrapper * parameter=NULL;
868 struct parameterwrapper * prevptr=NULL;
869 struct ___Object___ *tagptr=NULL;
870 struct parameterwrapper ** queues = vqueues;
871 int length = vlength;
872 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
876 queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
877 length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
879 tagptr=ptr->___tags___;
881 /* Outer loop iterates through all parameter queues an object of
882 this type could be in. */
883 for(j = 0; j < length; ++j) {
884 parameter = queues[j];
886 if (parameter->numbertags>0) {
888 goto nextloop; //that means the object has no tag but that param needs tag
889 else if(tagptr->type==TAGTYPE) { //one tag
890 //struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
891 for(i=0; i<parameter->numbertags; i++) {
892 //slotid is parameter->tagarray[2*i];
893 int tagid=parameter->tagarray[2*i+1];
894 if (tagid!=tagptr->flag)
895 goto nextloop; /*We don't have this tag */
897 } else { //multiple tags
898 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
899 for(i=0; i<parameter->numbertags; i++) {
900 //slotid is parameter->tagarray[2*i];
901 int tagid=parameter->tagarray[2*i+1];
903 for(j=0; j<ao->___cachedCode___; j++) {
904 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
915 for(i=0; i<parameter->numberofterms; i++) {
916 int andmask=parameter->intarray[i*2];
917 int checkmask=parameter->intarray[i*2+1];
918 if ((ptr->flag&andmask)==checkmask) {
919 enqueuetasks(parameter, prevptr, ptr, NULL, 0);
930 void enqueueObject_I(void * vptr, struct parameterwrapper ** vqueues, int vlength) {
931 struct ___Object___ *ptr = (struct ___Object___ *)vptr;
934 //struct QueueItem *tmpptr;
935 struct parameterwrapper * parameter=NULL;
938 struct parameterwrapper * prevptr=NULL;
939 struct ___Object___ *tagptr=NULL;
940 struct parameterwrapper ** queues = vqueues;
941 int length = vlength;
942 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
946 queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
947 length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
949 tagptr=ptr->___tags___;
951 /* Outer loop iterates through all parameter queues an object of
952 this type could be in. */
953 for(j = 0; j < length; ++j) {
954 parameter = queues[j];
956 if (parameter->numbertags>0) {
958 goto nextloop; //that means the object has no tag but that param needs tag
959 else if(tagptr->type==TAGTYPE) { //one tag
960 //struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
961 for(i=0; i<parameter->numbertags; i++) {
962 //slotid is parameter->tagarray[2*i];
963 int tagid=parameter->tagarray[2*i+1];
964 if (tagid!=tagptr->flag)
965 goto nextloop; /*We don't have this tag */
967 } else { //multiple tags
968 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
969 for(i=0; i<parameter->numbertags; i++) {
970 //slotid is parameter->tagarray[2*i];
971 int tagid=parameter->tagarray[2*i+1];
973 for(j=0; j<ao->___cachedCode___; j++) {
974 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
985 for(i=0; i<parameter->numberofterms; i++) {
986 int andmask=parameter->intarray[i*2];
987 int checkmask=parameter->intarray[i*2+1];
988 if ((ptr->flag&andmask)==checkmask) {
989 enqueuetasks_I(parameter, prevptr, ptr, NULL, 0);
1001 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl) {
1003 return (int*)(RUNMALLOC(sizeof(int)));
1008 bool redirect = false;
1009 int redirectlock = 0;
1010 for(; i < length; i++) {
1011 struct ___Object___ * ptr = (struct ___Object___ *)(ptrs[i]);
1014 if(ptr->lock == NULL) {
1017 lock = (int)(ptr->lock);
1020 if(lock != redirectlock) {
1021 RuntimeHashadd(tbl, lock, redirectlock);
1024 if(RuntimeHashcontainskey(tbl, lock)) {
1025 // already redirected
1027 RuntimeHashget(tbl, lock, &redirectlock);
1028 for(; j < locklen; j++) {
1029 if(locks[j] != redirectlock) {
1030 RuntimeHashadd(tbl, locks[j], redirectlock);
1035 for(j = 0; j < locklen; j++) {
1036 if(locks[j] == lock) {
1039 } else if(locks[j] > lock) {
1046 locks[h] = locks[h-1];
1055 return (int *)redirectlock;
1057 return (int *)(locks[0]);
1062 void addAliasLock(void * ptr, int lock) {
1063 struct ___Object___ * obj = (struct ___Object___ *)ptr;
1064 if(((int)ptr != lock) && (obj->lock != (int*)lock)) {
1065 // originally no alias lock associated or have a different alias lock
1066 // flush it as the new one
1067 obj->lock = (int *)lock;
1072 inline void setTaskExitIndex(int index) {
1073 taskInfoArray[taskInfoIndex]->exitIndex = index;
1076 inline void addNewObjInfo(void * nobj) {
1077 if(taskInfoArray[taskInfoIndex]->newObjs == NULL) {
1078 taskInfoArray[taskInfoIndex]->newObjs = createQueue();
1080 addNewItem(taskInfoArray[taskInfoIndex]->newObjs, nobj);
1086 * type: 0 -- transfer object
1087 * 1 -- transfer stall msg
1092 * // add for profile info
1093 * 6 -- transfer profile output msg
1094 * 7 -- transfer profile output finish msg
1095 * // add for alias lock strategy
1096 * 8 -- redirect lock request
1097 * 9 -- lock grant with redirect info
1098 * a -- lock deny with redirect info
1099 * b -- lock release with redirect info
1100 * c -- status confirm request
1101 * d -- status report msg
1103 * f -- requiring for new memory
1104 * 10 -- response for new memory request
1106 * 12 -- compact phase start
1107 * 13 -- flush phase start
1108 * 14 -- mark phase finish
1109 * 15 -- compact phase finish
1110 * 16 -- flush phase finish
1112 * 18 -- marked phase finish confirm request
1113 * 19 -- marked phase finish confirm response
1114 * 1a -- markedObj msg
1115 * 1b -- start moving objs msg
1116 * 1c -- ask for mapping info of a markedObj
1117 * 1d -- mapping info of a markedObj
1118 * 1e -- large objs info request
1119 * 1f -- large objs info response
1121 * ObjMsg: 0 + size of msg + obj's address + (task index + param index)+
1122 * StallMsg: 1 + corenum + sendobjs + receiveobjs (size is always 4 * sizeof(int))
1123 * LockMsg: 2 + lock type + obj pointer + lock + request core (size is always 5 * sizeof(int))
1124 * 3/4/5 + lock type + obj pointer + lock (size is always 4 * sizeof(int))
1125 * 8 + lock type + obj pointer + redirect lock + root request core + request core (size is always 6 * sizeof(int))
1126 * 9/a + lock type + obj pointer + redirect lock (size is always 4 * sizeof(int))
1127 * b + lock type + lock + redirect lock (size is always 4 * sizeof(int))
1128 * lock type: 0 -- read; 1 -- write
1129 * ProfileMsg: 6 + totalexetime (size is always 2 * sizeof(int))
1130 * 7 + corenum (size is always 2 * sizeof(int))
1131 * StatusMsg: c (size is always 1 * sizeof(int))
1132 * d + status + corenum (size is always 3 * sizeof(int))
1133 * status: 0 -- stall; 1 -- busy
1134 * TerminateMsg: e (size is always 1 * sizeof(int)
1135 * MemoryMsg: f + size + corenum (size is always 3 * sizeof(int))
1136 * 10 + base_va + size (size is always 3 * sizeof(int))
1137 * GCMsg: 11 (size is always 1 * sizeof(int))
1138 * 12 + size of msg + (num of objs to move + (start address + end address + dst core + start dst)+)? + (num of incoming objs + (start dst + orig core)+)? + (num of large obj lists + (start address + lenght + start dst)+)?
1139 * 13 (size is always 1 * sizeof(int))
1140 * 14 + corenum + gcsendobjs + gcreceiveobjs (size if always 4 * sizeof(int))
1141 * 15/16 + corenum (size is always 2 * sizeof(int))
1142 * 17 (size is always 1 * sizeof(int))
1143 * 18 (size if always 1 * sizeof(int))
1144 * 19 + size of msg + corenum + gcsendobjs + gcreceiveobjs (size is always 5 * sizeof(int))
1145 * 1a + obj's address (size is always 2 * sizeof(int))
1146 * 1b + corenum ( size is always 2 * sizeof(int))
1147 * 1c + obj's address + corenum (size is always 3 * sizeof(int))
1148 * 1d + obj's address + dst address (size if always 3 * sizeof(int))
1149 * 1e (size is always 1 * sizeof(int))
1150 * 1f + size of msg + corenum + (num of large obj lists + (start address + length)+)?
1152 * NOTE: for Tilera, all GCMsgs except the GC start msg should be processed with a different net/port with other msgs
1155 // receive object transferred from other cores
1156 // or the terminate message from other cores
1157 // Should be invoked in critical sections!!
1158 // NOTICE: following format is for threadsimulate version only
1159 // RAW version please see previous description
1160 // format: type + object
1161 // type: -1--stall msg
1163 // return value: 0--received an object
1164 // 1--received nothing
1165 // 2--received a Stall Msg
1166 // 3--received a lock Msg
1167 // RAW version: -1 -- received nothing
1168 // otherwise -- received msg type
1169 int receiveObject() {
1173 if(receiveMsg() == -1) {
1177 if(msgdataindex == msglength) {
1178 // received a whole msg
1179 int type, data1; // will receive at least 2 words including type
1184 // receive a object transfer msg
1185 struct transObjInfo * transObj = RUNMALLOC_I(sizeof(struct transObjInfo));
1189 BAMBOO_DEBUGPRINT(0xe880);
1192 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1194 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1196 BAMBOO_EXIT(0xa005);
1198 // store the object and its corresponding queue info, enqueue it later
1199 transObj->objptr = (void *)msgdata[2]; // data1 is now size of the msg
1200 transObj->length = (msglength - 3) / 2;
1201 transObj->queues = RUNMALLOC_I(sizeof(int)*(msglength - 3));
1202 for(k = 0; k < transObj->length; ++k) {
1203 transObj->queues[2*k] = msgdata[3+2*k];
1206 BAMBOO_DEBUGPRINT_REG(transObj->queues[2*k]);
1209 transObj->queues[2*k+1] = msgdata[3+2*k+1];
1212 BAMBOO_DEBUGPRINT_REG(transObj->queues[2*k+1]);
1216 // check if there is an existing duplicate item
1218 struct QueueItem * qitem = getHead(&objqueue);
1219 struct QueueItem * prev = NULL;
1220 while(qitem != NULL) {
1221 struct transObjInfo * tmpinfo = (struct transObjInfo *)(qitem->objectptr);
1222 if(tmpinfo->objptr == transObj->objptr) {
1223 // the same object, remove outdate one
1224 removeItem(&objqueue, qitem);
1230 qitem = getHead(&objqueue);
1232 qitem = getNextQueueItem(prev);
1235 addNewItem_I(&objqueue, (void *)transObj);
1237 ++(self_numreceiveobjs);
1242 // receive a stall msg
1243 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1244 // non startup core can not receive stall msg
1247 BAMBOO_DEBUGPRINT_REG(data1);
1249 BAMBOO_EXIT(0xa006);
1251 if(data1 < NUMCORES) {
1254 BAMBOO_DEBUGPRINT(0xe881);
1257 corestatus[data1] = 0;
1258 numsendobjs[data1] = msgdata[2];
1259 numreceiveobjs[data1] = msgdata[3];
1265 // receive lock request msg, handle it right now
1266 // check to see if there is a lock exist for the required obj
1267 // data1 -> lock type
1268 int data2 = msgdata[2]; // obj pointer
1269 int data3 = msgdata[3]; // lock
1270 int data4 = msgdata[4]; // request core
1271 deny = processlockrequest(data1, data3, data2, data4, data4, true); // -1: redirected, 0: approved, 1: denied
1273 // this lock request is redirected
1276 // send response msg
1277 // for 32 bit machine, the size is always 4 words
1278 int tmp = deny==1?4:3;
1280 cache_msg_4(data4, tmp, data1, data2, data3);
1282 send_msg_4(data4, tmp, data1, data2, data3);
1289 // receive lock grount msg
1290 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1292 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1294 BAMBOO_EXIT(0xa007);
1296 if((lockobj == msgdata[2]) && (lock2require == msgdata[3])) {
1299 BAMBOO_DEBUGPRINT(0xe882);
1308 // conflicts on lockresults
1310 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1312 BAMBOO_EXIT(0xa008);
1318 // receive lock deny msg
1319 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1321 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1323 BAMBOO_EXIT(0xa009);
1325 if((lockobj == msgdata[2]) && (lock2require == msgdata[3])) {
1328 BAMBOO_DEBUGPRINT(0xe883);
1337 // conflicts on lockresults
1339 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1341 BAMBOO_EXIT(0xa00a);
1347 // receive lock release msg
1348 processlockrelease(data1, msgdata[2], 0, false);
1354 // receive an output profile data request msg
1355 if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
1356 // startup core can not receive profile output finish msg
1357 BAMBOO_EXIT(0xa00c);
1361 BAMBOO_DEBUGPRINT(0xe885);
1365 totalexetime = data1;
1366 outputProfileData();
1368 cache_msg_2(STARTUPCORE, 7, BAMBOO_NUM_OF_CORE);
1370 send_msg_2(STARTUPCORE, 7, BAMBOO_NUM_OF_CORE);
1376 // receive a profile output finish msg
1377 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1378 // non startup core can not receive profile output finish msg
1380 BAMBOO_DEBUGPRINT_REG(data1);
1382 BAMBOO_EXIT(0xa00d);
1386 BAMBOO_DEBUGPRINT(0xe886);
1389 profilestatus[data1] = 0;
1395 // receive a redirect lock request msg, handle it right now
1396 // check to see if there is a lock exist for the required obj
1397 // data1 -> lock type
1398 int data2 = msgdata[2]; // obj pointer
1399 int data3 = msgdata[3]; // redirect lock
1400 int data4 = msgdata[4]; // root request core
1401 int data5 = msgdata[5]; // request core
1402 deny = processlockrequest(data1, data3, data2, data5, data4, true);
1404 // this lock request is redirected
1407 // send response msg
1408 // for 32 bit machine, the size is always 4 words
1410 cache_msg_4(data4, deny==1?0xa:9, data1, data2, data3);
1412 send_msg_4(data4, deny==1?0xa:9, data1, data2, data3);
1419 // receive a lock grant msg with redirect info
1420 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1422 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1424 BAMBOO_EXIT(0xa00e);
1426 if(lockobj == msgdata[2]) {
1429 BAMBOO_DEBUGPRINT(0xe891);
1434 RuntimeHashadd_I(objRedirectLockTbl, lockobj, msgdata[3]);
1439 // conflicts on lockresults
1441 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1443 BAMBOO_EXIT(0xa00f);
1449 // receive a lock deny msg with redirect info
1450 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1452 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1454 BAMBOO_EXIT(0xa010);
1456 if(lockobj == msgdata[2]) {
1459 BAMBOO_DEBUGPRINT(0xe892);
1468 // conflicts on lockresults
1470 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1472 BAMBOO_EXIT(0xa011);
1478 // receive a lock release msg with redirect info
1479 processlockrelease(data1, msgdata[2], msgdata[3], true);
1484 // receive a status confirm info
1485 if((BAMBOO_NUM_OF_CORE == STARTUPCORE) || (BAMBOO_NUM_OF_CORE > NUMCORES - 1)) {
1486 // wrong core to receive such msg
1487 BAMBOO_EXIT(0xa013);
1489 // send response msg
1492 BAMBOO_DEBUGPRINT(0xe887);
1496 cache_msg_3(STARTUPCORE, 0xd, busystatus?1:0, BAMBOO_NUM_OF_CORE);
1498 send_msg_3(STARTUPCORE, 0xd, busystatus?1:0, BAMBOO_NUM_OF_CORE);
1505 // receive a status confirm info
1506 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1507 // wrong core to receive such msg
1509 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1511 BAMBOO_EXIT(0xa014);
1515 BAMBOO_DEBUGPRINT(0xe888);
1521 corestatus[msgdata[2]] = msgdata[1];
1527 // receive a terminate msg
1530 BAMBOO_DEBUGPRINT(0xe889);
1538 // receive a shared memory request msg
1539 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1540 // wrong core to receive such msg
1542 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1544 BAMBOO_EXIT(0xa015);
1548 BAMBOO_DEBUGPRINT(0xe88a);
1551 // TODO change for GC
1552 void * mem = mspace_calloc(bamboo_free_msp, 1, msgdata[1]);
1554 BAMBOO_DEBUGPRINT(0xa016);
1555 BAMBOO_EXIT(0xa016);
1557 // send the start_va to request core
1559 cache_msg_3(msgdata[2], 0x10, mem, msgdata[1]);
1561 send_msg_3( msgdata[2], 0x10, mem, msgdata[1]);
1568 // receive a shared memory response msg
1571 BAMBOO_DEBUGPRINT(0xe88b);
1574 if(msgdata[2] == 0) {
1575 bamboo_smem_size = 0;
1576 bamboo_cur_msp = NULL;
1578 bamboo_smem_size = msgdata[2];
1579 bamboo_cur_msp = create_mspace_with_base((void*)msgdata[1], msgdata[2], 0);
1586 // receive a start GC msg
1589 BAMBOO_DEBUGPRINT(0xe88c);
1599 for(msgdataindex--; msgdataindex > 0; --msgdataindex) {
1600 msgdata[msgdataindex] = -1;
1606 BAMBOO_DEBUGPRINT(0xe88d);
1610 if(BAMBOO_MSG_AVAIL() != 0) {
1623 BAMBOO_DEBUGPRINT(0xe88e);
1627 /* if(isInterrupt) {
1636 ent enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
1637 void * taskpointerarray[MAXTASKPARAMS];
1639 //int numparams=parameter->task->numParameters;
1640 int numiterators=parameter->task->numTotal-1;
1645 struct taskdescriptor * task=parameter->task;
1647 //this add the object to parameterwrapper
1648 ObjectHashadd(parameter->objectset, (int) ptr, 0, (int) enterflags, numenterflags, enterflags==NULL);
1650 /* Add enqueued object to parameter vector */
1651 taskpointerarray[parameter->slot]=ptr;
1653 /* Reset iterators */
1654 for(j=0; j<numiterators; j++) {
1655 toiReset(¶meter->iterators[j]);
1658 /* Find initial state */
1659 for(j=0; j<numiterators; j++) {
1661 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
1662 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
1664 /* Need to backtrack */
1665 toiReset(¶meter->iterators[j]);
1669 /* Nothing to enqueue */
1675 /* Enqueue current state */
1677 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
1679 tpd->numParameters=numiterators+1;
1680 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
1682 for(j=0; j<=numiterators; j++) {
1683 tpd->parameterArray[j]=taskpointerarray[j]; //store the actual parameters
1686 if ((/*!gencontains(failedtasks, tpd)&&*/ !gencontains(activetasks,tpd))) {
1687 genputtable(activetasks, tpd, tpd);
1689 RUNFREE(tpd->parameterArray);
1693 /* This loop iterates to the next parameter combination */
1694 if (numiterators==0)
1697 for(j=numiterators-1; j<numiterators; j++) {
1699 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
1700 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
1702 /* Need to backtrack */
1703 toiReset(¶meter->iterators[j]);
1707 /* Nothing more to enqueue */
1715 int enqueuetasks_I(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
1716 void * taskpointerarray[MAXTASKPARAMS];
1718 //int numparams=parameter->task->numParameters;
1719 int numiterators=parameter->task->numTotal-1;
1724 struct taskdescriptor * task=parameter->task;
1726 //this add the object to parameterwrapper
1727 ObjectHashadd_I(parameter->objectset, (int) ptr, 0, (int) enterflags, numenterflags, enterflags==NULL);
1729 /* Add enqueued object to parameter vector */
1730 taskpointerarray[parameter->slot]=ptr;
1732 /* Reset iterators */
1733 for(j=0; j<numiterators; j++) {
1734 toiReset(¶meter->iterators[j]);
1737 /* Find initial state */
1738 for(j=0; j<numiterators; j++) {
1740 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
1741 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
1743 /* Need to backtrack */
1744 toiReset(¶meter->iterators[j]);
1748 /* Nothing to enqueue */
1754 /* Enqueue current state */
1756 struct taskparamdescriptor *tpd=RUNMALLOC_I(sizeof(struct taskparamdescriptor));
1758 tpd->numParameters=numiterators+1;
1759 tpd->parameterArray=RUNMALLOC_I(sizeof(void *)*(numiterators+1));
1761 for(j=0; j<=numiterators; j++) {
1762 tpd->parameterArray[j]=taskpointerarray[j]; //store the actual parameters
1765 if ((/*!gencontains(failedtasks, tpd)&&*/ !gencontains(activetasks,tpd))) {
1766 genputtable_I(activetasks, tpd, tpd);
1768 RUNFREE(tpd->parameterArray);
1772 /* This loop iterates to the next parameter combination */
1773 if (numiterators==0)
1776 for(j=numiterators-1; j<numiterators; j++) {
1778 if(toiHasNext(¶meter->iterators[j], taskpointerarray OPTARG(failed)))
1779 toiNext(¶meter->iterators[j], taskpointerarray OPTARG(failed));
1781 /* Need to backtrack */
1782 toiReset(¶meter->iterators[j]);
1786 /* Nothing more to enqueue */
1794 /* Handler for signals. The signals catch null pointer errors and
1795 arithmatic errors. */
1797 void myhandler(int sig, siginfo_t *info, void *uap) {
1800 printf("sig=%d\n",sig);
1803 sigemptyset(&toclear);
1804 sigaddset(&toclear, sig);
1805 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
1806 longjmp(error_handler,1);
1813 struct RuntimeHash *fdtoobject;
1815 void addreadfd(int fd) {
1818 FD_SET(fd, &readfds);
1821 void removereadfd(int fd) {
1822 FD_CLR(fd, &readfds);
1823 if (maxreadfd==(fd+1)) {
1825 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1837 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag);
1839 void executetasks() {
1840 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1843 struct ___Object___ * tmpparam = NULL;
1844 struct parameterdescriptor * pd=NULL;
1845 struct parameterwrapper *pw=NULL;
1850 struct LockValue locks[MAXTASKPARAMS];
1857 /* Set up signal handlers */
1858 struct sigaction sig;
1859 sig.sa_sigaction=&myhandler;
1860 sig.sa_flags=SA_SIGINFO;
1861 sigemptyset(&sig.sa_mask);
1863 /* Catch bus errors, segmentation faults, and floating point exceptions*/
1864 sigaction(SIGBUS,&sig,0);
1865 sigaction(SIGSEGV,&sig,0);
1866 sigaction(SIGFPE,&sig,0);
1867 sigaction(SIGPIPE,&sig,0);
1868 #endif // #if 0: non-multicore
1878 fdtoobject=allocateRuntimeHash(100);
1882 /* Map first block of memory to protected, anonymous page */
1883 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1888 while(hashsize(activetasks)>0) {
1890 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1893 BAMBOO_DEBUGPRINT(0xe990);
1896 /* Check if any filedescriptors have IO pending */
1899 struct timeval timeout={0,0};
1903 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1905 /* Process ready fd's */
1907 for(fd=0; fd<maxreadfd; fd++) {
1908 if (FD_ISSET(fd, &tmpreadfds)) {
1909 /* Set ready flag on object */
1911 // printf("Setting fd %d\n",fd);
1912 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1913 if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1914 enqueueObject(objptr, NULL, 0);
1923 /* See if there are any active tasks */
1924 if (hashsize(activetasks)>0) {
1927 #ifdef ACCURATEPROFILE
1928 profileTaskStart("tpd checking");
1932 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1933 genfreekey(activetasks, currtpd);
1935 numparams=currtpd->task->numParameters;
1936 numtotal=currtpd->task->numTotal;
1938 // clear the lockRedirectTbl (TODO, this table should be empty after all locks are released)
1940 for(j = 0; j < MAXTASKPARAMS; j++) {
1941 locks[j].redirectlock = 0;
1944 // get all required locks
1946 // check which locks are needed
1947 for(i = 0; i < numparams; i++) {
1948 void * param = currtpd->parameterArray[i];
1952 if(((struct ___Object___ *)param)->type == STARTUPTYPE) {
1954 taskpointerarray[i+OFFSET]=param;
1957 if(((struct ___Object___ *)param)->lock == NULL) {
1958 tmplock = (int)param;
1960 tmplock = (int)(((struct ___Object___ *)param)->lock);
1962 // insert into the locks array
1963 for(j = 0; j < locklen; j++) {
1964 if(locks[j].value == tmplock) {
1967 } else if(locks[j].value > tmplock) {
1974 locks[h].redirectlock = locks[h-1].redirectlock;
1975 locks[h].value = locks[h-1].value;
1977 locks[j].value = tmplock;
1978 locks[j].redirectlock = (int)param;
1981 } // line 2713: for(i = 0; i < numparams; i++)
1982 // grab these required locks
1984 BAMBOO_DEBUGPRINT(0xe991);
1986 for(i = 0; i < locklen; i++) {
1987 int * lock = (int *)(locks[i].redirectlock);
1989 // require locks for this parameter if it is not a startup object
1991 BAMBOO_DEBUGPRINT_REG((int)lock);
1992 BAMBOO_DEBUGPRINT_REG((int)(locks[i].value));
1995 BAMBOO_START_CRITICAL_SECTION();
1997 BAMBOO_DEBUGPRINT(0xf001);
2000 //isInterrupt = false;
2003 BAMBOO_WAITING_FOR_LOCK();
2007 while(BAMBOO_WAITING_FOR_LOCK() != -1) {
2011 grount = lockresult;
2021 //isInterrupt = true;
2023 BAMBOO_CLOSE_CRITICAL_SECTION();
2025 BAMBOO_DEBUGPRINT(0xf000);
2031 BAMBOO_DEBUGPRINT(0xe992);
2033 // can not get the lock, try later
2034 // releas all grabbed locks for previous parameters
2035 for(j = 0; j < i; ++j) {
2036 lock = (int*)(locks[j].redirectlock);
2037 releasewritelock(lock);
2039 genputtable(activetasks, currtpd, currtpd);
2040 if(hashsize(activetasks) == 1) {
2041 // only one task right now, wait a little while before next try
2047 #ifdef ACCURATEPROFILE
2048 // fail, set the end of the checkTaskInfo
2053 } // line 2794: if(grount == 0)
2054 } // line 2752: for(i = 0; i < locklen; i++)
2057 BAMBOO_DEBUGPRINT(0xe993);
2059 /* Make sure that the parameters are still in the queues */
2060 for(i=0; i<numparams; i++) {
2061 void * parameter=currtpd->parameterArray[i];
2065 BAMBOO_CACHE_FLUSH_RANGE((int)parameter, classsize[((struct ___Object___ *)parameter)->type]);
2067 BAMBOO_START_CRITICAL_SECTION_LOCK();
2069 BAMBOO_DEBUGPRINT(0xf001);
2071 if(RuntimeHashcontainskey(objRedirectLockTbl, (int)parameter)) {
2072 int redirectlock_r = 0;
2073 RuntimeHashget(objRedirectLockTbl, (int)parameter, &redirectlock_r);
2074 ((struct ___Object___ *)parameter)->lock = redirectlock_r;
2075 RuntimeHashremovekey(objRedirectLockTbl, (int)parameter);
2077 BAMBOO_CLOSE_CRITICAL_SECTION_LOCK();
2079 BAMBOO_DEBUGPRINT(0xf000);
2083 tmpparam = (struct ___Object___ *)parameter;
2084 pd=currtpd->task->descriptorarray[i];
2085 pw=(struct parameterwrapper *) pd->queue;
2086 /* Check that object is still in queue */
2088 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
2090 BAMBOO_DEBUGPRINT(0xe994);
2092 // release grabbed locks
2093 for(j = 0; j < locklen; ++j) {
2094 int * lock = (int *)(locks[j].redirectlock);
2095 releasewritelock(lock);
2097 RUNFREE(currtpd->parameterArray);
2102 /* Check if the object's flags still meets requirements */
2106 for(tmpi = 0; tmpi < pw->numberofterms; ++tmpi) {
2107 andmask=pw->intarray[tmpi*2];
2108 checkmask=pw->intarray[tmpi*2+1];
2109 if((((struct ___Object___ *)parameter)->flag&andmask)==checkmask) {
2115 // flags are never suitable
2116 // remove this obj from the queue
2118 int UNUSED, UNUSED2;
2121 BAMBOO_DEBUGPRINT(0xe995);
2123 ObjectHashget(pw->objectset, (int) parameter, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
2124 ObjectHashremove(pw->objectset, (int)parameter);
2125 if (enterflags!=NULL)
2126 RUNFREE(enterflags);
2127 // release grabbed locks
2128 for(j = 0; j < locklen; ++j) {
2129 int * lock = (int *)(locks[j].redirectlock);
2130 releasewritelock(lock);
2132 RUNFREE(currtpd->parameterArray);
2135 #ifdef ACCURATEPROFILE
2136 // fail, set the end of the checkTaskInfo
2141 } // line 2878: if (!ismet)
2145 /* Check that object still has necessary tags */
2146 for(j=0; j<pd->numbertags; j++) {
2147 int slotid=pd->tagarray[2*j]+numparams;
2148 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
2149 if (!containstag(parameter, tagd)) {
2151 BAMBOO_DEBUGPRINT(0xe996);
2154 // release grabbed locks
2156 for(tmpj = 0; tmpj < locklen; ++tmpj) {
2157 int * lock = (int *)(locks[tmpj].redirectlock);
2158 releasewritelock(lock);
2161 RUNFREE(currtpd->parameterArray);
2164 } // line2911: if (!containstag(parameter, tagd))
2165 } // line 2808: for(j=0; j<pd->numbertags; j++)
2167 taskpointerarray[i+OFFSET]=parameter;
2168 } // line 2824: for(i=0; i<numparams; i++)
2170 for(; i<numtotal; i++) {
2171 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
2177 /* Checkpoint the state */
2178 forward=allocateRuntimeHash(100);
2179 reverse=allocateRuntimeHash(100);
2180 //void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
2182 #endif // #if 0: for recovery
2184 if (x=setjmp(error_handler)) {
2189 printf("Fatal Error=%d, Recovering!\n",x);
2193 genputtable(failedtasks,currtpd,currtpd);
2194 //restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
2196 freeRuntimeHash(forward);
2197 freeRuntimeHash(reverse);
2201 #endif // #if 0: for recovery
2202 BAMBOO_DEBUGPRINT_REG(x);
2203 BAMBOO_EXIT(0xa022);
2205 #endif // #ifndef MULTICORE
2207 if (injectfailures) {
2208 if ((((double)random())/RAND_MAX)<failurechance) {
2209 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
2210 longjmp(error_handler,10);
2213 #endif // #if 0: for recovery
2214 /* Actually call task */
2217 ((int *)taskpointerarray)[0]=currtpd->numParameters;
2218 taskpointerarray[1]=NULL;
2220 #endif // #if 0: for garbage collection
2223 #ifdef ACCURATEPROFILE
2224 // check finish, set the end of the checkTaskInfo
2227 profileTaskStart(currtpd->task->name);
2232 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
2234 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
2236 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
2240 BAMBOO_DEBUGPRINT(0xe997);
2242 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
2243 } // line 2990: if(debugtask)
2245 #ifdef ACCURATEPROFILE
2246 // task finish, set the end of the checkTaskInfo
2248 // new a PostTaskInfo for the post-task execution
2249 profileTaskStart("post task execution");
2253 BAMBOO_DEBUGPRINT(0xe998);
2254 BAMBOO_DEBUGPRINT_REG(islock);
2259 BAMBOO_DEBUGPRINT(0xe999);
2261 for(i = 0; i < locklen; ++i) {
2262 void * ptr = (void *)(locks[i].redirectlock);
2263 int * lock = (int *)(locks[i].value);
2265 BAMBOO_DEBUGPRINT_REG((int)ptr);
2266 BAMBOO_DEBUGPRINT_REG((int)lock);
2268 if(RuntimeHashcontainskey(lockRedirectTbl, (int)lock)) {
2270 RuntimeHashget(lockRedirectTbl, (int)lock, &redirectlock);
2271 RuntimeHashremovekey(lockRedirectTbl, (int)lock);
2272 releasewritelock_r(lock, (int *)redirectlock);
2274 releasewritelock(ptr);
2277 } // line 3015: if(islock)
2280 // post task execution finish, set the end of the postTaskInfo
2285 freeRuntimeHash(forward);
2286 freeRuntimeHash(reverse);
2289 // Free up task parameter descriptor
2290 RUNFREE(currtpd->parameterArray);
2297 BAMBOO_DEBUGPRINT(0xe99a);
2298 //BAMBOO_DEBUGPRINT_REG(hashsize(activetasks));
2301 } // line 2946: if (x=setjmp(error_handler))
2304 } // line 2697: if (hashsize(activetasks)>0)
2305 } // line 2659: while(hashsize(activetasks)>0)
2307 BAMBOO_DEBUGPRINT(0xe99b);
2311 /* This function processes an objects tags */
2312 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
2315 for(i=0; i<pd->numbertags; i++) {
2316 int slotid=pd->tagarray[2*i];
2317 int tagid=pd->tagarray[2*i+1];
2319 if (statusarray[slotid+numparams]==0) {
2320 parameter->iterators[*iteratorcount].istag=1;
2321 parameter->iterators[*iteratorcount].tagid=tagid;
2322 parameter->iterators[*iteratorcount].slot=slotid+numparams;
2323 parameter->iterators[*iteratorcount].tagobjectslot=index;
2324 statusarray[slotid+numparams]=1;
2331 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
2334 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
2336 parameter->iterators[*iteratorcount].istag=0;
2337 parameter->iterators[*iteratorcount].slot=index;
2338 parameter->iterators[*iteratorcount].objectset=objectset;
2339 statusarray[index]=1;
2341 for(i=0; i<pd->numbertags; i++) {
2342 int slotid=pd->tagarray[2*i];
2343 //int tagid=pd->tagarray[2*i+1];
2344 if (statusarray[slotid+numparams]!=0) {
2345 /* This tag has already been enqueued, use it to narrow search */
2346 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
2350 parameter->iterators[*iteratorcount].numtags=tagcount;
2355 /* This function builds the iterators for a task & parameter */
2357 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
2358 int statusarray[MAXTASKPARAMS];
2360 int numparams=task->numParameters;
2361 int iteratorcount=0;
2362 for(i=0; i<MAXTASKPARAMS; i++) statusarray[i]=0;
2364 statusarray[index]=1; /* Initial parameter */
2365 /* Process tags for initial iterator */
2367 processtags(task->descriptorarray[index], index, parameter, &iteratorcount, statusarray, numparams);
2371 /* Check for objects with existing tags */
2372 for(i=0; i<numparams; i++) {
2373 if (statusarray[i]==0) {
2374 struct parameterdescriptor *pd=task->descriptorarray[i];
2376 for(j=0; j<pd->numbertags; j++) {
2377 int slotid=pd->tagarray[2*j];
2378 if(statusarray[slotid+numparams]!=0) {
2379 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
2380 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2387 /* Next do objects w/ unbound tags*/
2389 for(i=0; i<numparams; i++) {
2390 if (statusarray[i]==0) {
2391 struct parameterdescriptor *pd=task->descriptorarray[i];
2392 if (pd->numbertags>0) {
2393 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
2394 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2400 /* Nothing with a tag enqueued */
2402 for(i=0; i<numparams; i++) {
2403 if (statusarray[i]==0) {
2404 struct parameterdescriptor *pd=task->descriptorarray[i];
2405 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
2406 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2419 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
2422 for(i=0; i<numtasks[BAMBOO_NUM_OF_CORE]; i++) {
2423 struct taskdescriptor * task=taskarray[BAMBOO_NUM_OF_CORE][i];
2425 printf("%s\n", task->name);
2427 for(j=0; j<task->numParameters; j++) {
2428 struct parameterdescriptor *param=task->descriptorarray[j];
2429 struct parameterwrapper *parameter=param->queue;
2430 struct ObjectHash * set=parameter->objectset;
2431 struct ObjectIterator objit;
2433 printf(" Parameter %d\n", j);
2435 ObjectHashiterator(set, &objit);
2436 while(ObjhasNext(&objit)) {
2437 struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
2438 struct ___Object___ * tagptr=obj->___tags___;
2439 int nonfailed=Objdata4(&objit);
2440 int numflags=Objdata3(&objit);
2441 int flags=Objdata2(&objit);
2444 printf(" Contains %lx\n", obj);
2445 printf(" flag=%d\n", obj->flag);
2448 } else if (tagptr->type==TAGTYPE) {
2450 printf(" tag=%lx\n",tagptr);
2456 struct ArrayObject *ao=(struct ArrayObject *)tagptr;
2457 for(; tagindex<ao->___cachedCode___; tagindex++) {
2459 printf(" tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
2471 /* This function processes the task information to create queues for
2472 each parameter type. */
2474 void processtasks() {
2476 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
2479 for(i=0; i<numtasks[BAMBOO_NUM_OF_CORE]; i++) {
2480 struct taskdescriptor * task=taskarray[BAMBOO_NUM_OF_CORE][i];
2483 /* Build objectsets */
2484 for(j=0; j<task->numParameters; j++) {
2485 struct parameterdescriptor *param=task->descriptorarray[j];
2486 struct parameterwrapper *parameter=param->queue;
2487 parameter->objectset=allocateObjectHash(10);
2488 parameter->task=task;
2491 /* Build iterators for parameters */
2492 for(j=0; j<task->numParameters; j++) {
2493 struct parameterdescriptor *param=task->descriptorarray[j];
2494 struct parameterwrapper *parameter=param->queue;
2495 builditerators(task, j, parameter);
2500 void toiReset(struct tagobjectiterator * it) {
2503 } else if (it->numtags>0) {
2506 ObjectHashiterator(it->objectset, &it->it);
2510 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
2513 /* Get object with tags */
2514 struct ___Object___ *obj=objectarray[it->tagobjectslot];
2515 struct ___Object___ *tagptr=obj->___tags___;
2516 if (tagptr->type==TAGTYPE) {
2517 if ((it->tagobjindex==0)&& /* First object */
2518 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
2523 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
2524 int tagindex=it->tagobjindex;
2525 for(; tagindex<ao->___cachedCode___; tagindex++) {
2526 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
2527 if (td->flag==it->tagid) {
2528 it->tagobjindex=tagindex; /* Found right type of tag */
2534 } else if (it->numtags>0) {
2535 /* Use tags to locate appropriate objects */
2536 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
2537 struct ___Object___ *objptr=tag->flagptr;
2539 if (objptr->type!=OBJECTARRAYTYPE) {
2540 if (it->tagobjindex>0)
2542 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
2544 for(i=1; i<it->numtags; i++) {
2545 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
2546 if (!containstag(objptr,tag2))
2551 struct ArrayObject *ao=(struct ArrayObject *) objptr;
2554 for(tagindex=it->tagobjindex; tagindex<ao->___cachedCode___; tagindex++) {
2555 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
2556 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
2558 for(i=1; i<it->numtags; i++) {
2559 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
2560 if (!containstag(objptr,tag2))
2563 it->tagobjindex=tagindex;
2568 it->tagobjindex=tagindex;
2572 return ObjhasNext(&it->it);
2576 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
2578 struct ___Object___ * objptr=tag->flagptr;
2579 if (objptr->type==OBJECTARRAYTYPE) {
2580 struct ArrayObject *ao=(struct ArrayObject *)objptr;
2581 for(j=0; j<ao->___cachedCode___; j++) {
2582 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
2590 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
2591 /* hasNext has all of the intelligence */
2594 /* Get object with tags */
2595 struct ___Object___ *obj=objectarray[it->tagobjectslot];
2596 struct ___Object___ *tagptr=obj->___tags___;
2597 if (tagptr->type==TAGTYPE) {
2599 objectarray[it->slot]=tagptr;
2601 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
2602 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
2604 } else if (it->numtags>0) {
2605 /* Use tags to locate appropriate objects */
2606 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
2607 struct ___Object___ *objptr=tag->flagptr;
2608 if (objptr->type!=OBJECTARRAYTYPE) {
2610 objectarray[it->slot]=objptr;
2612 struct ArrayObject *ao=(struct ArrayObject *) objptr;
2613 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
2616 /* Iterate object */
2617 objectarray[it->slot]=(void *)Objkey(&it->it);