Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2352 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
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 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;
37using HeuristicLab.Modeling;
38using HeuristicLab.Modeling.Database;
39
40namespace HeuristicLab.CEDMA.Server {
41  public class GridExecuter : ExecuterBase {
42    private JobManager jobManager;
43    private Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
44
45    private TimeSpan StartJobInterval {
46      get { return TimeSpan.FromMilliseconds(3000); }
47    }
48
49    private TimeSpan WaitForFinishedJobsTimeout {
50      get { return TimeSpan.FromMilliseconds(100); }
51    }
52
53    public GridExecuter(IDispatcher dispatcher, IGridServer server, IModelingDatabase databaseService)
54      : base(dispatcher, databaseService) {
55      this.jobManager = new JobManager(server);
56      activeAlgorithms = new Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm>();
57      jobManager.Reset();
58    }
59
60    protected override void StartJobs() {
61      Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle, AsyncGridResult>();
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
66      while (true) {
67        try {
68          // if allowed then try to dispatch another run
69          if (asyncResults.Count < MaxActiveJobs) {
70            // get an execution from the dispatcher and execute in grid via job-manager
71            HeuristicLab.Modeling.IAlgorithm algorithm = Dispatcher.GetNextJob();
72            if (algorithm != null) {
73              AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
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;
77              procEngine.Reset();
78              AsyncGridResult asyncResult = jobManager.BeginExecuteEngine(procEngine);
79              asyncResults.Add(asyncResult.WaitHandle, asyncResult);
80              lock (activeAlgorithms) {
81                activeAlgorithms.Add(asyncResult, algorithm);
82              }
83              OnChanged();
84            }
85          }
86          // when there are active runs
87          if (asyncResults.Count > 0) {
88            WaitHandle[] whArr = asyncResults.Keys.ToArray();
89            int readyHandleIndex = WaitAny(whArr, WaitForFinishedJobsTimeout);
90            // if the wait didn't timeout, a new result is ready
91            if (readyHandleIndex != WaitHandle.WaitTimeout) {
92              // request the finished run and clean up
93              WaitHandle readyHandle = whArr[readyHandleIndex];
94              AsyncGridResult finishedResult = asyncResults[readyHandle];
95              asyncResults.Remove(readyHandle);
96              HeuristicLab.Modeling.IAlgorithm finishedAlgorithm = null;
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) {
108                HeuristicLab.Tracing.Logger.Error("CEDMA Executer: Exception in job execution thread. " + badEx.Message + Environment.NewLine + badEx.StackTrace);
109              }
110            }
111          }
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);
114        }
115        catch (Exception ex) {
116          HeuristicLab.Tracing.Logger.Warn("CEDMA Executer: Exception in job-management thread. " + ex.Message + Environment.NewLine + ex.StackTrace);
117        }
118      } // end while(true)
119    }
120
121    // wait until any job is finished
122    private int WaitAny(WaitHandle[] wh, TimeSpan WaitForFinishedJobsTimeout) {
123      if (wh.Length <= 64) {
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
135    public override string[] GetJobs() {
136      lock (activeAlgorithms) {
137        string[] retVal = new string[activeAlgorithms.Count];
138        int i = 0;
139        foreach (HeuristicLab.Modeling.IAlgorithm a in activeAlgorithms.Values) {
140          retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
141        }
142        return retVal;
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.