Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/Program.cs @ 6388

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

Worked on the MPIEngine (#1542)

File size: 5.5 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 HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.Xml;
28using System.Threading;
29using HeuristicLab.Operators.MPISupport;
30using HeuristicLab.Core;
31using Microsoft.Hpc.Scheduler;
32using System.ServiceModel;
33using System.Net;
34
35namespace HeuristicLab.MPIAlgorithmRunner {
36  class Program {
37    EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
38
39    private static void SetJobStatus(string address) {
40      Console.WriteLine("Started service... at " + address);
41
42      // Discover the job's context from the environment
43      String headNodeName = System.Environment.GetEnvironmentVariable("CCP_CLUSTER_NAME");
44      int jobId = System.Convert.ToInt32(System.Environment.GetEnvironmentVariable("CCP_JOBID"));
45
46      // Connect to the head node and get the job
47      IScheduler scheduler = new Scheduler();
48      scheduler.Connect(headNodeName);
49      ISchedulerJob job = scheduler.OpenJob(jobId);
50
51      job.SetCustomProperty("address", address);
52
53      job.Commit();
54    }
55
56    private static string GetAddress(ServiceHost service) {
57      IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
58      string address = "net.tcp://" + IPHost.AddressList[0].ToString() + ":" + service.ChannelDispatchers[0].Listener.Uri.Port + "/AlgorithmBroker";
59
60      return address;
61    }
62
63    static void Main(string[] args) {
64      using (new MPI.Environment(ref args)) {
65        int clients = MPI.Communicator.world.Group.Size;
66        int updateInterval = 5000;
67
68        MPI.Communicator communicator = MPI.Communicator.world.Clone() as MPI.Communicator;
69
70        if (communicator.Rank == 0) {
71          ServiceHost service = AlgorithmBroker.StartService(communicator);
72          AlgorithmBroker broker = (service.SingletonInstance as AlgorithmBroker);
73
74          string address = GetAddress(service);
75          SetJobStatus(address);
76
77          ItemList<ResultCollection> results = new ItemList<ResultCollection>();
78         
79          bool[] finished = new bool[clients];
80          int finishedCount = 0;
81
82          while (finishedCount != clients) {
83            for (int i = 0; i < clients; i++) {
84              if (!finished[i]) {
85                int client = i + 1;
86                ResultCollection result = communicator.Receive<MPITransportWrapper<ResultCollection>>(client, 1).InnerItem;
87
88                Console.WriteLine("Received result " + result);
89
90                if (results.Count != clients) {
91                  results.Add(result);
92                } else {
93                  results[i] = result;
94                }
95
96                Console.WriteLine("Probing...");
97                if (communicator.ImmediateProbe(client, 2) != null) {
98                  finished[i] = true;
99                  finishedCount++;
100                }
101              }
102            }
103
104            Console.WriteLine("Update results");
105            broker.Results = results;
106          }
107
108          lock (broker.ExitWaitHandle) {
109            broker.Terminated = true;
110          }
111
112          broker.ExitWaitHandle.WaitOne();
113          Console.WriteLine("Finished.");
114          service.Close();
115        } else {
116          Program p = new Program();
117          p.StartAlgorithm(updateInterval, communicator);
118        }
119      }
120    }
121
122    public void StartAlgorithm(int updateInterval, MPI.Communicator communicator) {
123      IAlgorithm alg = communicator.Receive<MPITransportWrapper<IAlgorithm>>(0, 0).InnerItem;
124
125      Console.WriteLine("Starting algorithm...");
126
127      alg.Stopped += new EventHandler(algorithm_Stopped);
128      waitHandle.Reset();
129
130      alg.Prepare(true);
131      alg.Start();
132
133      Timer t = new Timer(delegate(object state) {
134        if (alg.ExecutionState == ExecutionState.Started) {
135          Console.WriteLine("Pausing alg...");
136          alg.Pause();
137
138          while (alg.ExecutionState != ExecutionState.Paused) {
139            Thread.Sleep(100);
140          }
141
142          communicator.Send<MPITransportWrapper<ResultCollection>>(
143            new MPITransportWrapper<ResultCollection>(alg.Results), 0, 1);
144
145          Console.WriteLine("Sending update...");
146
147          Console.WriteLine("Resuming alg...");
148          alg.Start();
149        }
150      }, null, updateInterval, updateInterval);
151
152      waitHandle.WaitOne();
153
154      communicator.Send<int>(communicator.Rank, 0, 2);
155      communicator.Send<MPITransportWrapper<ResultCollection>>(
156          new MPITransportWrapper<ResultCollection>(alg.Results), 0, 1);
157    }
158
159    void algorithm_Stopped(object sender, EventArgs e) {
160      waitHandle.Set();
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.