[6349] | 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;
|
---|
[6354] | 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;
|
---|
[6388] | 33 | using Microsoft.Hpc.Scheduler;
|
---|
| 34 | using System.ServiceModel;
|
---|
| 35 | using HeuristicLab.MPIAlgorithmRunner;
|
---|
| 36 | using HeuristicLab.Operators.MPISupport;
|
---|
[6394] | 37 | using Microsoft.Hpc.Scheduler.Properties;
|
---|
| 38 | using System.Xml;
|
---|
[6349] | 39 |
|
---|
| 40 | namespace HeuristicLab.MPIEngine {
|
---|
| 41 | /// <summary>
|
---|
| 42 | /// Represents an engine that executes its steps in parallel (if possible) using multiple threads.
|
---|
| 43 | /// This engine is suitable for parallel processing on shared memory systems which provide multiple cores.
|
---|
| 44 | /// </summary>
|
---|
| 45 | [StorableClass]
|
---|
| 46 | [Item("MPI Engine", "Engine for parallel execution of algorithms using multiple processes (suitable for distributed memory systems with multiple cores).")]
|
---|
| 47 | public class MPIEngine : Engine {
|
---|
| 48 | [StorableConstructor]
|
---|
| 49 | protected MPIEngine(bool deserializing) : base(deserializing) { }
|
---|
| 50 | protected MPIEngine(MPIEngine original, Cloner cloner) : base(original, cloner) { }
|
---|
| 51 | public MPIEngine() : base() { }
|
---|
| 52 |
|
---|
| 53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 54 | return new MPIEngine(this, cloner);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[6388] | 57 | private IAlgorithm algorithm;
|
---|
[6354] | 58 |
|
---|
| 59 | public override void Start() {
|
---|
| 60 | if (ExecutionStack.Count == 1) {
|
---|
| 61 | ExecutionContext context = ExecutionStack.First() as ExecutionContext;
|
---|
| 62 |
|
---|
| 63 | ExecutionContext algorithmContext = context.Parent as ExecutionContext;
|
---|
| 64 |
|
---|
| 65 | EngineAlgorithm alg = typeof(ExecutionContext).InvokeMember("parameterizedItem",
|
---|
| 66 | BindingFlags.GetField | BindingFlags.NonPublic |
|
---|
| 67 | BindingFlags.Instance, null, algorithmContext, null) as EngineAlgorithm;
|
---|
| 68 |
|
---|
| 69 | alg = alg.Clone() as EngineAlgorithm;
|
---|
| 70 | alg.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 71 |
|
---|
[6388] | 72 | algorithm = alg;
|
---|
[6354] | 73 | }
|
---|
| 74 |
|
---|
| 75 | base.Start();
|
---|
[6349] | 76 | }
|
---|
| 77 |
|
---|
[6394] | 78 | protected override void OnPaused() {
|
---|
| 79 | base.OnPaused();
|
---|
| 80 |
|
---|
| 81 | Stop();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[6354] | 84 | protected override void Run(System.Threading.CancellationToken cancellationToken) {
|
---|
[6394] | 85 | string username = @"user";
|
---|
| 86 | string password = @"password";
|
---|
| 87 | string headNode = "blade00.hpc.fh-hagenberg.at";
|
---|
| 88 | List<string> requestedNodes = new List<string>();
|
---|
| 89 | requestedNodes.Add("BLADE00");
|
---|
| 90 | string path = @"C:\public\MPISupport";
|
---|
| 91 | int cpuPerNode = 8;
|
---|
| 92 |
|
---|
[6354] | 93 | if (ExecutionStack.Count == 1) {
|
---|
| 94 | ExecutionContext context = ExecutionStack.Pop() as ExecutionContext;
|
---|
[6349] | 95 |
|
---|
[6354] | 96 | IScope globalScope = context.Scope;
|
---|
[6349] | 97 |
|
---|
[6388] | 98 | string exec = @"mpiexec";
|
---|
[6394] | 99 | string args = @"-c " + cpuPerNode + " /genvlist CCP_JOBID " + path + @"\HeuristicLab.MPIAlgorithmRunner-3.3.exe";
|
---|
[6354] | 100 |
|
---|
[6388] | 101 | IScheduler scheduler = new Scheduler();
|
---|
[6394] | 102 | scheduler.Connect(headNode);
|
---|
[6354] | 103 |
|
---|
[6388] | 104 | ISchedulerJob job = scheduler.CreateJob();
|
---|
| 105 | job.Name = "HeuristicLab.MPIEngine";
|
---|
[6394] | 106 | foreach (string requestedNode in requestedNodes)
|
---|
| 107 | job.RequestedNodes.Add(requestedNode);
|
---|
[6388] | 108 | ISchedulerTask task = job.CreateTask();
|
---|
| 109 | task.Name = "HeuristicLab.MPIAlgorithmRunner";
|
---|
| 110 | task.CommandLine = exec + " " + args;
|
---|
| 111 | task.StdOutFilePath = "stdout.txt";
|
---|
| 112 | task.StdErrFilePath = "stderr.txt";
|
---|
[6394] | 113 | task.WorkDirectory = path;
|
---|
[6388] | 114 | task.MinimumNumberOfCores = task.MaximumNumberOfCores = 3;
|
---|
| 115 | job.AddTask(task);
|
---|
[6354] | 116 |
|
---|
[6394] | 117 | scheduler.SubmitJob(job, username, "");
|
---|
[6354] | 118 |
|
---|
[6388] | 119 | try {
|
---|
| 120 | string address = null;
|
---|
| 121 | int timeout = 10;
|
---|
[6354] | 122 |
|
---|
[6388] | 123 | while (address == null && timeout > 0) {
|
---|
[6394] | 124 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 125 |
|
---|
[6388] | 126 | ISchedulerJob schedulerJob = scheduler.OpenJob(job.Id);
|
---|
| 127 | if (schedulerJob != null) {
|
---|
[6394] | 128 | NameValue property = schedulerJob.GetCustomProperties().FirstOrDefault(p => p.Name == "address");
|
---|
| 129 |
|
---|
| 130 | if (property != null) {
|
---|
| 131 | address = property.Value;
|
---|
[6388] | 132 | } else {
|
---|
| 133 | System.Threading.Thread.Sleep(1000);
|
---|
| 134 | timeout--;
|
---|
[6349] | 135 | }
|
---|
[6394] | 136 | }
|
---|
[6349] | 137 | }
|
---|
| 138 |
|
---|
[6388] | 139 | if (address == null) {
|
---|
| 140 | throw new Exception("A timeout occurred when starting the MPIAlgorithmRunner");
|
---|
| 141 | }
|
---|
[6354] | 142 |
|
---|
[6388] | 143 | NetTcpBinding netTCPBinding = new NetTcpBinding(SecurityMode.None);
|
---|
[6394] | 144 | XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
|
---|
| 145 | quotas.MaxArrayLength = int.MaxValue;
|
---|
| 146 | netTCPBinding.ReaderQuotas = quotas;
|
---|
| 147 | netTCPBinding.MaxReceivedMessageSize = int.MaxValue;
|
---|
[6388] | 148 | ChannelFactory<IAlgorithmBroker> factory = new ChannelFactory<IAlgorithmBroker>(netTCPBinding, address);
|
---|
| 149 | IAlgorithmBroker proxy = factory.CreateChannel();
|
---|
| 150 |
|
---|
| 151 | proxy.TransmitAlgorithm(new MPITransportWrapper<IAlgorithm>(algorithm));
|
---|
| 152 |
|
---|
| 153 | while (!proxy.IsAlgorithmTerminated()) {
|
---|
[6394] | 154 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 155 |
|
---|
[6388] | 156 | ItemList<ResultCollection> results = proxy.GetResults().InnerItem;
|
---|
| 157 |
|
---|
| 158 | ResultCollection resultCollection = (globalScope.Variables["Results"].Value as ResultCollection);
|
---|
| 159 |
|
---|
| 160 | if (resultCollection != null && results != null) {
|
---|
| 161 | if (!resultCollection.ContainsKey("MPIResults"))
|
---|
| 162 | resultCollection.Add(new Result("MPIResults", results));
|
---|
| 163 |
|
---|
| 164 | resultCollection["MPIResults"].Value = results;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | System.Threading.Thread.Sleep(5000);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | catch (Exception e) {
|
---|
[6394] | 171 | scheduler.CancelJob(job.Id, "Exception: " + e.GetType());
|
---|
[6388] | 172 | throw e;
|
---|
| 173 | }
|
---|
[6394] | 174 | finally {
|
---|
| 175 | /*ISchedulerJob schedulerJob = scheduler.OpenJob(job.Id);
|
---|
| 176 | if (schedulerJob != null &&
|
---|
| 177 | (schedulerJob.State == JobState.Running || schedulerJob.State == JobState.Queued)) {
|
---|
| 178 | scheduler.CancelJob(job.Id, "Cancelled");
|
---|
| 179 | } */
|
---|
| 180 | }
|
---|
[6349] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|