[1898] | 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;
|
---|
[1922] | 41 | using HeuristicLab.Modeling;
|
---|
[1898] | 42 |
|
---|
| 43 | namespace HeuristicLab.CEDMA.Server {
|
---|
| 44 | public class HiveExecuter : ExecuterBase {
|
---|
[1922] | 45 | private Dictionary<WaitHandle, IAlgorithm> activeAlgorithms;
|
---|
[1898] | 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;
|
---|
[1922] | 60 | activeAlgorithms = new Dictionary<WaitHandle, IAlgorithm>();
|
---|
[1898] | 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);
|
---|
[1922] | 71 | // get an algo from the dispatcher and execute in grid via job-manager
|
---|
| 72 | IAlgorithm algorithm = Dispatcher.GetNextJob();
|
---|
| 73 | if (algorithm != null) {
|
---|
[1898] | 74 | HiveEngine engine = new HiveEngine();
|
---|
| 75 | engine.HiveServerUrl = hiveUrl;
|
---|
[1922] | 76 | IOperator initialOp = algorithm.Engine.OperatorGraph.InitialOperator;
|
---|
[1898] | 77 | engine.OperatorGraph.AddOperator(initialOp);
|
---|
| 78 | engine.OperatorGraph.InitialOperator = initialOp;
|
---|
[1922] | 79 | RegisterFinishedCallback(engine, algorithm, wh);
|
---|
[1898] | 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 |
|
---|
[1922] | 93 | IAlgorithm finishedAlgorithm = activeAlgorithms[readyHandle];
|
---|
| 94 | lock (activeAlgorithms) {
|
---|
| 95 | activeAlgorithms.Remove(readyHandle);
|
---|
[1898] | 96 | }
|
---|
[1922] | 97 | StoreResults(finishedAlgorithm);
|
---|
[1898] | 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 |
|
---|
[1922] | 107 | private void RegisterFinishedCallback(HiveEngine engine, IAlgorithm algorithm, List<WaitHandle> wh) {
|
---|
[1898] | 108 | ManualResetEvent waithandle = new ManualResetEvent(false);
|
---|
| 109 | wh.Add(waithandle);
|
---|
| 110 | engine.Finished += (sender, args) => waithandle.Set();
|
---|
[1922] | 111 | lock (activeAlgorithms) {
|
---|
| 112 | activeAlgorithms.Add(waithandle, algorithm);
|
---|
[1898] | 113 | }
|
---|
| 114 | activeEngines.Add(waithandle, engine);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public override string[] GetJobs() {
|
---|
[1922] | 118 | lock (activeAlgorithms) {
|
---|
| 119 | string[] retVal = new string[activeAlgorithms.Count];
|
---|
[1898] | 120 | int i = 0;
|
---|
[1922] | 121 | foreach (IAlgorithm a in activeAlgorithms.Values) {
|
---|
[2012] | 122 | retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
|
---|
[1898] | 123 | }
|
---|
| 124 | return retVal;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|