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

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