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

Private GIT Repository
48dac1dd72ea65fa78bedfa168e81d20a3bb762e
[loba.git] / process.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include <functional>
4 #include <iterator>
5 #include <numeric>
6 #include <stdexcept>
7 #include <sstream>
8 #include <xbt/log.h>
9 #include <xbt/time.h>
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
12
13 #include "misc.h"
14 #include "options.h"
15 #include "tracing.h"
16
17 #include "process.h"
18
19 double process::total_load_init = 0.0;
20 double process::total_load_running = 0.0;
21 double process::total_load_exit = 0.0;
22
23 int process::process_counter = 0;
24 double process::total_load_average;
25 double process::load_diff_threshold;
26
27 namespace {
28
29     void sleep_until_date(double& date, double duration)
30     {
31         double sleep_duration = date - MSG_get_clock();
32         if (sleep_duration > 0.0)
33             MSG_process_sleep(sleep_duration);
34         date = MSG_get_clock() + duration;
35     }
36
37 }
38
39 process::process(int argc, char* argv[])
40 {
41     if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
42         throw std::invalid_argument("bad or missing initial load parameter");
43
44     double iload = std::trunc(real_load);
45     if (opt::integer_transfer && real_load != iload) {
46         XBT_WARN("Initial load %g is not an integer.  Truncate it.",
47                  real_load);
48         real_load = iload;
49     }
50
51     neigh.assign(argv + 2, argv + argc);
52
53     pneigh.reserve(neigh.size());
54     for (unsigned i = 0 ; i < neigh.size() ; i++) {
55         neighbor* ptr = &neigh[i];
56         m_host_t host = MSG_get_host_by_name(ptr->get_name());
57         pneigh.push_back(ptr);
58         rev_neigh.insert(std::make_pair(host, ptr));
59     }
60
61     // Note: there should not be race condition with the current
62     // version of Simgrid, when updating the global variables.
63
64     prev_load_broadcast = -1;   // force sending of load on first send_all()
65     expected_load = real_load;
66     total_load_running += real_load;
67     total_load_init += real_load;
68     received_load = 0.0;
69
70     idle_duration = 0.0;
71     convergence = -1.0;
72
73     process_counter++;
74     total_load_average = total_load_running / process_counter;
75     load_diff_threshold = (opt::load_ratio_threshold +
76                            opt::avg_load_ratio * total_load_average) / 100.0;
77
78     ctrl_close_pending = data_close_pending = neigh.size();
79     close_received = false;
80     finalizing = false;
81
82     all_comp_iter = comp_iter = lb_iter = 0;
83
84     lb_thread = new_msg_thread("loba",
85                                std::bind(&process::load_balance_loop, this));
86
87     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
88     if (!LOG_ISENABLED(logp))
89         return;
90     std::ostringstream oss;
91     oss << neigh.size() << " neighbor";
92     if (!neigh.empty()) {
93         oss << ESSE(neigh.size()) << ": ";
94         std::transform(neigh.begin(), neigh.end() - 1,
95                        std::ostream_iterator<const char*>(oss, ", "),
96                        std::mem_fn(&neighbor::get_name));
97         oss << neigh.back().get_name();
98     }
99     XBT_LOG(logp, "Got %s.", oss.str().c_str());
100     print_loads(false, logp);
101 }
102
103 process::~process()
104 {
105     delete lb_thread;
106     total_load_exit += real_load;
107     xbt_assert(received_load == 0.0,
108                "received_load is %g, but should be 0.0 !", received_load);
109     if (opt::log_rate < 0)
110         return;
111     XBT_INFO("Final load after %d:%d:%d iterations: %g",
112              lb_iter, comp_iter, all_comp_iter, real_load);
113     if (convergence >= 0.0)
114         XBT_INFO("Convergence within %g%% was achieved at time %g",
115                  opt::avg_load_ratio, convergence);
116     else
117         XBT_INFO("Convergence within %g%% was not achieved",
118                  opt::avg_load_ratio);
119     XBT_VERB("Expected load was: %g", expected_load);
120     XBT_VERB("Total computation for this process: %g", get_comp_amount());
121     print_loads(true, xbt_log_priority_debug);
122 }
123
124 double process::get_iter_deviation() const
125 {
126     double average_cost = opt::comp_cost(total_load_average);
127     // Do not count idle periods
128     double comp_iter_opt = acc.comp_amount / average_cost;
129 /*
130     // Add iterations that could have been achieved while beeing idle
131     // (kept for documentation)
132     double self_speed = MSG_get_host_speed(MSG_host_self());
133     double average_duration = average_cost / self_speed;
134     comp_iter_opt += idle_duration / average_duration;
135 */
136     return comp_iter - comp_iter_opt;
137 }
138
139 int process::run()
140 {
141     if (opt::log_rate >= 0) {
142         XBT_INFO("Initial load: %g", real_load);
143         XBT_VERB("Initial expected load: %g", expected_load);
144     }
145     XBT_VERB("Starting...");
146     mutex.acquire();
147     lb_thread->start();
148     while (lb_iter <= opt::comp_iter_delay)
149         cond.wait(mutex);
150     mutex.release();
151     double sleep_duration = opt::comp_time_delay - MSG_get_clock();
152     if (sleep_duration > 0.0)
153         MSG_process_sleep(sleep_duration);
154     compute_loop();
155     lb_thread->wait();
156     XBT_VERB("Done.");
157     return 0;
158 }
159
160 void process::load_balance_loop()
161 {
162     using std::placeholders::_1;
163
164     double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
165     while (still_running()) {
166         if (lb_iter == opt::comp_iter_delay) {
167             mutex.acquire();
168             ++lb_iter;
169             cond.signal();
170             mutex.release();
171         } else {
172             ++lb_iter;
173         }
174
175         ctrl_receive(0.0);
176
177         mutex.acquire();
178         if (!opt::bookkeeping)
179             expected_load = real_load - get_sum_of_to_send();
180         // nothing to do with opt::bookkeeping
181
182         if (opt::log_rate && lb_iter % opt::log_rate == 0) {
183             XBT_INFO("(%u:%u:%u) current load: %g",
184                      lb_iter, comp_iter, all_comp_iter, real_load);
185             XBT_VERB("... expected load: %g", expected_load);
186         }
187
188         if (expected_load > 0.0)
189             load_balance();
190
191         print_loads(true, xbt_log_priority_debug);
192
193         // send
194         comm.ctrl_flush(false);
195         std::for_each(neigh.begin(), neigh.end(),
196                       std::bind(&process::ctrl_send, this, _1));
197         prev_load_broadcast = expected_load;
198         mutex.release();
199
200         sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
201     }
202
203     XBT_VERB("Going to finalize for %s...", __func__);
204     XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
205               neigh.size(), ESSE(neigh.size()));
206     std::for_each(neigh.begin(), neigh.end(),
207                   std::bind(&process::ctrl_close, this, _1));
208     while (ctrl_close_pending) {
209         comm.ctrl_flush(false);
210         XBT_DEBUG("waiting for %d CTRL_CLOSE", ctrl_close_pending);
211         ctrl_receive(-1.0);
212     }
213     comm.ctrl_flush(true);
214 }
215
216 void process::compute_loop()
217 {
218     using std::placeholders::_1;
219
220     double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
221     double idle_since_date = 0.0;
222     while (still_running()) {
223
224         do {
225             // receive
226             // if there is something to compute, do not block
227             // else, block the duration of an *lb* iteration
228             data_receive(real_load > 0.0 ? 0.0 : opt::min_lb_iter_duration);
229
230             // send
231             comm.data_flush(false);
232             mutex.acquire();
233             real_load += received_load;
234             received_load = 0.0;
235             std::for_each(neigh.begin(), neigh.end(),
236                           std::bind(&process::data_send, this, _1));
237             mutex.release();
238
239             ++all_comp_iter;
240
241         } while (real_load == 0.0);
242
243         convergence_check();
244
245         // compute
246         idle_duration += MSG_get_clock() - idle_since_date;
247         ++comp_iter;
248         double flops = opt::comp_cost(real_load);
249         m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
250         TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
251         XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
252         MSG_task_execute(task);
253         add_comp_amount(flops);
254         MSG_task_destroy(task);
255
256         idle_since_date = MSG_get_clock();
257
258         sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
259     }
260
261     XBT_VERB("Going to finalize for %s...", __func__);
262     // Note: idle duration is not counted during finalization
263     finalizing = true;
264     XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
265               neigh.size(), ESSE(neigh.size()));
266     std::for_each(neigh.begin(), neigh.end(),
267                   std::bind(&process::data_close, this, _1));
268     while (data_close_pending) {
269         comm.data_flush(false);
270         XBT_DEBUG("waiting for %d DATA_CLOSE", data_close_pending);
271         data_receive(-1.0);
272     }
273     real_load += received_load;
274     received_load = 0.0;
275     total_load_running -= real_load;
276     convergence_check();
277     comm.data_flush(true);
278 }
279
280 void process::convergence_check()
281 {
282     double load_diff = std::fabs(real_load - total_load_average);
283     bool converged = load_diff <= load_diff_threshold;
284
285     if (convergence >= 0.0) {
286         if (!converged) {
287             XBT_VERB("current load has diverged: %g (%.4g%%)",
288                      real_load, 100.0 * load_diff / total_load_average);
289             convergence = -1.0;
290         }
291     } else {
292         if (converged) {
293             XBT_VERB("current load has converged: %g (%.4g%%)",
294                      real_load,  100.0 * load_diff / total_load_average);
295             convergence = MSG_get_clock();
296         }
297     }
298 }
299
300 bool process::still_running()
301 {
302     static bool last_status = true;
303
304     if (!last_status) {
305         /* nop */
306
307     } else if (opt::exit_request) {
308         XBT_VERB("Global exit requested");
309         last_status = false;
310
311     } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
312         XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
313         last_status = false;
314
315     } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
316         XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
317         last_status = false;
318
319     } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
320         XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
321         last_status = false;
322
323     } else if (opt::exit_on_close && close_received) {
324         XBT_VERB("Close received");
325         last_status = false;
326
327     } else if (real_load == 0.0 && !data_close_pending) {
328         XBT_VERB("I'm a poor lonesome process, and I have no load...");
329         last_status = false;
330
331     } else if (100.0 * total_load_running / total_load_init <=
332                opt::load_ratio_threshold) {
333         // fixme: this check should be implemented with a distributed
334         // algorithm, and not a shared global variable!
335         XBT_VERB("No more load to balance in system.");
336         last_status = false;
337     }
338
339     return last_status;
340 }
341
342 double process::get_sum_of_to_send() const
343 {
344     using std::placeholders::_1;
345     using std::placeholders::_2;
346
347     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
348                            std::bind(std::plus<double>(), _1,
349                                      std::bind(&neighbor::get_to_send, _2)));
350 }
351
352 void process::load_balance()
353 {
354     if (lb_iter == 1)           // warn only once
355         XBT_WARN("process::load_balance() is a no-op!");
356 }
357
358 void process::send(neighbor& nb, double amount)
359 {
360     expected_load -= amount;
361     nb.set_to_send(nb.get_to_send() + amount);
362     nb.set_load(nb.get_load() + amount);
363 }
364
365 void process::ctrl_send(neighbor& nb)
366 {
367     double info_to_send = expected_load;
368     double debt_to_send;
369     if (opt::bookkeeping) {     // bookkeeping
370         debt_to_send = nb.get_to_send();
371         if (debt_to_send > 0.0) {
372             nb.set_to_send(0.0);
373             nb.set_debt(nb.get_debt() + debt_to_send);
374         }
375     } else {                    // !bookkeeping
376         debt_to_send = 0.0;
377     }
378     if (info_to_send != prev_load_broadcast || debt_to_send > 0.0) {
379         message* msg = new message(message::CTRL, info_to_send, debt_to_send);
380         add_ctrl_send_mesg(msg->get_size());
381         comm.ctrl_send(nb.get_ctrl_mbox(), msg);
382     }
383 }
384
385 double process::compute_load_to_send(double desired)
386 {
387     if (opt::integer_transfer)
388         desired = std::floor(desired);
389     return desired >= opt::min_transfer_amount ? desired : 0.0;
390 }
391
392 void process::data_send(neighbor& nb)
393 {
394     double load_to_send;
395     if (opt::bookkeeping) {     // bookkeeping
396         double excess_load;     // load amount we are able to send
397         if (opt::egocentric)
398             excess_load = std::max(0.0, real_load - expected_load);
399         else
400             excess_load = real_load;
401
402         double balance = nb.get_debt() - nb.get_credit();
403         load_to_send = std::min(excess_load,
404                                 std::max(0.0, balance));
405
406         // adjust load to send (rounding, truncation, etc.)
407         load_to_send = compute_load_to_send(load_to_send);
408         if (load_to_send > 0.0)
409             nb.set_debt(nb.get_debt() - load_to_send);
410     } else {                    // !bookkeeping
411         load_to_send = compute_load_to_send(nb.get_to_send());
412         if (load_to_send > 0.0)
413             nb.set_to_send(nb.get_to_send() - load_to_send);
414     }
415     real_load -= load_to_send;
416     while (load_to_send > 0.0) {
417         double amount;
418         if (opt::max_transfer_amount)
419             amount = std::min(load_to_send, opt::max_transfer_amount);
420         else
421             amount = load_to_send;
422         message* msg = new message(message::DATA, amount);
423         add_data_send_mesg(msg->get_size());
424         comm.data_send(nb.get_data_mbox(), msg);
425         load_to_send -= amount;
426     }
427 }
428
429 void process::ctrl_close(neighbor& nb)
430 {
431     comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
432 }
433
434 void process::data_close(neighbor& nb)
435 {
436     comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
437 }
438
439 void process::ctrl_receive(double timeout)
440 {
441     message* msg;
442     m_host_t from;
443
444     XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
445     while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
446         if (msg->get_type() != message::CTRL_CLOSE)
447             add_ctrl_recv_mesg(msg->get_size());
448         handle_message(msg, from);
449         timeout = 0.0;
450     }
451 }
452
453 void process::data_receive(double timeout)
454 {
455     message* msg;
456     m_host_t from;
457
458     XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
459     while (data_close_pending && comm.data_recv(msg, from, timeout)) {
460         if (msg->get_type() != message::DATA_CLOSE)
461             add_data_recv_mesg(msg->get_size());
462         handle_message(msg, from);
463         timeout = 0.0;
464     }
465 }
466
467 void process::handle_message(message* msg, m_host_t from)
468 {
469     switch (msg->get_type()) {
470     case message::CTRL: {
471         neighbor* n = rev_neigh[from];
472         n->set_load(msg->get_amount() + n->get_to_send());
473         if (opt::bookkeeping) {
474             double credit = msg->get_credit();
475             expected_load += credit;
476             n->set_credit(n->get_credit() + credit);
477         }
478         break;
479     }
480     case message::DATA: {
481         neighbor* n = rev_neigh[from];
482         double ld = msg->get_amount();
483         received_load += ld;
484         n->set_credit(n->get_credit() - ld);
485         break;
486     }
487     case message::CTRL_CLOSE:
488         ctrl_close_pending--;
489         close_received = true;
490         break;
491     case message::DATA_CLOSE:
492         data_close_pending--;
493         close_received = true;
494         break;
495     }
496     delete msg;
497 }
498
499 #define print_loads_generic(vec, verbose, logp, cat)                    \
500     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
501         using std::placeholders::_1;                                    \
502         XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected).  "     \
503                   "Neighbor loads:", real_load, expected_load);         \
504         std::for_each(vec.begin(), vec.end(),                           \
505                       std::bind(&neighbor::print, _1, verbose, logp, cat)); \
506     } else ((void)0)
507
508 void process::print_loads(bool verbose,
509                           e_xbt_log_priority_t logp,
510                           xbt_log_category_t cat) const
511 {
512     print_loads_generic(neigh, verbose, logp, cat);
513 }
514
515 void process::print_loads_p(bool verbose,
516                             e_xbt_log_priority_t logp,
517                             xbt_log_category_t cat) const
518 {
519     print_loads_generic(pneigh, verbose, logp, cat);
520 }
521
522 #undef print_loads_generic
523
524 // Local variables:
525 // mode: c++
526 // End: