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

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