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

Private GIT Repository
Add options to control min./max. data transfer amount.
[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 #include "tracing.h"
15
16 #include "process.h"
17
18 double process::total_load_init = 0.0;
19 double process::total_load_running = 0.0;
20 double process::total_load_exit = 0.0;
21
22 namespace {
23
24     void sleep_until_date(double& date, double duration = 0.0)
25     {
26         double sleep_duration = date - MSG_get_clock();
27         if (sleep_duration > 0.0)
28             MSG_process_sleep(sleep_duration);
29         date = MSG_get_clock() + duration;
30     }
31
32 }
33
34 process::process(int argc, char* argv[])
35 {
36     if (argc < 2 || !(std::istringstream(argv[1]) >> real_load))
37         throw std::invalid_argument("bad or missing initial load parameter");
38
39     neigh.assign(argv + 2, argv + argc);
40
41     pneigh.reserve(neigh.size());
42     for (unsigned i = 0 ; i < neigh.size() ; i++) {
43         neighbor* ptr = &neigh[i];
44         m_host_t host = MSG_get_host_by_name(ptr->get_name());
45         pneigh.push_back(ptr);
46         rev_neigh.insert(std::make_pair(host, ptr));
47     }
48
49     comp = 0.0;
50
51     prev_load_broadcast = -1;   // force sending of load on first send_all()
52     expected_load = real_load;
53     total_load_running += real_load;
54     total_load_init += real_load;
55
56     ctrl_close_pending = data_close_pending = neigh.size();
57     close_received = false;
58     finalizing = false;
59
60     comp_iter = lb_iter = 0;
61
62     lb_thread = new_msg_thread("loba",
63                                std::tr1::bind(&process::load_balance_loop,
64                                               this));
65
66     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
67     if (!LOG_ISENABLED(logp))
68         return;
69     std::ostringstream oss;
70     oss << neigh.size() << " neighbor";
71     if (!neigh.empty()) {
72         oss << ESSE(neigh.size()) << ": ";
73         std::transform(neigh.begin(), neigh.end() - 1,
74                        std::ostream_iterator<const char*>(oss, ", "),
75                        std::tr1::mem_fn(&neighbor::get_name));
76         oss << neigh.back().get_name();
77     }
78     XBT_LOG(logp, "Got %s.", oss.str().c_str());
79     print_loads(false, logp);
80 }
81
82 process::~process()
83 {
84     delete lb_thread;
85     total_load_exit += real_load;
86     if (opt::log_rate < 0)
87         return;
88     XBT_INFO("Final load after %d:%d iterations: %g",
89              lb_iter, comp_iter, real_load);
90     XBT_VERB("Expected load was: %g", expected_load);
91     XBT_VERB("Total computation for this process: %g", comp);
92 }
93
94 int process::run()
95 {
96     if (opt::log_rate >= 0) {
97         XBT_INFO("Initial load: %g", real_load);
98         XBT_VERB("Initial expected load: %g", expected_load);
99     }
100     XBT_VERB("Starting...");
101     mutex.acquire();
102     lb_thread->start();
103     while (lb_iter <= opt::comp_iter_delay)
104         cond.wait(mutex);
105     mutex.release();
106     double sleep_duration = opt::comp_time_delay - MSG_get_clock();
107     if (sleep_duration > 0.0)
108         MSG_process_sleep(sleep_duration);
109     compute_loop();
110     lb_thread->wait();
111     XBT_VERB("Done.");
112     return 0;
113 }
114
115 void process::load_balance_loop()
116 {
117     using std::tr1::bind;
118     using std::tr1::placeholders::_1;
119
120     double next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
121     while (still_running()) {
122         if (lb_iter == opt::comp_iter_delay) {
123             mutex.acquire();
124             ++lb_iter;
125             cond.signal();
126             mutex.release();
127         } else {
128             ++lb_iter;
129         }
130
131         if (!opt::bookkeeping)
132             expected_load = real_load - get_sum_of_to_send();
133         // nothing to do with opt::bookkeeping
134
135         if (opt::log_rate && lb_iter % opt::log_rate == 0) {
136             XBT_INFO("(%u:%u) current load: %g", lb_iter, comp_iter, real_load);
137             XBT_VERB("... expected load: %g", expected_load);
138         }
139
140         if (expected_load > 0.0)
141             load_balance();
142
143         print_loads(true, xbt_log_priority_debug);
144
145         // send
146         std::for_each(neigh.begin(), neigh.end(),
147                       bind(&process::ctrl_send, this, _1));
148         prev_load_broadcast = expected_load;
149
150         sleep_until_date(next_iter_after_date, opt::min_lb_iter_duration);
151         ctrl_receive(0.0);
152
153         comm.ctrl_flush(false);
154     }
155
156     XBT_VERB("Going to finalize for %s...", __func__);
157     XBT_DEBUG("send CTRL_CLOSE to %zu neighbor%s",
158               neigh.size(), ESSE(neigh.size()));
159     std::for_each(neigh.begin(), neigh.end(),
160                   bind(&process::ctrl_close, this, _1));
161     while (ctrl_close_pending) {
162         comm.ctrl_flush(false);
163         XBT_DEBUG("waiting for %d CTRL CLOSE", ctrl_close_pending);
164         ctrl_receive(-1.0);
165     }
166     comm.ctrl_flush(true);
167 }
168
169 void process::compute_loop()
170 {
171     using std::tr1::bind;
172     using std::tr1::placeholders::_1;
173
174     double next_iter_after_date = MSG_get_clock() + opt::min_comp_iter_duration;
175     while (still_running()) {
176         // receive
177         if (real_load > 0.0)
178             data_receive(0.0);
179         else
180             data_receive(opt::min_comp_iter_duration);
181
182         comm.data_flush(false);
183
184         if (real_load == 0.0)
185             continue;
186
187         // send
188         std::for_each(neigh.begin(), neigh.end(),
189                       bind(&process::data_send, this, _1));
190
191         // compute
192         ++comp_iter;
193         double flops = opt::comp_cost(real_load);
194         m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
195         TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
196         XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
197         MSG_task_execute(task);
198         comp += flops;
199         MSG_task_destroy(task);
200
201         sleep_until_date(next_iter_after_date, opt::min_comp_iter_duration);
202     }
203
204     XBT_VERB("Going to finalize for %s...", __func__);
205     // last send, for not losing load scheduled to be sent
206     std::for_each(neigh.begin(), neigh.end(),
207                   bind(&process::data_send, this, _1));
208     finalizing = true;
209     total_load_running -= real_load;
210     XBT_DEBUG("send DATA_CLOSE to %zu neighbor%s",
211               neigh.size(), ESSE(neigh.size()));
212     std::for_each(neigh.begin(), neigh.end(),
213                   bind(&process::data_close, this, _1));
214     while (data_close_pending) {
215         comm.data_flush(false);
216         XBT_DEBUG("waiting for %d DATA CLOSE", data_close_pending);
217         data_receive(-1.0);
218     }
219     comm.data_flush(true);
220 }
221
222 bool process::still_running()
223 {
224     static bool last_status = true;
225
226     if (!last_status) {
227         /* nop */
228
229     } else if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
230         XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
231         last_status = false;
232
233     } else if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
234         XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
235         last_status = false;
236
237     } else if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
238         XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
239         last_status = false;
240
241     } else if (opt::exit_on_close && close_received) {
242         XBT_VERB("Close received");
243         last_status = false;
244
245     } else if (real_load == 0.0 && !data_close_pending) {
246         XBT_VERB("I'm a poor lonesome process, and I have no load...");
247         last_status = false;
248
249     } else if (100.0 * total_load_running / total_load_init <=
250                opt::load_ratio_threshold) {
251         // fixme: this check should be implemented with a distributed
252         // algorithm, and not a shared global variable!
253         XBT_VERB("No more load to balance in system.");
254         last_status = false;
255     }
256
257     return last_status;
258 }
259
260 double process::get_sum_of_to_send() const
261 {
262     using std::tr1::bind;
263     using std::tr1::placeholders::_1;
264     using std::tr1::placeholders::_2;
265
266     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
267                            bind(std::plus<double>(),
268                                 _1, bind(&neighbor::get_to_send, _2)));
269 }
270
271 void process::load_balance()
272 {
273     if (lb_iter == 1)           // warn only once
274         XBT_WARN("process::load_balance() is a no-op!");
275 }
276
277 void process::send(neighbor& nb, double amount)
278 {
279     expected_load -= amount;
280     nb.set_to_send(nb.get_to_send() + amount);
281     nb.set_load(nb.get_load() + amount);
282 }
283
284 void process::ctrl_send(neighbor& nb)
285 {
286     double info_to_send = expected_load;
287     if (info_to_send != prev_load_broadcast)
288         comm.ctrl_send(nb.get_ctrl_mbox(),
289                        new message(message::INFO, info_to_send));
290     if (opt::bookkeeping) {
291         double debt_to_send = nb.get_to_send();
292         if (debt_to_send > 0.0) {
293             nb.set_to_send(0.0);
294             nb.set_debt(nb.get_debt() + debt_to_send);
295             comm.ctrl_send(nb.get_ctrl_mbox(),
296                            new message(message::CREDIT, debt_to_send));
297         }
298     }
299 }
300
301 void process::data_send(neighbor& nb)
302 {
303     double load_to_send;
304     if (opt::bookkeeping) {
305         load_to_send = std::min(real_load, nb.get_debt());
306         if (load_to_send >= opt::min_transfer_amount) {
307             nb.set_debt(nb.get_debt() - load_to_send);
308             real_load -= load_to_send;
309         } else {
310             load_to_send = 0.0;
311         }
312     } else {
313         load_to_send = nb.get_to_send();
314         if (load_to_send >= opt::min_transfer_amount) {
315             nb.set_to_send(0.0);
316             real_load -= load_to_send;
317         } else {
318             load_to_send = 0.0;
319         }
320     }
321     while (load_to_send > 0.0) {
322         double amount;
323         if (opt::max_transfer_amount)
324             amount = std::min(load_to_send, opt::max_transfer_amount);
325         else
326             amount = load_to_send;
327         comm.data_send(nb.get_data_mbox(), new message(message::LOAD, amount));
328         load_to_send -= amount;
329     }
330 }
331
332 void process::ctrl_close(neighbor& nb)
333 {
334     comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
335 }
336
337 void process::data_close(neighbor& nb)
338 {
339     comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
340 }
341
342 void process::ctrl_receive(double timeout)
343 {
344     message* msg;
345     m_host_t from;
346
347     XBT_DEBUG("%sblocking receive on ctrl (%g)", "\0non-" + !timeout, timeout);
348     while (ctrl_close_pending && comm.ctrl_recv(msg, from, timeout)) {
349         handle_message(msg, from);
350         timeout = 0.0;
351     }
352 }
353
354 void process::data_receive(double timeout)
355 {
356     message* msg;
357     m_host_t from;
358
359     XBT_DEBUG("%sblocking receive on data (%g)", "\0non-" + !timeout, timeout);
360     while (data_close_pending && comm.data_recv(msg, from, timeout)) {
361         handle_message(msg, from);
362         timeout = 0.0;
363     }
364 }
365
366 void process::handle_message(message* msg, m_host_t from)
367 {
368     switch (msg->get_type()) {
369     case message::INFO: {
370         neighbor* n = rev_neigh[from];
371         n->set_load(msg->get_amount() + n->get_to_send());
372         break;
373     }
374     case message::CREDIT:
375         expected_load += msg->get_amount();
376         break;
377     case message::LOAD: {
378         double ld = msg->get_amount();
379         real_load += ld;
380         if (finalizing)
381             total_load_running -= ld;
382         break;
383     }
384     case message::CTRL_CLOSE:
385         ctrl_close_pending--;
386         close_received = true;
387         break;
388     case message::DATA_CLOSE:
389         data_close_pending--;
390         close_received = true;
391         break;
392     }
393     delete msg;
394 }
395
396 #define print_loads_generic(vec, verbose, logp, cat)                    \
397     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
398         using std::tr1::bind;                                           \
399         using std::tr1::placeholders::_1;                               \
400         XBT_XCLOG(cat, logp, "My load: %g (real); %g (expected).  "     \
401                   "Neighbor loads:", real_load, expected_load);         \
402         std::for_each(vec.begin(), vec.end(),                           \
403                       bind(&neighbor::print, _1, verbose, logp, cat));  \
404     } else ((void)0)
405
406 void process::print_loads(bool verbose,
407                           e_xbt_log_priority_t logp,
408                           xbt_log_category_t cat) const
409 {
410     print_loads_generic(neigh, verbose, logp, cat);
411 }
412
413 void process::print_loads_p(bool verbose,
414                             e_xbt_log_priority_t logp,
415                             xbt_log_category_t cat) const
416 {
417     print_loads_generic(pneigh, verbose, logp, cat);
418 }
419
420 #undef print_loads_generic
421
422 // Local variables:
423 // mode: c++
424 // End: