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

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