Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented and integrated first version of backend for execution of CEDMA runs on HL.Hive. #642 (Hive backend for CEDMA)

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