Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/HiveExecuter.cs @ 2053

Last change on this file since 2053 was 2012, checked in by gkronber, 15 years ago

Fixed a few bugs introduced with r2000. #656 (CEDMA server should handle only one data set (problem) at a time)

File size: 4.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using System.ServiceModel;
29using HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
36using HeuristicLab.Grid;
37using System.Diagnostics;
38using HeuristicLab.Core;
39using System.Threading;
40using HeuristicLab.Hive.Engine;
41using HeuristicLab.Modeling;
42
43namespace HeuristicLab.CEDMA.Server {
44  public class HiveExecuter : ExecuterBase {
45    private Dictionary<WaitHandle, IAlgorithm> activeAlgorithms;
46    private Dictionary<WaitHandle, HiveEngine> activeEngines;
47    private string hiveUrl;
48
49    private TimeSpan StartJobInterval {
50      get { return TimeSpan.FromMilliseconds(500); }
51    }
52
53    private TimeSpan WaitForFinishedJobsTimeout {
54      get { return TimeSpan.FromMilliseconds(100); }
55    }
56
57    public HiveExecuter(IDispatcher dispatcher, IStore store, string hiveUrl)
58      : base(dispatcher, store) {
59      this.hiveUrl = hiveUrl;
60      activeAlgorithms = new Dictionary<WaitHandle, IAlgorithm>();
61      activeEngines = new Dictionary<WaitHandle, HiveEngine>();
62    }
63
64    protected override void StartJobs() {
65      List<WaitHandle> wh = new List<WaitHandle>();
66      while (true) {
67        try {
68          // start new jobs as long as there are less than MaxActiveJobs
69          while (wh.Count < MaxActiveJobs) {
70            Thread.Sleep(StartJobInterval);
71            // get an algo from the dispatcher and execute in grid via job-manager
72            IAlgorithm algorithm = Dispatcher.GetNextJob();
73            if (algorithm != null) {
74              HiveEngine engine = new HiveEngine();
75              engine.HiveServerUrl = hiveUrl;
76              IOperator initialOp = algorithm.Engine.OperatorGraph.InitialOperator;
77              engine.OperatorGraph.AddOperator(initialOp);
78              engine.OperatorGraph.InitialOperator = initialOp;
79              RegisterFinishedCallback(engine, algorithm, wh);
80              engine.Reset();
81              engine.Execute();
82            }
83          }
84          // wait until any job is finished
85          WaitHandle[] whArr = wh.ToArray();
86          int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout);
87          if (readyHandleIndex != WaitHandle.WaitTimeout) {
88            WaitHandle readyHandle = whArr[readyHandleIndex];
89            wh.Remove(readyHandle);
90            HiveEngine finishedEngine = activeEngines[readyHandle];
91            activeEngines.Remove(readyHandle);
92
93            IAlgorithm finishedAlgorithm = activeAlgorithms[readyHandle];
94            lock (activeAlgorithms) {
95              activeAlgorithms.Remove(readyHandle);
96            }
97            StoreResults(finishedAlgorithm);
98            readyHandle.Close();
99          }
100        }
101        catch (Exception ex) {
102          Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
103        }
104      }
105    }
106
107    private void RegisterFinishedCallback(HiveEngine engine, IAlgorithm algorithm, List<WaitHandle> wh) {
108      ManualResetEvent waithandle = new ManualResetEvent(false);
109      wh.Add(waithandle);
110      engine.Finished += (sender, args) => waithandle.Set();
111      lock (activeAlgorithms) {
112        activeAlgorithms.Add(waithandle, algorithm);
113      }
114      activeEngines.Add(waithandle, engine);
115    }
116
117    public override string[] GetJobs() {
118      lock (activeAlgorithms) {
119        string[] retVal = new string[activeAlgorithms.Count];
120        int i = 0;
121        foreach (IAlgorithm a in activeAlgorithms.Values) {
122          retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
123        }
124        return retVal;
125      }
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.