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

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