1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System.Reflection;
|
---|
28 | using System.IO;
|
---|
29 | using HeuristicLab.Persistence.Default.Xml;
|
---|
30 | using System.Diagnostics;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using System.Linq;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.MPIEngine {
|
---|
35 | /// <summary>
|
---|
36 | /// Represents an engine that executes its steps in parallel (if possible) using multiple threads.
|
---|
37 | /// This engine is suitable for parallel processing on shared memory systems which provide multiple cores.
|
---|
38 | /// </summary>
|
---|
39 | [StorableClass]
|
---|
40 | [Item("MPI Engine", "Engine for parallel execution of algorithms using multiple processes (suitable for distributed memory systems with multiple cores).")]
|
---|
41 | public class MPIEngine : Engine {
|
---|
42 | [StorableConstructor]
|
---|
43 | protected MPIEngine(bool deserializing) : base(deserializing) { }
|
---|
44 | protected MPIEngine(MPIEngine original, Cloner cloner) : base(original, cloner) { }
|
---|
45 | public MPIEngine() : base() { }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new MPIEngine(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private string algFile;
|
---|
52 |
|
---|
53 | public override void Start() {
|
---|
54 | if (ExecutionStack.Count == 1) {
|
---|
55 | ExecutionContext context = ExecutionStack.First() as ExecutionContext;
|
---|
56 |
|
---|
57 | ExecutionContext algorithmContext = context.Parent as ExecutionContext;
|
---|
58 |
|
---|
59 | EngineAlgorithm alg = typeof(ExecutionContext).InvokeMember("parameterizedItem",
|
---|
60 | BindingFlags.GetField | BindingFlags.NonPublic |
|
---|
61 | BindingFlags.Instance, null, algorithmContext, null) as EngineAlgorithm;
|
---|
62 |
|
---|
63 | alg = alg.Clone() as EngineAlgorithm;
|
---|
64 | alg.Engine = new SequentialEngine.SequentialEngine();
|
---|
65 |
|
---|
66 | algFile = Path.GetTempFileName();
|
---|
67 | XmlGenerator.Serialize(alg, algFile);
|
---|
68 | }
|
---|
69 |
|
---|
70 | base.Start();
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override void Run(System.Threading.CancellationToken cancellationToken) {
|
---|
74 | if (ExecutionStack.Count == 1) {
|
---|
75 | ExecutionContext context = ExecutionStack.Pop() as ExecutionContext;
|
---|
76 |
|
---|
77 | IScope globalScope = context.Scope;
|
---|
78 |
|
---|
79 | string resultFile = Path.GetTempFileName();
|
---|
80 | ItemList<ResultCollection> empty = new ItemList<ResultCollection>();
|
---|
81 | XmlGenerator.Serialize(empty, resultFile);
|
---|
82 |
|
---|
83 | string exec = @"C:\Program Files\Microsoft Compute Cluster Pack\Bin\mpiexec.exe";
|
---|
84 | string args = @"-n 3 HeuristicLab.MPIAlgorithmRunner-3.3.exe " + algFile + " " + resultFile + " 5000";
|
---|
85 |
|
---|
86 | System.Threading.Thread pollThread = new System.Threading.Thread(delegate(object state) {
|
---|
87 | while (true) {
|
---|
88 | System.Threading.Thread.Sleep(5000);
|
---|
89 |
|
---|
90 | lock (resultFile) {
|
---|
91 | object results = XmlParser.Deserialize(resultFile);
|
---|
92 | ResultCollection resultCollection = (globalScope.Variables["Results"].Value as ResultCollection);
|
---|
93 |
|
---|
94 | if (resultCollection != null) {
|
---|
95 | if (!resultCollection.ContainsKey("MPIResults"))
|
---|
96 | resultCollection.Add(new Result("MPIResults", results as IItem));
|
---|
97 |
|
---|
98 | resultCollection["MPIResults"].Value = results as IItem;
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 | }
|
---|
103 | });
|
---|
104 | pollThread.Start();
|
---|
105 |
|
---|
106 | Process p = Process.Start(exec, args);
|
---|
107 | p.WaitForExit();
|
---|
108 |
|
---|
109 | pollThread.Abort();
|
---|
110 | File.Delete(algFile);
|
---|
111 | File.Delete(resultFile);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|