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