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 |
|
---|
33 | namespace HeuristicLab.DistributedEngine {
|
---|
34 | public class DistributedEngine : EngineBase, IEditable {
|
---|
35 | // currently executed operators
|
---|
36 | private IOperator[] currentOperators;
|
---|
37 | private int operatorIndex;
|
---|
38 | private IGridServer server;
|
---|
39 |
|
---|
40 | private string serverAddress;
|
---|
41 | public string ServerAddress {
|
---|
42 | get { return serverAddress; }
|
---|
43 | set {
|
---|
44 | if(value != serverAddress) {
|
---|
45 | serverAddress = value;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public DistributedEngine() {
|
---|
51 | currentOperators = new IOperator[1000];
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
56 | DistributedEngine clone = (DistributedEngine)base.Clone(clonedObjects);
|
---|
57 | clone.ServerAddress = serverAddress;
|
---|
58 | return clone;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IView CreateView() {
|
---|
62 | return new DistributedEngineEditor(this);
|
---|
63 | }
|
---|
64 | public virtual IEditor CreateEditor() {
|
---|
65 | return new DistributedEngineEditor(this);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void Execute() {
|
---|
69 | NetTcpBinding binding = new NetTcpBinding();
|
---|
70 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
71 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
72 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
73 | ChannelFactory<IGridServer> factory = new ChannelFactory<IGridServer>(binding);
|
---|
74 | server = factory.CreateChannel(new EndpointAddress(serverAddress));
|
---|
75 |
|
---|
76 | base.Execute();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override void ExecuteSteps(int steps) {
|
---|
80 | throw new InvalidOperationException("DistributedEngine doesn't support stepwise execution");
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override void Abort() {
|
---|
84 | base.Abort();
|
---|
85 | for(int i = 0; i < currentOperators.Length; i++) {
|
---|
86 | if(currentOperators[i] != null)
|
---|
87 | currentOperators[i].Abort();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void ProcessNextOperation() {
|
---|
92 | operatorIndex = 1;
|
---|
93 | ProcessNextOperation(myExecutionStack, 0);
|
---|
94 | }
|
---|
95 | private void ProcessNextOperation(Stack<IOperation> stack, int currentOperatorIndex) {
|
---|
96 | IOperation operation = stack.Pop();
|
---|
97 | if(operation is AtomicOperation) {
|
---|
98 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
99 | IOperation next = null;
|
---|
100 | try {
|
---|
101 | currentOperators[currentOperatorIndex] = atomicOperation.Operator;
|
---|
102 | next = atomicOperation.Operator.Execute(atomicOperation.Scope);
|
---|
103 | } catch(Exception ex) {
|
---|
104 | // push operation on stack again
|
---|
105 | stack.Push(atomicOperation);
|
---|
106 | Abort();
|
---|
107 | ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
|
---|
108 | }
|
---|
109 | if(next != null)
|
---|
110 | stack.Push(next);
|
---|
111 | OnOperationExecuted(atomicOperation);
|
---|
112 | if(atomicOperation.Operator.Breakpoint) Abort();
|
---|
113 | } else if(operation is CompositeOperation) {
|
---|
114 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
115 | if(compositeOperation.ExecuteInParallel) {
|
---|
116 | Dictionary<Guid, AtomicOperation> runningEngines = new Dictionary<Guid, AtomicOperation>();
|
---|
117 | foreach(AtomicOperation parOperation in compositeOperation.Operations) {
|
---|
118 | ProcessingEngine engine = new ProcessingEngine(OperatorGraph, GlobalScope, parOperation); // OperatorGraph not needed?
|
---|
119 | MemoryStream memStream = new MemoryStream();
|
---|
120 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
121 | PersistenceManager.Save(engine, stream);
|
---|
122 | stream.Close();
|
---|
123 | Guid currentEngineGuid = server.BeginExecuteEngine(memStream.ToArray());
|
---|
124 | runningEngines[currentEngineGuid] = parOperation;
|
---|
125 | }
|
---|
126 | foreach(Guid engineGuid in runningEngines.Keys) {
|
---|
127 | byte[] scopeXml = server.EndExecuteEngine(engineGuid);
|
---|
128 | GZipStream stream = new GZipStream(new MemoryStream(scopeXml), CompressionMode.Decompress);
|
---|
129 | IScope newScope = (IScope)PersistenceManager.Load(stream);
|
---|
130 | IScope oldScope = runningEngines[engineGuid].Scope;
|
---|
131 | oldScope.Clear();
|
---|
132 | foreach(IVariable variable in newScope.Variables) {
|
---|
133 | oldScope.AddVariable(variable);
|
---|
134 | }
|
---|
135 | foreach(IScope subScope in newScope.SubScopes) {
|
---|
136 | oldScope.AddSubScope(subScope);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | // TASK (gkronber 12.2.08)
|
---|
141 | //if (Canceled) {
|
---|
142 | // // write back not finished tasks
|
---|
143 | // CompositeOperation remaining = new CompositeOperation();
|
---|
144 | // remaining.ExecuteInParallel = true;
|
---|
145 | // for (int i = 0; i < list.tasks.Length; i++) {
|
---|
146 | // if (list.tasks[i].Count > 0) {
|
---|
147 | // CompositeOperation task = new CompositeOperation();
|
---|
148 | // while (list.tasks[i].Count > 0)
|
---|
149 | // task.AddOperation(list.tasks[i].Pop());
|
---|
150 | // remaining.AddOperation(task);
|
---|
151 | // }
|
---|
152 | // }
|
---|
153 | // if (remaining.Operations.Count > 0)
|
---|
154 | // stack.Push(remaining);
|
---|
155 | //}
|
---|
156 | } else {
|
---|
157 | for(int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
158 | stack.Push(compositeOperation.Operations[i]);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | #region Persistence Methods
|
---|
164 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
165 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
166 | XmlAttribute addressAttribute = document.CreateAttribute("ServerAddress");
|
---|
167 | addressAttribute.Value = ServerAddress;
|
---|
168 | node.Attributes.Append(addressAttribute);
|
---|
169 | return node;
|
---|
170 | }
|
---|
171 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
172 | base.Populate(node, restoredObjects);
|
---|
173 | ServerAddress = node.Attributes["ServerAddress"].Value;
|
---|
174 | }
|
---|
175 | #endregion
|
---|
176 | }
|
---|
177 | }
|
---|