Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Modeling Database Backend/sources/HeuristicLab.CEDMA.Server/3.3/GridExecuter.cs @ 2204

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

Fixed compile errors #712.

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 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;
41using HeuristicLab.Modeling.Database;
42
43namespace HeuristicLab.CEDMA.Server {
44  public class GridExecuter : ExecuterBase {
45    private JobManager jobManager;
46    private Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
47
48    private TimeSpan StartJobInterval {
49      get { return TimeSpan.FromMilliseconds(500); }
50    }
51
52    private TimeSpan WaitForFinishedJobsTimeout {
53      get { return TimeSpan.FromMilliseconds(100); }
54    }
55
56    private TimeSpan WaitForNewJobsInterval {
57      get { return TimeSpan.FromSeconds(3); }
58    }
59
60    public GridExecuter(IDispatcher dispatcher,  IGridServer server, IModelingDatabase databaseService)
61      : base(dispatcher, databaseService) {
62      this.jobManager = new JobManager(server);
63      activeAlgorithms = new Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm>();
64      jobManager.Reset();
65    }
66
67    protected override void StartJobs() {
68      Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle, AsyncGridResult>();
69      while (true) {
70        try {
71          // start new jobs as long as there are less than MaxActiveJobs
72          while (asyncResults.Count < MaxActiveJobs) {
73            Thread.Sleep(StartJobInterval);
74            // get an execution from the dispatcher and execute in grid via job-manager
75            HeuristicLab.Modeling.IAlgorithm algorithm = Dispatcher.GetNextJob();
76            if (algorithm != null) {
77              AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
78              ProcessingEngine procEngine = new ProcessingEngine(algorithm.Engine.GlobalScope, op);
79              procEngine.OperatorGraph.AddOperator(algorithm.Engine.OperatorGraph.InitialOperator);
80              procEngine.OperatorGraph.InitialOperator = algorithm.Engine.OperatorGraph.InitialOperator;
81              procEngine.Reset();
82              AsyncGridResult asyncResult = jobManager.BeginExecuteEngine(procEngine);
83              asyncResults.Add(asyncResult.WaitHandle, asyncResult);
84              lock (activeAlgorithms) {
85                activeAlgorithms.Add(asyncResult, algorithm);
86              }
87              OnChanged();
88            }
89          }
90          if (asyncResults.Count > 0) {
91            WaitHandle[] whArr = asyncResults.Keys.ToArray();
92            int readyHandleIndex = WaitAny(whArr, WaitForFinishedJobsTimeout);
93            if (readyHandleIndex != WaitHandle.WaitTimeout) {
94              WaitHandle readyHandle = whArr[readyHandleIndex];
95              HeuristicLab.Modeling.IAlgorithm finishedAlgorithm = null;
96              AsyncGridResult finishedResult = null;
97              lock (activeAlgorithms) {
98                finishedResult = asyncResults[readyHandle];
99                finishedAlgorithm = activeAlgorithms[finishedResult];
100                activeAlgorithms.Remove(finishedResult);
101                asyncResults.Remove(readyHandle);
102              }
103              OnChanged();
104              try {
105                IEngine finishedEngine = jobManager.EndExecuteEngine(finishedResult);
106                SetResults(finishedEngine.GlobalScope, finishedAlgorithm.Engine.GlobalScope);
107                StoreResults(finishedAlgorithm);
108              }
109              catch (Exception badEx) {
110                HeuristicLab.Tracing.Logger.Error("CEDMA Executer: Exception in job execution thread. " + badEx.Message+Environment.NewLine+badEx.StackTrace);
111              }
112            }
113          } else {
114            Thread.Sleep(WaitForNewJobsInterval);
115          }
116        }
117
118        catch (Exception ex) {
119          HeuristicLab.Tracing.Logger.Warn("CEDMA Executer: Exception in job-management thread. " + ex.Message + Environment.NewLine + ex.StackTrace);
120        }
121      }
122    }
123
124    // wait until any job is finished
125    private int WaitAny(WaitHandle[] wh, TimeSpan WaitForFinishedJobsTimeout) {
126      if (wh.Length <= 64) {
127        return WaitHandle.WaitAny(wh, WaitForFinishedJobsTimeout);
128      } else {
129        for (int i = 0; i < wh.Length; i++) {
130          if (wh[i].WaitOne(WaitForFinishedJobsTimeout)) {
131            return i;
132          }
133        }
134        return WaitHandle.WaitTimeout;
135      }
136    }
137
138    public override string[] GetJobs() {
139      lock (activeAlgorithms) {
140        string[] retVal = new string[activeAlgorithms.Count];
141        int i = 0;
142        foreach (HeuristicLab.Modeling.IAlgorithm a in activeAlgorithms.Values) {
143          retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
144        }
145        return retVal;
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.