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
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 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;
[1922]40using HeuristicLab.Modeling;
[2195]41using HeuristicLab.Modeling.Database;
[1060]42
43namespace HeuristicLab.CEDMA.Server {
[1898]44  public class GridExecuter : ExecuterBase {
[1060]45    private JobManager jobManager;
[2195]46    private Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
[1060]47
[1287]48    private TimeSpan StartJobInterval {
49      get { return TimeSpan.FromMilliseconds(500); }
50    }
51
52    private TimeSpan WaitForFinishedJobsTimeout {
53      get { return TimeSpan.FromMilliseconds(100); }
54    }
55
[2145]56    private TimeSpan WaitForNewJobsInterval {
57      get { return TimeSpan.FromSeconds(3); }
58    }
59
[2185]60    public GridExecuter(IDispatcher dispatcher,  IGridServer server, IModelingDatabase databaseService)
[2195]61      : base(dispatcher, databaseService) {
[2058]62      this.jobManager = new JobManager(server);
[2195]63      activeAlgorithms = new Dictionary<AsyncGridResult, HeuristicLab.Modeling.IAlgorithm>();
[1060]64      jobManager.Reset();
65    }
66
[1898]67    protected override void StartJobs() {
[2094]68      Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle, AsyncGridResult>();
[1060]69      while (true) {
70        try {
71          // start new jobs as long as there are less than MaxActiveJobs
[2055]72          while (asyncResults.Count < MaxActiveJobs) {
[1287]73            Thread.Sleep(StartJobInterval);
[1060]74            // get an execution from the dispatcher and execute in grid via job-manager
[2195]75            HeuristicLab.Modeling.IAlgorithm algorithm = Dispatcher.GetNextJob();
[1922]76            if (algorithm != null) {
77              AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
[2064]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;
[2073]81              procEngine.Reset();
[2064]82              AsyncGridResult asyncResult = jobManager.BeginExecuteEngine(procEngine);
[2055]83              asyncResults.Add(asyncResult.WaitHandle, asyncResult);
[1922]84              lock (activeAlgorithms) {
[2055]85                activeAlgorithms.Add(asyncResult, algorithm);
[1287]86              }
[2094]87              OnChanged();
[1287]88            }
[1060]89          }
[2145]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];
[2195]95              HeuristicLab.Modeling.IAlgorithm finishedAlgorithm = null;
[2145]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) {
[2152]110                HeuristicLab.Tracing.Logger.Error("CEDMA Executer: Exception in job execution thread. " + badEx.Message+Environment.NewLine+badEx.StackTrace);
[2145]111              }
[1287]112            }
[2145]113          } else {
114            Thread.Sleep(WaitForNewJobsInterval);
[1060]115          }
116        }
[2145]117
[1060]118        catch (Exception ex) {
[2153]119          HeuristicLab.Tracing.Logger.Warn("CEDMA Executer: Exception in job-management thread. " + ex.Message + Environment.NewLine + ex.StackTrace);
[1060]120        }
121      }
122    }
[1287]123
[2145]124    // wait until any job is finished
[2125]125    private int WaitAny(WaitHandle[] wh, TimeSpan WaitForFinishedJobsTimeout) {
[2128]126      if (wh.Length <= 64) {
[2125]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
[1898]138    public override string[] GetJobs() {
[1922]139      lock (activeAlgorithms) {
140        string[] retVal = new string[activeAlgorithms.Count];
[1287]141        int i = 0;
[2195]142        foreach (HeuristicLab.Modeling.IAlgorithm a in activeAlgorithms.Values) {
[2012]143          retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
[1287]144        }
145        return retVal;
146      }
147    }
[1060]148  }
149}
Note: See TracBrowser for help on using the repository browser.