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

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