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