[1060] | 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 System.ServiceModel.Description;
|
---|
| 30 | using System.Linq;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
| 32 | using HeuristicLab.Grid;
|
---|
| 33 | using System.Diagnostics;
|
---|
| 34 | using HeuristicLab.Core;
|
---|
| 35 | using System.Threading;
|
---|
[1922] | 36 | using HeuristicLab.Modeling;
|
---|
[2223] | 37 | using HeuristicLab.Modeling.Database;
|
---|
[1060] | 38 |
|
---|
| 39 | namespace HeuristicLab.CEDMA.Server {
|
---|
[1898] | 40 | public class GridExecuter : ExecuterBase {
|
---|
[1060] | 41 | private JobManager jobManager;
|
---|
[2223] | 42 | private Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
|
---|
[1060] | 43 |
|
---|
[1287] | 44 | private TimeSpan StartJobInterval {
|
---|
[2258] | 45 | get { return TimeSpan.FromMilliseconds(3000); }
|
---|
[1287] | 46 | }
|
---|
| 47 |
|
---|
| 48 | private TimeSpan WaitForFinishedJobsTimeout {
|
---|
| 49 | get { return TimeSpan.FromMilliseconds(100); }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[2258] | 52 | public GridExecuter(IDispatcher dispatcher, IGridServer server, IModelingDatabase databaseService)
|
---|
[2223] | 53 | : base(dispatcher, databaseService) {
|
---|
[2058] | 54 | this.jobManager = new JobManager(server);
|
---|
[2223] | 55 | activeAlgorithms = new Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm>();
|
---|
[1060] | 56 | jobManager.Reset();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1898] | 59 | protected override void StartJobs() {
|
---|
[2094] | 60 | Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle, AsyncGridResult>();
|
---|
[2258] | 61 | // inifinite loop:
|
---|
| 62 | // 1. try to dispatch one algo
|
---|
| 63 | // 2. when at least one run is dispatched try to get the result
|
---|
| 64 | // 3. sleep
|
---|
[1060] | 65 | while (true) {
|
---|
| 66 | try {
|
---|
[2258] | 67 | // if allowed then try to dispatch another run
|
---|
| 68 | if (asyncResults.Count < MaxActiveJobs) {
|
---|
[1060] | 69 | // get an execution from the dispatcher and execute in grid via job-manager
|
---|
[2223] | 70 | HeuristicLab.Modeling.IAlgorithm algorithm = Dispatcher.GetNextJob();
|
---|
[1922] | 71 | if (algorithm != null) {
|
---|
| 72 | AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
|
---|
[2064] | 73 | ProcessingEngine procEngine = new ProcessingEngine(algorithm.Engine.GlobalScope, op);
|
---|
| 74 | procEngine.OperatorGraph.AddOperator(algorithm.Engine.OperatorGraph.InitialOperator);
|
---|
| 75 | procEngine.OperatorGraph.InitialOperator = algorithm.Engine.OperatorGraph.InitialOperator;
|
---|
[2073] | 76 | procEngine.Reset();
|
---|
[2064] | 77 | AsyncGridResult asyncResult = jobManager.BeginExecuteEngine(procEngine);
|
---|
[2055] | 78 | asyncResults.Add(asyncResult.WaitHandle, asyncResult);
|
---|
[1922] | 79 | lock (activeAlgorithms) {
|
---|
[2055] | 80 | activeAlgorithms.Add(asyncResult, algorithm);
|
---|
[1287] | 81 | }
|
---|
[2094] | 82 | OnChanged();
|
---|
[1287] | 83 | }
|
---|
[1060] | 84 | }
|
---|
[2258] | 85 | // when there are active runs
|
---|
[2145] | 86 | if (asyncResults.Count > 0) {
|
---|
| 87 | WaitHandle[] whArr = asyncResults.Keys.ToArray();
|
---|
| 88 | int readyHandleIndex = WaitAny(whArr, WaitForFinishedJobsTimeout);
|
---|
[2258] | 89 | // if the wait didn't timeout, a new result is ready
|
---|
[2145] | 90 | if (readyHandleIndex != WaitHandle.WaitTimeout) {
|
---|
[2258] | 91 | // request the finished run and clean up
|
---|
[2145] | 92 | WaitHandle readyHandle = whArr[readyHandleIndex];
|
---|
[2258] | 93 | AsyncGridResult finishedResult = asyncResults[readyHandle];
|
---|
| 94 | asyncResults.Remove(readyHandle);
|
---|
[2223] | 95 | HeuristicLab.Modeling.IAlgorithm finishedAlgorithm = null;
|
---|
[2145] | 96 | lock (activeAlgorithms) {
|
---|
| 97 | finishedAlgorithm = activeAlgorithms[finishedResult];
|
---|
| 98 | activeAlgorithms.Remove(finishedResult);
|
---|
| 99 | }
|
---|
| 100 | OnChanged();
|
---|
| 101 | try {
|
---|
| 102 | IEngine finishedEngine = jobManager.EndExecuteEngine(finishedResult);
|
---|
[2422] | 103 | SetResults(finishedEngine.GlobalScope, finishedAlgorithm.Engine.GlobalScope);
|
---|
| 104 | StoreResults(finishedAlgorithm);
|
---|
[2145] | 105 | }
|
---|
| 106 | catch (Exception badEx) {
|
---|
[2258] | 107 | HeuristicLab.Tracing.Logger.Error("CEDMA Executer: Exception in job execution thread. " + badEx.Message + Environment.NewLine + badEx.StackTrace);
|
---|
[2145] | 108 | }
|
---|
[1287] | 109 | }
|
---|
[1060] | 110 | }
|
---|
[2258] | 111 | // when there are no active runs then sleep until we try to start a new run (to prevent excessive looping)
|
---|
| 112 | Thread.Sleep(StartJobInterval);
|
---|
[1060] | 113 | }
|
---|
| 114 | catch (Exception ex) {
|
---|
[2153] | 115 | HeuristicLab.Tracing.Logger.Warn("CEDMA Executer: Exception in job-management thread. " + ex.Message + Environment.NewLine + ex.StackTrace);
|
---|
[1060] | 116 | }
|
---|
[2258] | 117 | } // end while(true)
|
---|
[1060] | 118 | }
|
---|
[1287] | 119 |
|
---|
[2145] | 120 | // wait until any job is finished
|
---|
[2125] | 121 | private int WaitAny(WaitHandle[] wh, TimeSpan WaitForFinishedJobsTimeout) {
|
---|
[2128] | 122 | if (wh.Length <= 64) {
|
---|
[2125] | 123 | return WaitHandle.WaitAny(wh, WaitForFinishedJobsTimeout);
|
---|
| 124 | } else {
|
---|
| 125 | for (int i = 0; i < wh.Length; i++) {
|
---|
| 126 | if (wh[i].WaitOne(WaitForFinishedJobsTimeout)) {
|
---|
| 127 | return i;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | return WaitHandle.WaitTimeout;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[1898] | 134 | public override string[] GetJobs() {
|
---|
[1922] | 135 | lock (activeAlgorithms) {
|
---|
| 136 | string[] retVal = new string[activeAlgorithms.Count];
|
---|
[1287] | 137 | int i = 0;
|
---|
[2223] | 138 | foreach (HeuristicLab.Modeling.IAlgorithm a in activeAlgorithms.Values) {
|
---|
[2440] | 139 | retVal[i++] = a.Name + " " + a.TargetVariable;
|
---|
[1287] | 140 | }
|
---|
| 141 | return retVal;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
[1060] | 144 | }
|
---|
| 145 | }
|
---|