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

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