Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed some timeout related issues (#1542)

File size: 4.4 KB
RevLine 
[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
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;
[6394]34using System.Xml;
[6388]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);
[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}
Note: See TracBrowser for help on using the repository browser.