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

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