Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/HeuristicLab.MPIAlgorithmRunner/3.3/AlgorithmBroker.cs @ 6394

Last change on this file since 6394 was 6394, checked in by svonolfe, 13 years ago

Added first working version of the MPI engine (#1542)

File size: 4.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Net;
27using System.Net.Sockets;
28using System.ServiceModel;
29using System.ServiceModel.Description;
30using HeuristicLab.Operators.MPISupport;
31using HeuristicLab.Optimization;
32using HeuristicLab.Core;
33using System.Threading;
34using System.Xml;
35                     
36namespace 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);
56      netTCPBinding.MaxReceivedMessageSize = int.MaxValue;
57      XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
58      quotas.MaxArrayLength = int.MaxValue;
59      netTCPBinding.ReaderQuotas = quotas;
60      endpoint.Binding = netTCPBinding;
61      int port = FindFreeTcpPort();
62      endpoint.Address = new EndpointAddress("net.tcp://localhost:" + port + "/AlgorithmBroker");
63      service.AddServiceEndpoint(endpoint);
64      ServiceDebugBehavior debug = service.Description.Behaviors.Find<ServiceDebugBehavior>();
65      debug.IncludeExceptionDetailInFaults = true;
66
67      return service;
68    }
69
70    public static ServiceHost StartService(MPI.Communicator communicator) {
71      try {
72        ServiceHost service = CreateService(communicator);
73
74        service.Open();
75
76        return service;
77      }
78      catch (AddressAlreadyInUseException) {
79        return StartService(communicator);
80      }
81    }
82
83    private MPI.Communicator communicator;
84
85    public AlgorithmBroker(MPI.Communicator communicator) {
86      this.communicator = communicator;
87      ExitWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
88    }
89
90    #region IAlgorithmBroker Members
91
92    public void TransmitAlgorithm(MPITransportWrapper<IAlgorithm> algorithm) {
93      Console.WriteLine("Transmitting Algorithm...");
94      int clients = MPI.Communicator.world.Group.Size - 1;
95
96      for(int i = 0; i < clients; i++) {
97        int client = i + 1;
98        communicator.Send<MPITransportWrapper<IAlgorithm>>(algorithm, client, 0);
99      }
100    }
101
102    public ItemList<ResultCollection> Results { get; set; }
103
104    public MPITransportWrapper<ItemList<ResultCollection>> GetResults() {
105      Console.WriteLine("Transmitting Results...");
106      return new MPITransportWrapper<ItemList<ResultCollection>>(Results);
107    }
108
109    public EventWaitHandle ExitWaitHandle { get; private set; }
110
111    public bool Terminated { get; set; }
112
113    public bool IsAlgorithmTerminated() {
114      lock (ExitWaitHandle) {
115        if (Terminated)
116          ExitWaitHandle.Set();
117        return Terminated;
118      }
119    }
120    #endregion
121  }
122}
Note: See TracBrowser for help on using the repository browser.