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.Xml;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Grid;
|
---|
29 | using System.ServiceModel;
|
---|
30 | using System.IO;
|
---|
31 | using System.IO.Compression;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using System.Windows.Forms;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.DistributedEngine {
|
---|
36 | public class DistributedEngine : EngineBase, IEditable {
|
---|
37 | private JobManager jobManager;
|
---|
38 | private CompositeOperation waitingOperations;
|
---|
39 | private string serverAddress;
|
---|
40 | public string ServerAddress {
|
---|
41 | get { return serverAddress; }
|
---|
42 | set {
|
---|
43 | if(value != serverAddress) {
|
---|
44 | serverAddress = value;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
49 | DistributedEngine clone = (DistributedEngine)base.Clone(clonedObjects);
|
---|
50 | clone.ServerAddress = serverAddress;
|
---|
51 | return clone;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public override IView CreateView() {
|
---|
55 | return new DistributedEngineEditor(this);
|
---|
56 | }
|
---|
57 | public virtual IEditor CreateEditor() {
|
---|
58 | return new DistributedEngineEditor(this);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override void Execute() {
|
---|
62 | if(jobManager == null) this.jobManager = new JobManager(serverAddress);
|
---|
63 | jobManager.Reset();
|
---|
64 | base.Execute();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override void ExecuteSteps(int steps) {
|
---|
68 | throw new InvalidOperationException("DistributedEngine doesn't support stepwise execution");
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected override void ProcessNextOperation() {
|
---|
72 | IOperation operation = myExecutionStack.Pop();
|
---|
73 | if(operation is AtomicOperation) {
|
---|
74 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
75 | IOperation next = null;
|
---|
76 | try {
|
---|
77 | next = atomicOperation.Operator.Execute(atomicOperation.Scope);
|
---|
78 | } catch(Exception ex) {
|
---|
79 | // push operation on stack again
|
---|
80 | myExecutionStack.Push(atomicOperation);
|
---|
81 | Abort();
|
---|
82 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
|
---|
83 | }
|
---|
84 | if(next != null)
|
---|
85 | myExecutionStack.Push(next);
|
---|
86 | OnOperationExecuted(atomicOperation);
|
---|
87 | if(atomicOperation.Operator.Breakpoint) Abort();
|
---|
88 | } else if(operation is CompositeOperation) {
|
---|
89 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
90 | if(compositeOperation.ExecuteInParallel) {
|
---|
91 | try {
|
---|
92 | WaitHandle[] waithandles = new WaitHandle[compositeOperation.Operations.Count];
|
---|
93 | int i = 0;
|
---|
94 | foreach(AtomicOperation parOperation in compositeOperation.Operations) {
|
---|
95 | waithandles[i++] = jobManager.BeginExecuteOperation(OperatorGraph, GlobalScope, parOperation);
|
---|
96 | }
|
---|
97 | // WaitAll works only with maximally 64 waithandles
|
---|
98 | if(waithandles.Length <= 64) {
|
---|
99 | WaitHandle.WaitAll(waithandles);
|
---|
100 | } else {
|
---|
101 | for(i = 0; i < waithandles.Length; i++) {
|
---|
102 | waithandles[i].WaitOne();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if(jobManager.Exception != null) {
|
---|
106 | myExecutionStack.Push(compositeOperation);
|
---|
107 | Abort();
|
---|
108 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(jobManager.Exception); });
|
---|
109 | }
|
---|
110 | } catch(Exception e) {
|
---|
111 | myExecutionStack.Push(compositeOperation);
|
---|
112 | Abort();
|
---|
113 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(jobManager.Exception); });
|
---|
114 | }
|
---|
115 | } else {
|
---|
116 | for(int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
117 | myExecutionStack.Push(compositeOperation.Operations[i]);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | #region Persistence Methods
|
---|
123 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
124 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
125 | XmlAttribute addressAttribute = document.CreateAttribute("ServerAddress");
|
---|
126 | addressAttribute.Value = ServerAddress;
|
---|
127 | node.Attributes.Append(addressAttribute);
|
---|
128 | return node;
|
---|
129 | }
|
---|
130 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
131 | base.Populate(node, restoredObjects);
|
---|
132 | ServerAddress = node.Attributes["ServerAddress"].Value;
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 | }
|
---|
136 | }
|
---|