[1060] | 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 HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 30 | using HeuristicLab.CEDMA.DB;
|
---|
| 31 | using System.ServiceModel.Description;
|
---|
| 32 | using System.Linq;
|
---|
| 33 | using HeuristicLab.CEDMA.Core;
|
---|
| 34 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 35 | using HeuristicLab.Data;
|
---|
| 36 | using HeuristicLab.Grid;
|
---|
| 37 | using System.Diagnostics;
|
---|
| 38 | using HeuristicLab.Core;
|
---|
| 39 | using System.Threading;
|
---|
[1922] | 40 | using HeuristicLab.Modeling;
|
---|
[1060] | 41 |
|
---|
| 42 | namespace HeuristicLab.CEDMA.Server {
|
---|
[1898] | 43 | public class GridExecuter : ExecuterBase {
|
---|
[1060] | 44 | private JobManager jobManager;
|
---|
[2055] | 45 | private Dictionary<AsyncGridResult, IAlgorithm> activeAlgorithms;
|
---|
[1060] | 46 |
|
---|
[1287] | 47 | private TimeSpan StartJobInterval {
|
---|
| 48 | get { return TimeSpan.FromMilliseconds(500); }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | private TimeSpan WaitForFinishedJobsTimeout {
|
---|
| 52 | get { return TimeSpan.FromMilliseconds(100); }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[2058] | 55 | public GridExecuter(IDispatcher dispatcher, IStore store, IGridServer server)
|
---|
[2012] | 56 | : base(dispatcher, store) {
|
---|
[2058] | 57 | this.jobManager = new JobManager(server);
|
---|
[2055] | 58 | activeAlgorithms = new Dictionary<AsyncGridResult, IAlgorithm>();
|
---|
[1060] | 59 | jobManager.Reset();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[1898] | 62 | protected override void StartJobs() {
|
---|
[2055] | 63 | Dictionary<WaitHandle, AsyncGridResult> asyncResults = new Dictionary<WaitHandle,AsyncGridResult>();
|
---|
[1060] | 64 | while (true) {
|
---|
| 65 | try {
|
---|
| 66 | // start new jobs as long as there are less than MaxActiveJobs
|
---|
[2055] | 67 | while (asyncResults.Count < MaxActiveJobs) {
|
---|
[1287] | 68 | Thread.Sleep(StartJobInterval);
|
---|
[1060] | 69 | // get an execution from the dispatcher and execute in grid via job-manager
|
---|
[1922] | 70 | IAlgorithm algorithm = Dispatcher.GetNextJob();
|
---|
| 71 | if (algorithm != null) {
|
---|
| 72 | AtomicOperation op = new AtomicOperation(algorithm.Engine.OperatorGraph.InitialOperator, algorithm.Engine.GlobalScope);
|
---|
[2064] | 73 | ProcessingEngine procEngine = new ProcessingEngine(algorithm.Engine.GlobalScope, op);
|
---|
| 74 | procEngine.OperatorGraph.AddOperator(algorithm.Engine.OperatorGraph.InitialOperator);
|
---|
| 75 | procEngine.OperatorGraph.InitialOperator = algorithm.Engine.OperatorGraph.InitialOperator;
|
---|
| 76 | AsyncGridResult asyncResult = jobManager.BeginExecuteEngine(procEngine);
|
---|
[2055] | 77 | asyncResults.Add(asyncResult.WaitHandle, asyncResult);
|
---|
[1922] | 78 | lock (activeAlgorithms) {
|
---|
[2055] | 79 | activeAlgorithms.Add(asyncResult, algorithm);
|
---|
[1287] | 80 | }
|
---|
| 81 | }
|
---|
[1060] | 82 | }
|
---|
| 83 | // wait until any job is finished
|
---|
[2055] | 84 | WaitHandle[] whArr = asyncResults.Keys.ToArray();
|
---|
[1287] | 85 | int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout);
|
---|
| 86 | if (readyHandleIndex != WaitHandle.WaitTimeout) {
|
---|
| 87 | WaitHandle readyHandle = whArr[readyHandleIndex];
|
---|
[1922] | 88 | IAlgorithm finishedAlgorithm = null;
|
---|
[2055] | 89 | AsyncGridResult finishedResult = null;
|
---|
[1922] | 90 | lock (activeAlgorithms) {
|
---|
[2055] | 91 | finishedResult = asyncResults[readyHandle];
|
---|
| 92 | finishedAlgorithm = activeAlgorithms[finishedResult];
|
---|
| 93 | activeAlgorithms.Remove(finishedResult);
|
---|
| 94 | asyncResults.Remove(readyHandle);
|
---|
[1287] | 95 | }
|
---|
| 96 | try {
|
---|
[2055] | 97 | IEngine finishedEngine = jobManager.EndExecuteEngine(finishedResult);
|
---|
| 98 | SetResults(finishedEngine.GlobalScope, finishedAlgorithm.Engine.GlobalScope);
|
---|
[2053] | 99 | StoreResults(finishedAlgorithm);
|
---|
[1287] | 100 | }
|
---|
| 101 | catch (Exception badEx) {
|
---|
| 102 | Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message);
|
---|
| 103 | }
|
---|
[1060] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | catch (Exception ex) {
|
---|
| 107 | Trace.WriteLine("CEDMA Executer: Exception in job-management thread. " + ex.Message);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[1287] | 111 |
|
---|
[1898] | 112 | public override string[] GetJobs() {
|
---|
[1922] | 113 | lock (activeAlgorithms) {
|
---|
| 114 | string[] retVal = new string[activeAlgorithms.Count];
|
---|
[1287] | 115 | int i = 0;
|
---|
[1922] | 116 | foreach (IAlgorithm a in activeAlgorithms.Values) {
|
---|
[2012] | 117 | retVal[i++] = a.Name + " " + a.Dataset.GetVariableName(a.TargetVariable);
|
---|
[1287] | 118 | }
|
---|
| 119 | return retVal;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[1060] | 122 | }
|
---|
| 123 | }
|
---|