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