Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/MPIAlgorithmRunner/3.3/AlgorithmBroker.cs @ 6391

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

Worked on the MPIEngine (#1542)

File size: 3.9 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;
34                     
35namespace HeuristicLab.MPIAlgorithmRunner {
36  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
37  class AlgorithmBroker: IAlgorithmBroker {
38    private static int FindFreeTcpPort() {
39      TcpListener l = new TcpListener(IPAddress.Parse("127.0.0.1"), 0);
40      l.Start();
41      int port = ((IPEndPoint)l.LocalEndpoint).Port;
42      l.Stop();
43      return port;
44    }
45
46    private static ServiceHost CreateService(MPI.Communicator communicator) {
47      AlgorithmBroker broker = new AlgorithmBroker(communicator);
48      ServiceHost service = new ServiceHost(broker);
49
50      ContractDescription contract = ContractDescription.GetContract(typeof(IAlgorithmBroker));
51      contract.ContractType = typeof(IAlgorithmBroker);
52      ServiceEndpoint endpoint = new ServiceEndpoint(contract);
53      endpoint.Name = "AlgorithmBrokerEndpoint";
54      NetTcpBinding netTCPBinding = new NetTcpBinding(SecurityMode.None);
55      endpoint.Binding = netTCPBinding;
56      int port = FindFreeTcpPort();
57      endpoint.Address = new EndpointAddress("net.tcp://localhost:" + port + "/AlgorithmBroker");
58      service.AddServiceEndpoint(endpoint);
59
60      return service;
61    }
62
63    public static ServiceHost StartService(MPI.Communicator communicator) {
64      try {
65        ServiceHost service = CreateService(communicator);
66
67        service.Open();
68
69        return service;
70      }
71      catch (AddressAlreadyInUseException) {
72        return StartService(communicator);
73      }
74    }
75
76    private MPI.Communicator communicator;
77
78    public AlgorithmBroker(MPI.Communicator communicator) {
79      this.communicator = communicator;
80      ExitWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
81    }
82
83    #region IAlgorithmBroker Members
84
85    public void TransmitAlgorithm(MPITransportWrapper<IAlgorithm> algorithm) {
86      Console.WriteLine("Transmitting Algorithm...");
87      int clients = MPI.Communicator.world.Group.Size;
88
89      for(int i = 0; i < clients; i++) {
90        int client = i + 1;
91        communicator.Send<MPITransportWrapper<IAlgorithm>>(algorithm, client, 0);
92      }
93    }
94
95    public ItemList<ResultCollection> Results { get; set; }
96
97    public MPITransportWrapper<ItemList<ResultCollection>> GetResults() {
98      Console.WriteLine("Transmitting Results...");
99      return new MPITransportWrapper<ItemList<ResultCollection>>(Results);
100    }
101
102    public EventWaitHandle ExitWaitHandle { get; private set; }
103
104    public bool Terminated { get; set; }
105
106    public bool IsAlgorithmTerminated() {
107      lock (ExitWaitHandle) {
108        if (Terminated)
109          ExitWaitHandle.Set();
110        return Terminated;
111      }
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.