]> AND Private Git Repository - loba.git/blob - process.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
9f46f8f8d0c1f067cf34b725747771c063b8740c
[loba.git] / process.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <iterator>
4 #include <numeric>
5 #include <stdexcept>
6 #include <sstream>
7 #include <xbt/log.h>
8 #include <xbt/time.h>
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
11
12 #include "misc.h"
13 #include "options.h"
14
15 #include "process.h"
16
17 double process::total_load_init = 0.0;
18 double process::total_load_running = 0.0;
19 double process::total_load_exit = 0.0;
20
21 process::process(int argc, char* argv[])
22 {
23     if (argc < 2 || !(std::istringstream(argv[1]) >> load))
24         throw std::invalid_argument("bad or missing initial load parameter");
25
26     neigh.assign(argv + 2, argv + argc);
27
28     pneigh.reserve(neigh.size());
29     for (unsigned i = 0 ; i < neigh.size() ; i++) {
30         neighbor* ptr = &neigh[i];
31         m_host_t host = MSG_get_host_by_name(ptr->get_name());
32         pneigh.push_back(ptr);
33         rev_neigh.insert(std::make_pair(host, ptr));
34     }
35
36     prev_load_broadcast = -1;   // force sending of load on first send()
37     expected_load = load;
38     total_load_running += load;
39     total_load_init += load;
40
41     ctrl_close_pending = data_close_pending = neigh.size();
42     close_received = false;
43     finalizing = false;
44
45     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
46     if (!LOG_ISENABLED(logp))
47         return;
48     std::ostringstream oss;
49     oss << neigh.size() << " neighbor";
50     if (!neigh.empty()) {
51         oss << ESSE(neigh.size()) << ": ";
52         std::transform(neigh.begin(), neigh.end() - 1,
53                        std::ostream_iterator<const char*>(oss, ", "),
54                        std::tr1::mem_fn(&neighbor::get_name));
55         oss << neigh.back().get_name();
56     }
57     LOG1(logp, "Got %s.", oss.str().c_str());
58     print_loads(false, logp);
59 }
60
61 process::~process()
62 {
63     total_load_exit += load;
64 }
65
66 int process::run()
67 {
68     double next_iter_after_date = 0.0;
69     INFO1("Initial load: %g", load);
70     VERB0("Starting...");
71     comp_iter = lb_iter = 0;
72     while (true) {
73         if (opt::min_iter_duration) {
74             double now = MSG_get_clock();
75             if (now < next_iter_after_date){
76                 double delay = next_iter_after_date - now;
77                 DEBUG1("sleeping for %g s", delay);
78                 MSG_process_sleep(next_iter_after_date - now);
79             }
80             next_iter_after_date = MSG_get_clock() + opt::min_iter_duration;
81         }
82
83         double ld = lb_load();
84         if (ld > 0.0) {
85             ++lb_iter;
86
87             if (opt::log_rate && lb_iter % opt::log_rate == 0) {
88                 if (opt::bookkeeping)
89                     INFO4("(%u:%u) current load: %g ; expected: %g",
90                           lb_iter, comp_iter, load, expected_load);
91                 else
92                     INFO2("(%u) current load: %g",
93                           lb_iter, load);
94             }
95
96             ld -= load_balance(ld);
97
98             print_loads(true, xbt_log_priority_debug);
99         }
100         lb_load() = ld;
101
102         // send load information, and load (data) if any
103         send();
104         if (load > 0.0) {
105             ++comp_iter;
106             compute();
107         }
108
109         if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
110             VERB2("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
111             break;
112         }
113         if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
114             VERB2("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
115             break;
116         }
117         if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
118             VERB2("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
119             break;
120         }
121
122         // block on receiving unless there is something to compute or
123         // to send
124         bool wait = (load == 0 && lb_load() == prev_load_broadcast);
125         receive(wait);
126
127         // one of our neighbor is finalizing
128         if (opt::exit_on_close && close_received) {
129             VERB0("Close received");
130             break;
131         }
132
133         // have no load and cannot receive anything
134         if (load == 0.0 && !may_receive()) {
135             VERB0("I'm a poor lonesome process, and I have no load...");
136             break;
137         }
138
139         // fixme: this check should be implemented with a distributed
140         // algorithm, and not a shared global variable!
141         // fixme: should this chunk be moved before call to receive() ?
142         if (100.0 * total_load_running / total_load_init <=
143             opt::load_ratio_threshold) {
144             VERB0("No more load to balance in system.");
145             break;
146         }
147     }
148     VERB0("Going to finalize...");
149     finalize();
150
151     /* Open Questions :
152      * - definition of load on heterogeneous hosts ?
153      * - how to detect convergence ?
154      * - how to manage link failures ?
155      */
156
157     VERB0("Done.");
158     if (opt::bookkeeping) {
159         INFO4("Final load after %d:%d iterations: %g ; expected: %g",
160               lb_iter, comp_iter, load, expected_load);
161     } else {
162         INFO2("Final load after %d iterations: %g",
163               lb_iter, load);
164         if (lb_iter != comp_iter)
165             WARN2("lb_iter (%d) and comp_iter (%d) differ!",
166                   lb_iter, comp_iter);
167     }
168     return 0;
169 }
170
171 double process::sum_of_to_send() const
172 {
173     using std::tr1::bind;
174     using std::tr1::placeholders::_1;
175     using std::tr1::placeholders::_2;
176
177     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
178                            bind(std::plus<double>(),
179                                 _1, bind(&neighbor::get_to_send, _2)));
180 }
181
182 double process::load_balance(double /*my_load*/)
183 {
184     if (lb_iter == 1)           // warn only once
185         WARN0("process::load_balance() is a no-op!");
186     return 0.0;
187 }
188
189 void process::compute()
190 {
191     if (load > 0.0) {
192         double duration = opt::comp_cost(load);
193         m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
194         DEBUG2("compute %g flop%s", duration, ESSE(duration));
195         MSG_task_execute(task);
196         MSG_task_destroy(task);
197     } else {
198         DEBUG0("nothing to compute !");
199     }
200 }
201
202 void process::send1_no_bookkeeping(neighbor& nb)
203 {
204     if (load != prev_load_broadcast)
205         comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
206     double load_to_send = nb.get_to_send();
207     if (load_to_send > 0.0) {
208         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
209         nb.set_to_send(0.0);
210     }
211 }
212
213 void process::send1_bookkeeping(neighbor& nb)
214 {
215     if (expected_load != prev_load_broadcast)
216         comm.send(nb.get_ctrl_mbox(),
217                   new message(message::INFO, expected_load));
218     double load_to_send;
219     double new_debt;
220     double debt_to_send = nb.get_to_send();
221     if (debt_to_send > 0.0) {
222         comm.send(nb.get_ctrl_mbox(),
223                   new message(message::CREDIT, debt_to_send));
224         nb.set_to_send(0.0);
225         new_debt = nb.get_debt() + debt_to_send;
226     } else {
227         new_debt = nb.get_debt();
228     }
229     if (load <= new_debt) {
230         load_to_send = load;
231         nb.set_debt(new_debt - load_to_send);
232         load = 0.0;
233     } else {
234         load_to_send = new_debt;
235         nb.set_debt(0.0);
236         load -= load_to_send;
237     }
238     if (load_to_send > 0.0)
239         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
240 }
241
242 void process::send()
243 {
244     using std::tr1::bind;
245     using std::tr1::placeholders::_1;
246
247     if (opt::bookkeeping) {
248         std::for_each(neigh.begin(), neigh.end(),
249                       bind(&process::send1_bookkeeping, this, _1));
250         prev_load_broadcast = expected_load;
251     } else {
252         std::for_each(neigh.begin(), neigh.end(),
253                       bind(&process::send1_no_bookkeeping, this, _1));
254         prev_load_broadcast = load;
255     }
256     comm.flush(false);
257 }
258
259 void process::receive(bool wait)
260 {
261     message* msg;
262     m_host_t from;
263
264     while (may_receive() && comm.recv(msg, from, wait)) {
265         switch (msg->get_type()) {
266         case message::INFO: {
267             neighbor* n = rev_neigh[from];
268             n->set_load(msg->get_amount());
269             break;
270         }
271         case message::CREDIT:
272             expected_load += msg->get_amount();
273             break;
274         case message::LOAD: {
275             double ld = msg->get_amount();
276             load += ld;
277             if (finalizing)
278                 total_load_running -= ld;
279             break;
280         }
281         case message::CTRL_CLOSE:
282             ctrl_close_pending--;
283             close_received = true;
284             break;
285         case message::DATA_CLOSE:
286             data_close_pending--;
287             close_received = true;
288             break;
289         }
290         delete msg;
291         wait = false;           // only wait on first recv
292     }
293     comm.flush(false);
294 }
295
296 void process::finalize1(neighbor& nb)
297 {
298     comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
299     comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
300 }
301
302 void process::finalize()
303 {
304     using std::tr1::bind;
305     using std::tr1::placeholders::_1;
306
307     finalizing = true;
308     total_load_running -= load;
309
310     DEBUG2("send CLOSE to %lu neighbor%s",
311            (unsigned long )neigh.size(), ESSE(neigh.size()));
312     std::for_each(neigh.begin(), neigh.end(),
313                   bind(&process::finalize1, this, _1));
314
315     DEBUG2("wait for CLOSE from %lu neighbor%s",
316            (unsigned long )neigh.size(), ESSE(neigh.size()));
317     while (may_receive()) {
318         comm.flush(false);
319         receive(true);
320     }
321
322     comm.flush(true);
323 }
324
325 #define print_loads_generic(vec, verbose, logp, cat)                    \
326     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
327         using std::tr1::bind;                                           \
328         using std::tr1::placeholders::_1;                               \
329         XCLOG0(cat, logp, "Neighbor loads:");                           \
330         std::for_each(vec.begin(), vec.end(),                           \
331                       bind(&neighbor::print, _1, verbose, logp, cat));  \
332     } else ((void)0)
333
334 void process::print_loads(bool verbose,
335                           e_xbt_log_priority_t logp,
336                           xbt_log_category_t cat) const
337 {
338     print_loads_generic(neigh, verbose, logp, cat);
339 }
340
341 void process::print_loads_p(bool verbose,
342                             e_xbt_log_priority_t logp,
343                             xbt_log_category_t cat) const
344 {
345     print_loads_generic(pneigh, verbose, logp, cat);
346 }
347
348 #undef print_loads_generic
349
350 // Local variables:
351 // mode: c++
352 // End: