From 311302817bb617efe0f5d4165edff1b2d9aac3d1 Mon Sep 17 00:00:00 2001 From: jzhou Date: Fri, 8 Oct 2010 00:56:04 +0000 Subject: [PATCH] Fix alias locks. KMeans is now serialized but the execution is stucked somewhere. Need more investigation and bug fixing --- Robust/src/IR/Flat/BuildCodeMultiCore.java | 70 ++++++++-------- Robust/src/Runtime/bamboo/multicoretask.c | 96 ++++++++++++++++++---- 2 files changed, 116 insertions(+), 50 deletions(-) diff --git a/Robust/src/IR/Flat/BuildCodeMultiCore.java b/Robust/src/IR/Flat/BuildCodeMultiCore.java index e88737c2..4d97167e 100644 --- a/Robust/src/IR/Flat/BuildCodeMultiCore.java +++ b/Robust/src/IR/Flat/BuildCodeMultiCore.java @@ -1519,41 +1519,41 @@ public class BuildCodeMultiCore extends BuildCode { // associate locks with parameters int lockindex = 0; for(int i = 0; i < this.m_aliasSets.size(); i++) { - Vector toadd = this.m_aliasSets.elementAt(i); - - output.print("int tmplen_" + lockindex + " = 0;"); - output.println("void * tmpptrs_" + lockindex + "[] = {"); - for(int j = 0; j < toadd.size(); j++) { - int para = toadd.elementAt(j).intValue(); - output.print(super.generateTemp(fm, fm.getParameter(para), lb)); - if(j < toadd.size() - 1) { - output.print(", "); - } else { - output.println("};"); - } - } - output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", tmplen_" + lockindex + ", lockRedirectTbl);"); - - for(int j = 0; j < toadd.size(); j++) { - int para = toadd.elementAt(j).intValue(); - output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + i + "]);"); - } - // check if this lock is also associated with any FlatNew nodes - if(this.m_aliasFNTbl4Para.containsKey(toadd.elementAt(0))) { - if(this.m_aliaslocksTbl4FN == null) { - this.m_aliaslocksTbl4FN = new Hashtable>(); - } - Vector tmpv = this.m_aliasFNTbl4Para.get(toadd.elementAt(0)); - for(int j = 0; j < tmpv.size(); j++) { - FlatNew fn = tmpv.elementAt(j); - if(!this.m_aliaslocksTbl4FN.containsKey(fn)) { - this.m_aliaslocksTbl4FN.put(fn, new Vector()); - } - this.m_aliaslocksTbl4FN.get(fn).add(i); - } - this.m_aliasFNTbl4Para.remove(toadd.elementAt(0)); - } - lockindex++; + Vector toadd = this.m_aliasSets.elementAt(i); + + output.println("int tmplen_" + lockindex + " = " + toadd.size()); + output.println("void * tmpptrs_" + lockindex + "[] = {"); + for(int j = 0; j < toadd.size(); j++) { + int para = toadd.elementAt(j).intValue(); + output.print(super.generateTemp(fm, fm.getParameter(para), lb)); + if(j < toadd.size() - 1) { + output.print(", "); + } else { + output.println("};"); + } + } + output.println("aliaslocks[tmpi++] = getAliasLock(tmpptrs_" + lockindex + ", tmplen_" + lockindex + ", lockRedirectTbl);"); + + for(int j = 0; j < toadd.size(); j++) { + int para = toadd.elementAt(j).intValue(); + output.println("addAliasLock(" + super.generateTemp(fm, fm.getParameter(para), lb) + ", aliaslocks[" + i + "]);"); + } + // check if this lock is also associated with any FlatNew nodes + if(this.m_aliasFNTbl4Para.containsKey(toadd.elementAt(0))) { + if(this.m_aliaslocksTbl4FN == null) { + this.m_aliaslocksTbl4FN = new Hashtable>(); + } + Vector tmpv = this.m_aliasFNTbl4Para.get(toadd.elementAt(0)); + for(int j = 0; j < tmpv.size(); j++) { + FlatNew fn = tmpv.elementAt(j); + if(!this.m_aliaslocksTbl4FN.containsKey(fn)) { + this.m_aliaslocksTbl4FN.put(fn, new Vector()); + } + this.m_aliaslocksTbl4FN.get(fn).add(i); + } + this.m_aliasFNTbl4Para.remove(toadd.elementAt(0)); + } + lockindex++; } Object[] key = this.m_aliasFNTbl4Para.keySet().toArray(); diff --git a/Robust/src/Runtime/bamboo/multicoretask.c b/Robust/src/Runtime/bamboo/multicoretask.c index 495e2f10..02c0472c 100644 --- a/Robust/src/Runtime/bamboo/multicoretask.c +++ b/Robust/src/Runtime/bamboo/multicoretask.c @@ -467,7 +467,11 @@ bool checkObjQueue() { BAMBOO_DEBUGPRINT_REG((int)obj); // grab lock and flush the obj grount = 0; - getwritelock_I(obj); + struct ___Object___ * tmpobj = (struct ___Object___ *)obj; + while(tmpobj->lock != NULL) { + tmpobj = (struct ___Object___ *)(tmpobj->lock); + } + getwritelock_I(tmpobj); while(!lockflag) { BAMBOO_WAITING_FOR_LOCK(0); } // while(!lockflag) @@ -501,7 +505,7 @@ bool checkObjQueue() { enqueueObject_I(obj, queues, 1); BAMBOO_DEBUGPRINT_REG(hashsize(activetasks)); } // for(k = 0; k < objInfo->length; ++k) - releasewritelock_I(obj); + releasewritelock_I(tmpobj); RUNFREE(objInfo->queues); RUNFREE(objInfo); } else { @@ -1314,6 +1318,42 @@ nextloop: int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl) { +#ifdef TILERA_BME + int i = 0; + int locks[length]; + int locklen = 0; + // sort all the locks required by the objs in the aliased set + for(; i < length; i++) { + struct ___Object___ * ptr = (struct ___Object___ *)(ptrs[i]); + int lock = 0; + int j = 0; + if(ptr->lock == NULL) { + lock = (int)(ptr); + } else { + lock = (int)(ptr->lock); + } + bool insert = true; + for(j = 0; j < locklen; j++) { + if(locks[j] == lock) { + insert = false; + break; + } else if(locks[j] > lock) { + break; + } + } + if(insert) { + int h = locklen; + for(; h > j; h--) { + locks[h] = locks[h-1]; + } + locks[j] = lock; + locklen++; + } + } + // use the smallest lock as the shared lock for the whole set + return (int *)(locks[0]); +#else // TILERA_BME + // TODO possible bug here!!! if(length == 0) { return (int*)(RUNMALLOC(sizeof(int))); } else { @@ -1369,9 +1409,16 @@ int * getAliasLock(void ** ptrs, if(redirect) { return (int *)redirectlock; } else { + // use the first lock as the shared lock + for(j = 1; j < locklen; j++) { + if(locks[j] != locks[0]) { + RuntimeHashadd(tbl, locks[j], locks[0]); + } + } return (int *)(locks[0]); } } +#endif // TILERA_BME } void addAliasLock(void * ptr, @@ -1380,6 +1427,12 @@ void addAliasLock(void * ptr, if(((int)ptr != lock) && (obj->lock != (int*)lock)) { // originally no alias lock associated or have a different alias lock // flush it as the new one +#ifdef TILERA_BME + while(obj->lock != NULL) { + // previously have alias lock, trace the 'root' obj and redirect it + obj = (struct ___Object___ *)(obj->lock); + } +#endif // TILERA_BME obj->lock = (int *)lock; } } @@ -3515,11 +3568,20 @@ newtask: taskpointerarray[i+OFFSET]=param; goto execute; } - if(((struct ___Object___ *)param)->lock == NULL) { + /*if(((struct ___Object___ *)param)->lock == NULL) { tmplock = (int)param; } else { - tmplock = (int)(((struct ___Object___ *)param)->lock); - } + struct ___Object___ * obj = (struct ___Object___ *)param; + while(obj->lock != NULL) { + obj = (struct ___Object___ *)(obj->lock); + } + tmplock = (int)(obj); + }*/ + struct ___Object___ * obj = (struct ___Object___ *)param; + while(obj->lock != NULL) { + obj = (struct ___Object___ *)(obj->lock); + } + tmplock = (int)(obj); // insert into the locks array for(j = 0; j < runtime_locklen; j++) { if(runtime_locks[j].value == tmplock) { @@ -3544,7 +3606,7 @@ newtask: BAMBOO_DEBUGPRINT(0xe991); for(i = 0; i < runtime_locklen; i++) { - int * lock = (int *)(runtime_locks[i].redirectlock); + int * lock = (int *)(runtime_locks[i].value);//(runtime_locks[i].redirectlock); islock = true; // require locks for this parameter if it is not a startup object BAMBOO_DEBUGPRINT_REG((int)lock); @@ -3580,7 +3642,7 @@ newtask: // can not get the lock, try later // release all grabbed locks for previous parameters for(j = 0; j < i; ++j) { - lock = (int*)(runtime_locks[j].redirectlock); + lock = (int*)(runtime_locks[j].value/*redirectlock*/); releasewritelock(lock); } genputtable(activetasks, currtpd, currtpd); @@ -3620,7 +3682,7 @@ newtask: BAMBOO_DEBUGPRINT_REG(parameter); // release grabbed locks for(j = 0; j < runtime_locklen; ++j) { - int * lock = (int *)(runtime_locks[j].redirectlock); + int * lock = (int *)(runtime_locks[j].value/*redirectlock*/); releasewritelock(lock); } RUNFREE(currtpd->parameterArray); @@ -3656,7 +3718,7 @@ newtask: RUNFREE(enterflags); // release grabbed locks for(j = 0; j < runtime_locklen; ++j) { - int * lock = (int *)(runtime_locks[j].redirectlock); + int * lock = (int *)(runtime_locks[j].value/*redirectlock*/); releasewritelock(lock); } RUNFREE(currtpd->parameterArray); @@ -3683,7 +3745,7 @@ parameterpresent: // release grabbed locks int tmpj = 0; for(tmpj = 0; tmpj < runtime_locklen; ++tmpj) { - int * lock = (int *)(runtime_locks[tmpj].redirectlock); + int * lock = (int *)(runtime_locks[tmpj].value/*redirectlock*/); releasewritelock(lock); } } @@ -3732,23 +3794,27 @@ execute: if(islock) { BAMBOO_DEBUGPRINT(0xe999); - for(i = 0; i < runtime_locklen; ++i) { - void * ptr = (void *)(runtime_locks[i].redirectlock); - int * lock = (int *)(runtime_locks[i].value); + for(i = runtime_locklen; i>0; i--) { + void * ptr = (void *)(runtime_locks[i-1].redirectlock); + int * lock = (int *)(runtime_locks[i-1].value); BAMBOO_DEBUGPRINT_REG((int)ptr); BAMBOO_DEBUGPRINT_REG((int)lock); BAMBOO_DEBUGPRINT_REG(*((int*)lock+5)); #ifndef MULTICORE_GC +#ifndef TILERA_BME if(RuntimeHashcontainskey(lockRedirectTbl, (int)lock)) { int redirectlock; RuntimeHashget(lockRedirectTbl, (int)lock, &redirectlock); RuntimeHashremovekey(lockRedirectTbl, (int)lock); releasewritelock_r(lock, (int *)redirectlock); - } else { + } else -1{ +#else + { +#endif #else { #endif - releasewritelock(ptr); + releasewritelock(lock); // ptr } } } // line 3015: if(islock) -- 2.34.1