Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/GridExecuter.cs @ 2273

Last change on this file since 2273 was 2258, checked in by gkronber, 15 years ago
  • Reimplemented method to read a list of already executed algorithms and configurations from the results DB
  • Fixed a bug in the GridExecuter
  • Added a button in the dispatcher view to speed up configuration of input variables

#712

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