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

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