Free cookie consent management tool by TermsFeed Policy Generator

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

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

#650 (IAlgorithm and derived interfaces should provide properties to retrieve results):

  • Implemented properties to retrieve model quality
  • Changed CEDMA executor to retrieve results via properties
  • Removed obsolete class Execution in CEDMA (replaced by the interface IAlgorithm)
File size: 4.7 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<WaitHandle, 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, string gridUrl) : base(dispatcher, store) {
56      this.jobManager = new JobManager(gridUrl);
57      jobManager.Reset();
58    }
59
60    protected override void StartJobs() {
61      List<WaitHandle> wh = new List<WaitHandle>();
62      Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle, AtomicOperation>();
63      while (true) {
64        try {
65          // start new jobs as long as there are less than MaxActiveJobs
66          while (wh.Count < MaxActiveJobs) {
67            Thread.Sleep(StartJobInterval);
68            // get an execution from the dispatcher and execute in grid via job-manager
69            IAlgorithm algorithm = Dispatcher.GetNextJob();
70            if (algorithm != null) {
71              AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
72              WaitHandle opWh = jobManager.BeginExecuteOperation(algorithm.Engine.GlobalScope, op);
73              wh.Add(opWh);
74              activeOperations.Add(opWh, op);
75              lock (activeAlgorithms) {
76                activeAlgorithms.Add(opWh, algorithm);
77              }
78            }
79          }
80          // wait until any job is finished
81          WaitHandle[] whArr = wh.ToArray();
82          int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout);
83          if (readyHandleIndex != WaitHandle.WaitTimeout) {
84            WaitHandle readyHandle = whArr[readyHandleIndex];
85            AtomicOperation finishedOp = activeOperations[readyHandle];
86            wh.Remove(readyHandle);
87            IAlgorithm finishedAlgorithm = null;
88            lock (activeAlgorithms) {
89              finishedAlgorithm = activeAlgorithms[readyHandle];
90              activeAlgorithms.Remove(readyHandle);
91            }
92            activeOperations.Remove(readyHandle);
93            readyHandle.Close();
94            ProcessingEngine finishedEngine = null;
95            try {
96              finishedEngine = jobManager.EndExecuteOperation(finishedOp);
97            }
98            catch (Exception badEx) {
99              Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
100            }
101            if (finishedEngine != null) {
102              StoreResults(finishedAlgorithm);
103            }
104          }
105        }
106        catch (Exception ex) {
107          Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
108        }
109      }
110    }
111
112    public override string[] GetJobs() {
113      lock (activeAlgorithms) {
114        string[] retVal = new string[activeAlgorithms.Count];
115        int i = 0;
116        foreach (IAlgorithm a in activeAlgorithms.Values) {
117          retVal[i++] = "Target-Variable: " + a.TargetVariable + " " + a.Description;
118        }
119        return retVal;
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.