[6388] | 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 System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Net;
|
---|
| 27 | using System.Net.Sockets;
|
---|
| 28 | using System.ServiceModel;
|
---|
| 29 | using System.ServiceModel.Description;
|
---|
| 30 | using HeuristicLab.Operators.MPISupport;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Core;
|
---|
| 33 | using System.Threading;
|
---|
[6394] | 34 | using System.Xml;
|
---|
[6388] | 35 |
|
---|
| 36 | namespace HeuristicLab.MPIAlgorithmRunner {
|
---|
| 37 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
---|
| 38 | class AlgorithmBroker: IAlgorithmBroker {
|
---|
| 39 | private static int FindFreeTcpPort() {
|
---|
| 40 | TcpListener l = new TcpListener(IPAddress.Parse("127.0.0.1"), 0);
|
---|
| 41 | l.Start();
|
---|
| 42 | int port = ((IPEndPoint)l.LocalEndpoint).Port;
|
---|
| 43 | l.Stop();
|
---|
| 44 | return port;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private static ServiceHost CreateService(MPI.Communicator communicator) {
|
---|
| 48 | AlgorithmBroker broker = new AlgorithmBroker(communicator);
|
---|
| 49 | ServiceHost service = new ServiceHost(broker);
|
---|
| 50 |
|
---|
| 51 | ContractDescription contract = ContractDescription.GetContract(typeof(IAlgorithmBroker));
|
---|
| 52 | contract.ContractType = typeof(IAlgorithmBroker);
|
---|
| 53 | ServiceEndpoint endpoint = new ServiceEndpoint(contract);
|
---|
| 54 | endpoint.Name = "AlgorithmBrokerEndpoint";
|
---|
| 55 | NetTcpBinding netTCPBinding = new NetTcpBinding(SecurityMode.None);
|
---|
[6394] | 56 | netTCPBinding.MaxReceivedMessageSize = int.MaxValue;
|
---|
| 57 | XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
|
---|
| 58 | quotas.MaxArrayLength = int.MaxValue;
|
---|
| 59 | netTCPBinding.ReaderQuotas = quotas;
|
---|
[6400] | 60 | netTCPBinding.SendTimeout = new TimeSpan(600000000);
|
---|
| 61 | netTCPBinding.ReceiveTimeout = new TimeSpan(600000000);
|
---|
[6388] | 62 | endpoint.Binding = netTCPBinding;
|
---|
| 63 | int port = FindFreeTcpPort();
|
---|
| 64 | endpoint.Address = new EndpointAddress("net.tcp://localhost:" + port + "/AlgorithmBroker");
|
---|
| 65 | service.AddServiceEndpoint(endpoint);
|
---|
[6394] | 66 | ServiceDebugBehavior debug = service.Description.Behaviors.Find<ServiceDebugBehavior>();
|
---|
| 67 | debug.IncludeExceptionDetailInFaults = true;
|
---|
[6388] | 68 |
|
---|
| 69 | return service;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public static ServiceHost StartService(MPI.Communicator communicator) {
|
---|
| 73 | try {
|
---|
| 74 | ServiceHost service = CreateService(communicator);
|
---|
| 75 |
|
---|
| 76 | service.Open();
|
---|
| 77 |
|
---|
| 78 | return service;
|
---|
| 79 | }
|
---|
| 80 | catch (AddressAlreadyInUseException) {
|
---|
| 81 | return StartService(communicator);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private MPI.Communicator communicator;
|
---|
| 86 |
|
---|
| 87 | public AlgorithmBroker(MPI.Communicator communicator) {
|
---|
| 88 | this.communicator = communicator;
|
---|
| 89 | ExitWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | #region IAlgorithmBroker Members
|
---|
| 93 |
|
---|
[6398] | 94 | public void TransmitAlgorithm(MPITransportWrapper<IAlgorithm> algorithm, int updateInterval) {
|
---|
[6388] | 95 | Console.WriteLine("Transmitting Algorithm...");
|
---|
[6394] | 96 | int clients = MPI.Communicator.world.Group.Size - 1;
|
---|
[6388] | 97 |
|
---|
| 98 | for(int i = 0; i < clients; i++) {
|
---|
| 99 | int client = i + 1;
|
---|
| 100 | communicator.Send<MPITransportWrapper<IAlgorithm>>(algorithm, client, 0);
|
---|
[6398] | 101 | communicator.Send<int>(updateInterval, client, 1);
|
---|
[6388] | 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public ItemList<ResultCollection> Results { get; set; }
|
---|
| 106 |
|
---|
| 107 | public MPITransportWrapper<ItemList<ResultCollection>> GetResults() {
|
---|
| 108 | Console.WriteLine("Transmitting Results...");
|
---|
| 109 | return new MPITransportWrapper<ItemList<ResultCollection>>(Results);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public EventWaitHandle ExitWaitHandle { get; private set; }
|
---|
| 113 |
|
---|
| 114 | public bool Terminated { get; set; }
|
---|
| 115 |
|
---|
| 116 | public bool IsAlgorithmTerminated() {
|
---|
| 117 | lock (ExitWaitHandle) {
|
---|
| 118 | if (Terminated)
|
---|
| 119 | ExitWaitHandle.Set();
|
---|
| 120 | return Terminated;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 | }
|
---|
| 125 | }
|
---|