00001 /* 00002 * taskGraph: ordered execution of repetitive computation 00003 */ 00004 00005 #include "taskGraph.h" 00006 #include "taskGraph.decl.h" 00007 00008 00009 CkArrayID taskGraphInit() { 00010 return CProxy_taskGraphArray::ckNew(); 00011 } 00012 00013 void taskGraphDelete(CkArrayID id, CkArrayIndex taskID) { 00014 CProxy_taskGraphArray array(id); 00015 array(taskID).deleteElement(); 00016 } 00017 00018 /* 00019 * Now define the taskGraphArray that actually handles doing all that work. 00020 */ 00021 taskGraphArray::taskGraphArray( 00022 CkVec<CkArrayIndex> deps, 00023 taskGraphSolver *data, 00024 CkCallback returnResults 00025 ) : Waiting() { 00026 // Set some state variables 00027 ReturnResults = returnResults; 00028 Self = data; 00029 isSolved = false; 00030 00031 // Save everything I need to know about 00032 DepsCount = deps.length(); 00033 DepsData = new taskGraphSolver*[DepsCount]; 00034 DepsReceived = 0; 00035 00036 // Ask everyone I depend on for their data 00037 CProxy_taskGraphArray neighbor(thisArrayID); 00038 for ( int i = 0 ; i < DepsCount ; i++ ) { 00039 neighbor(deps[i]).requestData(thisIndexMax); 00040 } 00041 00042 // If we're waiting on nothing we're solved 00043 tryToSolve(); 00044 } 00045 00046 00047 void taskGraphArray::tryToSolve() { 00048 if ( DepsCount == DepsReceived ) { 00049 if ( DepsCount != 0 ) { 00050 Self->solve(DepsCount, DepsData); 00051 } else { 00052 Self->setup(); 00053 } 00054 isSolved = true; 00055 00056 // Return that to whoever spawned me 00057 callbackMsg *res = new callbackMsg( Self, thisIndexMax ); 00058 ReturnResults.send( res ); 00059 00060 // And tell everyone who's waiting on me that I solved myself 00061 for ( int i = 0 ; i < Waiting.size() ; i++ ) { 00062 CProxy_taskGraphArray neighbor(thisArrayID); 00063 neighbor(Waiting[i]).depositData(Self); 00064 } 00065 } 00066 } 00067 00068 00069 void taskGraphArray::requestData(CkArrayIndex from) { 00070 // If the problem isn't solved, kick this request onto the waiting queue 00071 if ( ! isSolved ) { 00072 Waiting.insertAtEnd(from); 00073 return; 00074 } 00075 00076 // Otherwise, if the problem is solved send them the data we generated 00077 CProxy_taskGraphArray neighbor(thisArrayID); 00078 neighbor(from).depositData(Self); 00079 } 00080 00081 00082 void taskGraphArray::depositData(taskGraphSolver *data) { 00083 // Someone sent me data back. 00084 DepsData[DepsReceived++] = data; 00085 00086 // Now that we got that data try and solve the problem 00087 tryToSolve(); 00088 } 00089 00090 00091 void taskGraphArray::deleteElement() { 00092 // Warn of a possibly stupid action 00093 if ( Waiting.size() != 0 ) { 00094 ckerr << "Warning! taskGraphArray::delete called on an element that " 00095 << "currently has " << Waiting.length() << " other elements " 00096 << "waiting on it! Deleting anyway..." << endl; 00097 } 00098 00099 // Impelemnt deletion 00100 CProxy_taskGraphArray array(thisArrayID); 00101 // array(thisIndexMax).destroy(); 00102 } 00103 00104 #include "taskGraph.def.h"