Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed a bug #681

File size: 5.5 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 HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34using HeuristicLab.GP.StructureIdentification;
35using HeuristicLab.Data;
36using HeuristicLab.Grid;
37using System.Diagnostics;
38using HeuristicLab.Core;
39using System.Threading;
40using HeuristicLab.Modeling;
41
42namespace HeuristicLab.CEDMA.Server {
43  public class GridExecuter : ExecuterBase {
44    private JobManager jobManager;
45    private Dictionary<AsyncGridResult, IAlgorithm> activeAlgorithms;
46
47    private TimeSpan StartJobInterval {
48      get { return TimeSpan.FromMilliseconds(500); }
49    }
50
51    private TimeSpan WaitForFinishedJobsTimeout {
52      get { return TimeSpan.FromMilliseconds(100); }
53    }
54
55    public GridExecuter(IDispatcher dispatcher, IStore store, IGridServer server)
56      : base(dispatcher, store) {
57      this.jobManager = new JobManager(server);
58      activeAlgorithms = new Dictionary<AsyncGridResult, IAlgorithm>();
59      jobManager.Reset();
60    }
61
62    protected override void StartJobs() {
63      Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle, AsyncGridResult>();
64      while (true) {
65        try {
66          // start new jobs as long as there are less than MaxActiveJobs
67          while (asyncResults.Count < MaxActiveJobs) {
68            Thread.Sleep(StartJobInterval);
69            // get an execution from the dispatcher and execute in grid via job-manager
70            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          WaitHandle[] whArr = asyncResults.Keys.ToArray();
86          int readyHandleIndex = WaitAny(whArr, WaitForFinishedJobsTimeout);
87          if (readyHandleIndex != WaitHandle.WaitTimeout) {
88            WaitHandle readyHandle = whArr[readyHandleIndex];
89            IAlgorithm finishedAlgorithm = null;
90            AsyncGridResult finishedResult = null;
91            lock (activeAlgorithms) {
92              finishedResult = asyncResults[readyHandle];
93              finishedAlgorithm = activeAlgorithms[finishedResult];
94              activeAlgorithms.Remove(finishedResult);
95              asyncResults.Remove(readyHandle);
96            }
97            OnChanged();
98            try {
99              IEngine finishedEngine = jobManager.EndExecuteEngine(finishedResult);
100              SetResults(finishedEngine.GlobalScope, finishedAlgorithm.Engine.GlobalScope);
101              StoreResults(finishedAlgorithm);
102            }
103            catch (Exception badEx) {
104              HeuristicLab.Tracing.Logger.Error("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
105            }
106          }
107        }
108        catch (Exception ex) {
109          HeuristicLab.Tracing.Logger.Warn("CEDMA Executer: Exception in job-management thread. " + ex.Message);
110        }
111      }
112    }
113
114        // wait until any job is finished
115    private int WaitAny(WaitHandle[] wh, TimeSpan WaitForFinishedJobsTimeout) {
116      if (wh.Length <= 64) {
117        return WaitHandle.WaitAny(wh, WaitForFinishedJobsTimeout);
118      } else {
119        for (int i = 0; i < wh.Length; i++) {
120          if (wh[i].WaitOne(WaitForFinishedJobsTimeout)) {
121            return i;
122          }
123        }
124        return WaitHandle.WaitTimeout;
125      }
126    }
127
128    public override string[] GetJobs() {
129      lock (activeAlgorithms) {
130        string[] retVal = new string[activeAlgorithms.Count];
131        int i = 0;
132        foreach (IAlgorithm a in activeAlgorithms.Values) {
133          retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
134        }
135        return retVal;
136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.