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

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