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

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