Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from GP-refactoring branch back into the trunk #713.

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