Changeset 1287 for trunk/sources/HeuristicLab.CEDMA.Server/Executer.cs
- Timestamp:
- 03/08/09 12:48:18 (15 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.Server/Executer.cs
r1060 r1287 41 41 namespace HeuristicLab.CEDMA.Server { 42 42 public class Executer { 43 private static int MaxActiveJobs { 44 get { return 20; } 45 } 46 private Dispatcher dispatcher; 43 private IDispatcher dispatcher; 47 44 private JobManager jobManager; 48 45 private IStore store; 46 private Dictionary<WaitHandle, Execution> activeExecutions; 49 47 50 public Executer(Dispatcher dispatcher, IStore store, string gridUrl) { 48 private TimeSpan StartJobInterval { 49 get { return TimeSpan.FromMilliseconds(500); } 50 } 51 52 private TimeSpan WaitForFinishedJobsTimeout { 53 get { return TimeSpan.FromMilliseconds(100); } 54 } 55 56 private int maxActiveJobs; 57 public int MaxActiveJobs { 58 get { return maxActiveJobs; } 59 set { 60 if (value < 0) throw new ArgumentException("Only positive values are allowed for MaxActiveJobs"); 61 maxActiveJobs = value; 62 } 63 } 64 65 public Executer(IDispatcher dispatcher, IStore store, string gridUrl) { 66 activeExecutions = new Dictionary<WaitHandle, Execution>(); 67 maxActiveJobs = 10; 51 68 this.dispatcher = dispatcher; 52 69 this.store = store; … … 61 78 private void StartJobs() { 62 79 List<WaitHandle> wh = new List<WaitHandle>(); 63 Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle,AtomicOperation>(); 64 Dictionary<WaitHandle, Execution> activeExecutions = new Dictionary<WaitHandle,Execution>(); 80 Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle, AtomicOperation>(); 65 81 while (true) { 66 82 try { 67 83 // start new jobs as long as there are less than MaxActiveJobs 68 84 while (wh.Count < MaxActiveJobs) { 85 Thread.Sleep(StartJobInterval); 69 86 // get an execution from the dispatcher and execute in grid via job-manager 70 87 Execution execution = dispatcher.GetNextJob(); 71 AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope); 72 WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op); 73 wh.Add(opWh); 74 activeOperations.Add(opWh, op); 88 if (execution != null) { 89 AtomicOperation op = new AtomicOperation(execution.Engine.OperatorGraph.InitialOperator, execution.Engine.GlobalScope); 90 WaitHandle opWh = jobManager.BeginExecuteOperation(execution.Engine.GlobalScope, op); 91 wh.Add(opWh); 92 activeOperations.Add(opWh, op); 93 lock (activeExecutions) { 94 activeExecutions.Add(opWh, execution); 95 } 96 } 75 97 } 76 98 // wait until any job is finished 77 99 WaitHandle[] whArr = wh.ToArray(); 78 int readyHandleIndex = WaitHandle.WaitAny(whArr); 79 WaitHandle readyHandle = whArr[readyHandleIndex]; 80 AtomicOperation finishedOp = activeOperations[readyHandle]; 81 Execution finishedExecution = activeExecutions[readyHandle]; 82 wh.Remove(readyHandle); 83 activeExecutions.Remove(readyHandle); 84 activeOperations.Remove(readyHandle); 85 ProcessingEngine finishedEngine = null; 86 try { 87 finishedEngine = jobManager.EndExecuteOperation(finishedOp); 88 } catch (Exception badEx) { 89 Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message); 90 } 91 if (finishedEngine != null) { 92 Entity modelEntity = new Entity(Ontology.CedmaNameSpace+Guid.NewGuid()); 93 store.Add(new Statement(modelEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree)); 94 Entity targetVariableAttr = new Entity(Ontology.CedmaNameSpace+Guid.NewGuid()); 95 store.Add(new Statement(modelEntity, Ontology.PredicateModelAttribute, targetVariableAttr)); 96 store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeName, new Literal("TargetVariable"))); 97 store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeValue, new Literal(finishedExecution.TargetVariable))); 98 store.Add(new Statement(targetVariableAttr, Ontology.PredicateModelAttributeType, Ontology.TypeCategoricalAttribute)); 99 store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, modelEntity)); 100 int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout); 101 if (readyHandleIndex != WaitHandle.WaitTimeout) { 102 WaitHandle readyHandle = whArr[readyHandleIndex]; 103 AtomicOperation finishedOp = activeOperations[readyHandle]; 104 wh.Remove(readyHandle); 105 Execution finishedExecution = null; 106 lock (activeExecutions) { 107 finishedExecution = activeExecutions[readyHandle]; 108 activeExecutions.Remove(readyHandle); 109 } 110 activeOperations.Remove(readyHandle); 111 ProcessingEngine finishedEngine = null; 112 try { 113 finishedEngine = jobManager.EndExecuteOperation(finishedOp); 114 } 115 catch (Exception badEx) { 116 Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message); 117 } 118 if (finishedEngine != null) { 119 StoreResults(finishedExecution, finishedEngine); 120 } 100 121 } 101 122 } … … 105 126 } 106 127 } 128 129 private void StoreResults(Execution finishedExecution, ProcessingEngine finishedEngine) { 130 Entity model = new Entity(Ontology.CedmaNameSpace + Guid.NewGuid()); 131 store.Add(new Statement(model, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree)); 132 store.Add(new Statement(finishedExecution.DataSetEntity, Ontology.PredicateHasModel, model)); 133 StoreModelAttribute(model, Ontology.TargetVariable, finishedExecution.TargetVariable); 134 Scope bestModelScope = finishedEngine.GlobalScope.GetVariableValue<Scope>("BestValidationSolution", false); 135 StoreModelVariable(model, Ontology.TrainingMeanSquaredError, bestModelScope, "Quality"); 136 StoreModelVariable(model, Ontology.ValidationMeanSquaredError, bestModelScope, "ValidationQuality"); 137 StoreModelVariable(model, Ontology.TestMeanSquaredError, bestModelScope, "TestQuality"); 138 StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageError, bestModelScope, "TrainingMAPE"); 139 StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageError, bestModelScope, "ValidationMAPE"); 140 StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageError, bestModelScope, "TestMAPE"); 141 StoreModelVariable(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError, bestModelScope, "TrainingMAPRE"); 142 StoreModelVariable(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError, bestModelScope, "ValidationMAPRE"); 143 StoreModelVariable(model, Ontology.TestMeanAbsolutePercentageOfRangeError, bestModelScope, "TestMAPRE"); 144 StoreModelVariable(model, Ontology.TrainingCoefficientOfDetermination, bestModelScope, "TrainingR2"); 145 StoreModelVariable(model, Ontology.ValidationCoefficientOfDetermination, bestModelScope, "ValidationR2"); 146 StoreModelVariable(model, Ontology.TestCoefficientOfDetermination, bestModelScope, "TestR2"); 147 StoreModelVariable(model, Ontology.TrainingTheilsInequalityCoefficient, bestModelScope, "TrainingTheilInequalityCoefficient"); 148 StoreModelVariable(model, Ontology.ValidationTheilsInequalityCoefficient, bestModelScope, "ValidationTheilInequalityCoefficient"); 149 StoreModelVariable(model, Ontology.TestTheilsInequalityCoefficient, bestModelScope, "TestTheilInequalityCoefficient"); 150 StoreModelVariable(model, Ontology.TrainingAccuracy, bestModelScope, "TrainingAccuracy"); 151 StoreModelVariable(model, Ontology.ValidationAccuracy, bestModelScope, "ValidationAccuracy"); 152 StoreModelVariable(model, Ontology.TestAccuracy, bestModelScope, "TestAccuracy"); 153 StoreModelVariable(model, Ontology.TreeSize, bestModelScope, "TreeSize"); 154 StoreModelVariable(model, Ontology.TreeHeight, bestModelScope, "TreeHeight"); 155 StoreModelVariable(model, Ontology.EvaluatedSolutions, bestModelScope, "EvaluatedSolutions"); 156 157 byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false)); 158 store.Add(new Statement(model, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel)))); 159 } 160 161 private void StoreModelVariable(Entity model, Entity entity, Scope scope, string variableName) { 162 if (scope.GetVariable(variableName) != null) 163 StoreModelAttribute(model, entity, scope.GetVariableValue<ObjectData>(variableName, false).Data); 164 } 165 166 private void StoreModelAttribute(Entity model, Entity predicate, object value) { 167 store.Add(new Statement(model, predicate, new Literal(value))); 168 } 169 170 internal string[] GetJobs() { 171 lock (activeExecutions) { 172 string[] retVal = new string[activeExecutions.Count]; 173 int i = 0; 174 foreach (Execution e in activeExecutions.Values) { 175 retVal[i++] = "Target-Variable: " + e.TargetVariable + " " + e.Description; 176 } 177 return retVal; 178 } 179 } 107 180 } 108 181 }
Note: See TracChangeset
for help on using the changeset viewer.