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

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