Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.CEDMA.Server/3.3/GridExecuter.cs @ 16472

Last change on this file since 16472 was 2440, checked in by gkronber, 15 years ago

Fixed #784 (ProblemInjector should be changed to read variable names instead of indexes for input and target variables)

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