Changeset 5153 for branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.HiveEngine/3.3
- Timestamp:
- 12/21/10 18:16:24 (14 years ago)
- Location:
- branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.HiveEngine/3.3
- Files:
-
- 5 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.HiveEngine/3.3/HeuristicLab.HiveEngine-3.3.csproj
r5136 r5153 32 32 <WarningLevel>4</WarningLevel> 33 33 </PropertyGroup> 34 <PropertyGroup> 35 <SignAssembly>true</SignAssembly> 36 </PropertyGroup> 37 <PropertyGroup> 38 <AssemblyOriginatorKeyFile>HeuristicLab.snk</AssemblyOriginatorKeyFile> 39 </PropertyGroup> 34 40 <ItemGroup> 35 41 <Reference Include="HeuristicLab.Clients.Common-3.3"> … … 48 54 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Core-3.3.dll</HintPath> 49 55 </Reference> 56 <Reference Include="HeuristicLab.Core.Views-3.3"> 57 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Core.Views-3.3.dll</HintPath> 58 </Reference> 59 <Reference Include="HeuristicLab.MainForm-3.3"> 60 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.MainForm-3.3.dll</HintPath> 61 </Reference> 62 <Reference Include="HeuristicLab.MainForm.WindowsForms-3.3"> 63 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.MainForm.WindowsForms-3.3.dll</HintPath> 64 </Reference> 65 <Reference Include="HeuristicLab.Optimization.Views-3.3"> 66 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Optimization.Views-3.3.dll</HintPath> 67 </Reference> 50 68 <Reference Include="HeuristicLab.Persistence-3.3"> 51 69 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Persistence-3.3.dll</HintPath> … … 53 71 <Reference Include="HeuristicLab.PluginInfrastructure-3.3"> 54 72 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 73 </Reference> 74 <Reference Include="HeuristicLab.Random-3.3"> 75 <HintPath>..\..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Random-3.3.dll</HintPath> 55 76 </Reference> 56 77 <Reference Include="HeuristicLab.SequentialEngine-3.3"> … … 60 81 <Reference Include="System.Core" /> 61 82 <Reference Include="System.Drawing" /> 83 <Reference Include="System.Windows.Forms" /> 62 84 <Reference Include="System.Xml.Linq" /> 63 85 <Reference Include="System.Data.DataSetExtensions" /> … … 67 89 <ItemGroup> 68 90 <Compile Include="ScopeMergeException.cs" /> 91 <Compile Include="Views\HiveEngineView.cs"> 92 <SubType>UserControl</SubType> 93 </Compile> 94 <Compile Include="Views\HiveEngineView.Designer.cs"> 95 <DependentUpon>HiveEngineView.cs</DependentUpon> 96 </Compile> 97 <None Include="HeuristicLab.snk" /> 69 98 <None Include="Properties\AssemblyInfo.cs.frame" /> 70 99 <None Include="HeuristicLabHiveEnginePlugin.cs.frame" /> … … 92 121 </ProjectReference> 93 122 </ItemGroup> 123 <ItemGroup> 124 <EmbeddedResource Include="Views\HiveEngineView.resx"> 125 <DependentUpon>HiveEngineView.cs</DependentUpon> 126 </EmbeddedResource> 127 </ItemGroup> 94 128 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 95 129 <PropertyGroup> -
branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.HiveEngine/3.3/HiveEngine.cs
r5136 r5153 13 13 using HeuristicLab.Hive.Contracts.ResponseObjects; 14 14 using System.Threading; 15 using HeuristicLab.Random; 15 16 16 17 namespace HeuristicLab.HiveEngine { … … 19 20 /// </summary> 20 21 [StorableClass] 21 [Item("Hive Parallel Engine", "Engine for parallel execution on the hive.")]22 [Item("Hive Engine", "Engine for parallel execution on the hive. You need enable `Parallel` for at least one operator in your operator graph to have all childoperations parallelized. Also those childoperations must not have sideeffects on a higher scope.")] 22 23 public class HiveEngine : Engine { 24 [Storable] 23 25 private IOperator currentOperator; 24 26 27 [Storable] 28 public string ResourceIds { get; set; } 29 25 30 #region constructors and cloning 26 public HiveEngine() { } 31 public HiveEngine() { 32 ResourceIds = "HEAL"; 33 } 27 34 [StorableConstructor] 28 35 protected HiveEngine(bool deserializing) : base(deserializing) { } 29 36 protected HiveEngine(HiveEngine original, Cloner cloner) 30 37 : base(original, cloner) { 38 this.ResourceIds = original.ResourceIds; 39 this.currentOperator = cloner.Clone(original.currentOperator); 31 40 } 32 41 public override IDeepCloneable Clone(Cloner cloner) { … … 42 51 while (coll != null) { 43 52 if (coll.Parallel) { 44 IScope scope = ((IAtomicOperation)coll.Where(x => x is IAtomicOperation).First()).Scope; 45 IEnumerable<IOperation> finishedOperations = ExecuteOnHive(coll.Select(op => new OperationJob(op))); 46 foreach (IOperation op in finishedOperations) { 47 ReIntegrateScope(scope, op); 53 IDictionary<IOperation, OperationJob> jobs = new Dictionary<IOperation, OperationJob>(); 54 foreach (IOperation op in coll) { 55 jobs.Add(op, new OperationJob(op)); 56 } 57 58 ExecuteOnHive(jobs); 59 60 foreach (var kvp in jobs) { 61 if (kvp.Key is IAtomicOperation) { 62 ReIntegrateScope(kvp.Value.Operation as IAtomicOperation, kvp.Key as IAtomicOperation); 63 } else if (kvp.Key is OperationCollection) { 64 // todo 65 } 48 66 } 49 67 } else { … … 73 91 } 74 92 75 private static void ReIntegrateScope(IScope scope, IOperation operation) { 76 if (operation is IAtomicOperation) { 77 MergeScopes(scope, ((IAtomicOperation)operation).Scope); 78 } else if (operation is OperationCollection) { 79 foreach (IOperation op in (OperationCollection)operation) { 80 ReIntegrateScope(scope, op); 93 private IRandom FindRandomParameter(IExecutionContext ec) { 94 try { 95 if (ec == null) 96 return null; 97 98 foreach (var p in ec.Parameters) { 99 if (p.Name == "Random" && p is IValueParameter) 100 return ((IValueParameter)p).Value as IRandom; 81 101 } 102 return FindRandomParameter(ec.Parent); 82 103 } 104 catch { return null; } 83 105 } 84 106 85 private static void MergeScopes(IScope target, IScope source) { 86 foreach (IVariable variable in source.Variables) { 87 if (target.Variables.ContainsKey(variable.Name)) { 88 if (target.Variables[variable.Name] != source.Variables[variable.Name]) { 89 // problem: ResultCollection are not equal altough they contain the same values. 90 // - either implement ResultCollection.Equals, or consider ResultCollection as special case here 91 //throw new ScopeMergeException(string.Format("Variable {0} already exists in target scope and has a different value ({1}, {2}).", variable.Name, target.Variables[variable.Name].Value, variable.Value)); 92 } 93 } else { 94 target.Variables.Add(variable); 95 } 96 } 97 98 //this is not useful on the parentscope of the initial scope 99 //foreach (IScope subScope in source.SubScopes) { 100 // target.SubScopes.Add(subScope); 101 //} 102 103 if (target.Parent != null && source.Parent != null) 104 MergeScopes(target.Parent, source.Parent); 107 private static void ReIntegrateScope(IAtomicOperation source, IAtomicOperation target) { 108 ExchangeScope(source.Scope, target.Scope); 105 109 } 106 110 107 108 private IEnumerable<IOperation> TestExecuteOnHive(IEnumerable<OperationJob> operationJobs) { 109 //var clonedJobs = operationJobs.Select(x => (OperationJob)x.Clone()).ToArray(); 110 var results = new List<IOperation>(); 111 112 foreach (var job in operationJobs) { 113 job.Start(); 114 while (job.ExecutionState != Core.ExecutionState.Stopped) { 115 Thread.Sleep(100); 116 } 117 results.Add(job.Operation); 118 } 119 return results; 111 private static void ExchangeScope(IScope source, IScope target) { 112 target.Variables.Clear(); 113 target.Variables.AddRange(source.Variables); 114 target.SubScopes.Clear(); 115 target.SubScopes.AddRange(source.SubScopes); 116 // TODO: validate if parent scopes match - otherwise source is invalid 120 117 } 121 118 122 private IEnumerable<IOperation> ExecuteOnHive(IEnumerable<OperationJob> operationJobs) { 123 IEnumerable<Guid> jobIds; 119 /// <summary> 120 /// This method blocks until all jobs are finished 121 /// </summary> 122 /// <param name="jobDict"></param> 123 private void ExecuteOnHive(IDictionary<IOperation, OperationJob> jobDict) { 124 Log.LogMessage(string.Format("Executing {0} operations on the hive.", jobDict.Count)); 125 IDictionary<Guid, IOperation> jobIds = new Dictionary<Guid, IOperation>(); 124 126 JobResultList results; 125 126 127 127 128 using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) { 128 129 List<JobDto> jobs = new List<JobDto>(); 129 foreach (OperationJob operationJob in operationJobs) { 130 var groups = new string[] { "HEAL" }; 130 foreach (var kvp in jobDict) { 131 // shuffle random variable to avoid the same random sequence in each operation 132 IRandom random = FindRandomParameter(kvp.Key as IExecutionContext); 133 if (random != null) 134 random.Reset(random.Next()); 135 136 var groups = ResourceIds.Split(';'); 131 137 SerializedJob serializedJob = new SerializedJob(); 132 serializedJob.SerializedJobData = SerializedJob.Serialize( operationJob);138 serializedJob.SerializedJobData = SerializedJob.Serialize(kvp.Value); 133 139 serializedJob.JobInfo = new JobDto(); 134 140 serializedJob.JobInfo.State = JobState.Offline; … … 137 143 ResponseObject<JobDto> response = service.Obj.AddJobWithGroupStrings(serializedJob, groups); 138 144 jobs.Add(response.Obj); 145 jobIds.Add(response.Obj.Id, kvp.Key); 139 146 } 140 jobIds = jobs.Select(x => x.Id); 141 results = service.Obj.GetJobResults(jobIds).Obj; 147 results = service.Obj.GetJobResults(jobIds.Keys).Obj; 142 148 } 143 149 … … 148 154 Thread.Sleep(5000); 149 155 using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) { 150 results = service.Obj.GetJobResults(jobIds ).Obj;156 results = service.Obj.GetJobResults(jobIds.Keys).Obj; 151 157 } 152 158 } 153 159 154 160 // all finished 155 List<OperationJob> finishedJobs = new List<OperationJob>();156 161 using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) { 157 foreach (Guid jobId in jobIds ) {162 foreach (Guid jobId in jobIds.Keys) { 158 163 SerializedJob serializedJob = service.Obj.GetLastSerializedResult(jobId).Obj; 159 164 OperationJob operationJob = SerializedJob.Deserialize<OperationJob>(serializedJob.SerializedJobData); 160 finishedJobs.Add(operationJob);165 jobDict[jobIds[jobId]] = operationJob; 161 166 } 162 167 } 163 168 164 169 // delete jobs 165 // TODO 170 using (Disposable<IClientFacade> service = ServiceLocator.Instance.StreamedClientFacadePool.GetService()) { 171 foreach (Guid jobId in jobIds.Keys) { 172 service.Obj.DeleteJob(jobId); 173 } 174 } 166 175 167 return finishedJobs.Select(x => x.Operation);176 Log.LogMessage(string.Format("Operations on the hive finished.", jobDict.Count)); 168 177 } 169 178 … … 176 185 if (currentOperator != null) currentOperator.Abort(); 177 186 } 187 178 188 } 179 189 }
Note: See TracChangeset
for help on using the changeset viewer.