1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 | using System.Net;
|
---|
28 | using System.ServiceModel;
|
---|
29 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
30 | using HeuristicLab.CEDMA.DB;
|
---|
31 | using System.ServiceModel.Description;
|
---|
32 | using System.Linq;
|
---|
33 | using HeuristicLab.CEDMA.Core;
|
---|
34 | using HeuristicLab.GP.StructureIdentification;
|
---|
35 | using HeuristicLab.Data;
|
---|
36 | using HeuristicLab.Grid;
|
---|
37 | using System.Diagnostics;
|
---|
38 | using HeuristicLab.Core;
|
---|
39 | using System.Threading;
|
---|
40 | using HeuristicLab.Hive.Engine;
|
---|
41 |
|
---|
42 | namespace HeuristicLab.CEDMA.Server {
|
---|
43 | public class HiveExecuter : ExecuterBase {
|
---|
44 | private Dictionary<WaitHandle, Execution> activeExecutions;
|
---|
45 | private Dictionary<WaitHandle, HiveEngine> activeEngines;
|
---|
46 | private string hiveUrl;
|
---|
47 |
|
---|
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 | public HiveExecuter(IDispatcher dispatcher, IStore store, string hiveUrl)
|
---|
57 | : base(dispatcher, store) {
|
---|
58 | this.hiveUrl = hiveUrl;
|
---|
59 | activeExecutions = new Dictionary<WaitHandle, Execution>();
|
---|
60 | activeEngines = new Dictionary<WaitHandle, HiveEngine>();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override void StartJobs() {
|
---|
64 | List<WaitHandle> wh = new List<WaitHandle>();
|
---|
65 | while (true) {
|
---|
66 | try {
|
---|
67 | // start new jobs as long as there are less than MaxActiveJobs
|
---|
68 | while (wh.Count < MaxActiveJobs) {
|
---|
69 | Thread.Sleep(StartJobInterval);
|
---|
70 | // get an execution from the dispatcher and execute in grid via job-manager
|
---|
71 | Execution execution = Dispatcher.GetNextJob();
|
---|
72 | if (execution != null) {
|
---|
73 | HiveEngine engine = new HiveEngine();
|
---|
74 | engine.HiveServerUrl = hiveUrl;
|
---|
75 | IOperator initialOp = execution.Engine.OperatorGraph.InitialOperator;
|
---|
76 | engine.OperatorGraph.AddOperator(initialOp);
|
---|
77 | engine.OperatorGraph.InitialOperator = initialOp;
|
---|
78 | RegisterFinishedCallback(engine, execution, wh);
|
---|
79 | engine.Reset();
|
---|
80 | engine.Execute();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | // wait until any job is finished
|
---|
84 | WaitHandle[] whArr = wh.ToArray();
|
---|
85 | int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout);
|
---|
86 | if (readyHandleIndex != WaitHandle.WaitTimeout) {
|
---|
87 | WaitHandle readyHandle = whArr[readyHandleIndex];
|
---|
88 | wh.Remove(readyHandle);
|
---|
89 | HiveEngine finishedEngine = activeEngines[readyHandle];
|
---|
90 | activeEngines.Remove(readyHandle);
|
---|
91 |
|
---|
92 | Execution finishedExecution = activeExecutions[readyHandle];
|
---|
93 | lock (activeExecutions) {
|
---|
94 | activeExecutions.Remove(readyHandle);
|
---|
95 | }
|
---|
96 | StoreResults(finishedExecution, finishedEngine);
|
---|
97 | readyHandle.Close();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | catch (Exception ex) {
|
---|
101 | Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void RegisterFinishedCallback(HiveEngine engine, Execution execution, List<WaitHandle> wh) {
|
---|
107 | ManualResetEvent waithandle = new ManualResetEvent(false);
|
---|
108 | wh.Add(waithandle);
|
---|
109 | engine.Finished += (sender, args) => waithandle.Set();
|
---|
110 | lock (activeExecutions) {
|
---|
111 | activeExecutions.Add(waithandle, execution);
|
---|
112 | }
|
---|
113 | activeEngines.Add(waithandle, engine);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override string[] GetJobs() {
|
---|
117 | lock (activeExecutions) {
|
---|
118 | string[] retVal = new string[activeExecutions.Count];
|
---|
119 | int i = 0;
|
---|
120 | foreach (Execution e in activeExecutions.Values) {
|
---|
121 | retVal[i++] = "Target-Variable: " + e.TargetVariable + " " + e.Description;
|
---|
122 | }
|
---|
123 | return retVal;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|