Free cookie consent management tool by TermsFeed Policy Generator

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

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

Changed CEDMA dispatcher to allow different input variable sets for each target variable. #676 (Cockpit for the CEDMA Server to control algorithm settings)

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