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

Private GIT Repository
Separate ctrl and data communications.
[loba.git] / process.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <iterator>
4 #include <stdexcept>
5 #include <sstream>
6 #include <xbt/log.h>
7 #include <xbt/time.h>
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(proc);
10
11 #include "misc.h"
12 #include "options.h"
13 #include "tracing.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]) >> real_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_all()
39     expected_load = real_load;
40     total_load_running += real_load;
41     total_load_init += real_load;
42
43     ctrl_close_pending = data_close_pending = neigh.size();
44     close_received = false;
45     finalizing = false;
46
47     comp_iter = lb_iter = 0;
48
49     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
50     if (!LOG_ISENABLED(logp))
51         return;
52     std::ostringstream oss;
53     oss << neigh.size() << " neighbor";
54     if (!neigh.empty()) {
55         oss << ESSE(neigh.size()) << ": ";
56         std::transform(neigh.begin(), neigh.end() - 1,
57                        std::ostream_iterator<const char*>(oss, ", "),
58                        std::tr1::mem_fn(&neighbor::get_name));
59         oss << neigh.back().get_name();
60     }
61     XBT_LOG(logp, "Got %s.", oss.str().c_str());
62     print_loads(false, logp);
63 }
64
65 process::~process()
66 {
67     total_load_exit += real_load;
68     if (opt::bookkeeping) {
69         XBT_INFO("Final load after %d:%d iterations: %g ; expected: %g",
70                  lb_iter, comp_iter, real_load, expected_load);
71     } else {
72         XBT_INFO("Final load after %d iterations: %g",
73                  lb_iter, real_load);
74         if (lb_iter != comp_iter)
75             XBT_WARN("lb_iter (%d) and comp_iter (%d) differ!",
76                      lb_iter, comp_iter);
77     }
78     XBT_VERB("Total computation for this process: %g", comp);
79 }
80
81 int process::run()
82 {
83     double next_iter_after_date = 0.0;
84     XBT_INFO("Initial load: %g", real_load);
85     XBT_VERB("Starting...");
86     while (true) {
87         if (get_load() > 0.0) {
88             double now = MSG_get_clock();
89             if (now < next_iter_after_date)
90                 MSG_process_sleep(next_iter_after_date - now);
91             next_iter_after_date = MSG_get_clock() + opt::min_lb_iter_duration;
92
93             ++lb_iter;
94
95             if (opt::log_rate && lb_iter % opt::log_rate == 0) {
96                 if (opt::bookkeeping)
97                     XBT_INFO("(%u:%u) current load: %g ; expected: %g",
98                              lb_iter, comp_iter, real_load, expected_load);
99                 else
100                     XBT_INFO("(%u) current load: %g",
101                              lb_iter, real_load);
102             }
103
104             load_balance();
105
106             print_loads(true, xbt_log_priority_debug);
107         }
108
109         // send load information, and load (data) if any
110         send_all();
111         if (real_load > 0.0) {
112             ++comp_iter;
113             compute();
114         }
115
116         if (opt::lb_maxiter && lb_iter >= opt::lb_maxiter) {
117             XBT_VERB("Reached lb_maxiter: %d/%d", lb_iter, opt::lb_maxiter);
118             break;
119         }
120         if (opt::comp_maxiter && comp_iter >= opt::comp_maxiter) {
121             XBT_VERB("Reached comp_maxiter: %d/%d", comp_iter, opt::comp_maxiter);
122             break;
123         }
124         if (opt::time_limit && MSG_get_clock() >= opt::time_limit) {
125             XBT_VERB("Reached time limit: %g/%g", MSG_get_clock(), opt::time_limit);
126             break;
127         }
128
129         // block on receiving unless there is something to compute or
130         // to send
131         double timeout;
132         if (real_load != 0 || get_load() != prev_load_broadcast)
133             timeout = 0.0;
134         else if (opt::min_lb_iter_duration)
135             timeout = opt::min_lb_iter_duration;
136         else
137             timeout = 1.0;
138         receive(timeout);
139
140         // one of our neighbor is finalizing
141         if (opt::exit_on_close && close_received) {
142             XBT_VERB("Close received");
143             break;
144         }
145
146         // have no load and cannot receive anything
147         if (real_load == 0.0 && !may_receive()) {
148             XBT_VERB("I'm a poor lonesome process, and I have no load...");
149             break;
150         }
151
152         // fixme: this check should be implemented with a distributed
153         // algorithm, and not a shared global variable!
154         // fixme: should this chunk be moved before call to receive() ?
155         if (100.0 * total_load_running / total_load_init <=
156             opt::load_ratio_threshold) {
157             XBT_VERB("No more load to balance in system.");
158             break;
159         } else {
160             XBT_DEBUG("still %g load to balance, continuing...", total_load_running);
161         }
162     }
163     XBT_VERB("Going to finalize...");
164     finalize();
165
166     /* Open Questions :
167      * - definition of load on heterogeneous hosts ?
168      * - how to detect convergence ?
169      * - how to manage link failures ?
170      */
171
172     XBT_VERB("Done.");
173     return 0;
174 }
175
176 void process::load_balance()
177 {
178     if (lb_iter == 1)           // warn only once
179         XBT_WARN("process::load_balance() is a no-op!");
180 }
181
182 void process::compute()
183 {
184     if (real_load > 0.0) {
185         double flops = opt::comp_cost(real_load);
186         m_task_t task = MSG_task_create("computation", flops, 0.0, NULL);
187         TRACE_msg_set_task_category(task, TRACE_CAT_COMP);
188         XBT_DEBUG("compute %g flop%s", flops, ESSE(flops));
189         MSG_task_execute(task);
190         comp += flops;
191         MSG_task_destroy(task);
192     } else {
193         XBT_DEBUG("nothing to compute !");
194     }
195 }
196
197 void process::send(neighbor& nb, double amount)
198 {
199     set_load(get_load() - amount);
200     nb.set_to_send(nb.get_to_send() + amount);
201     nb.set_load(nb.get_load() + amount); // fixme: make this optional?
202 }
203
204 void process::send1_no_bookkeeping(neighbor& nb)
205 {
206     if (real_load != prev_load_broadcast)
207         comm.ctrl_send(nb.get_ctrl_mbox(),
208                        new message(message::INFO, real_load));
209     double load_to_send = nb.get_to_send();
210     if (load_to_send > 0.0) {
211         comm.data_send(nb.get_data_mbox(),
212                        new message(message::LOAD, load_to_send));
213         nb.set_to_send(0.0);
214     }
215 }
216
217 void process::send1_bookkeeping(neighbor& nb)
218 {
219     if (expected_load != prev_load_broadcast)
220         comm.ctrl_send(nb.get_ctrl_mbox(),
221                        new message(message::INFO, expected_load));
222     double load_to_send;
223     double new_debt;
224     double debt_to_send = nb.get_to_send();
225     if (debt_to_send > 0.0) {
226         comm.ctrl_send(nb.get_ctrl_mbox(),
227                        new message(message::CREDIT, debt_to_send));
228         nb.set_to_send(0.0);
229         new_debt = nb.get_debt() + debt_to_send;
230     } else {
231         new_debt = nb.get_debt();
232     }
233     if (real_load <= new_debt) {
234         load_to_send = real_load;
235         nb.set_debt(new_debt - load_to_send);
236         real_load = 0.0;
237     } else {
238         load_to_send = new_debt;
239         nb.set_debt(0.0);
240         real_load -= load_to_send;
241     }
242     if (load_to_send > 0.0)
243         comm.data_send(nb.get_data_mbox(),
244                        new message(message::LOAD, load_to_send));
245 }
246
247 void process::send_all()
248 {
249     using std::tr1::bind;
250     using std::tr1::placeholders::_1;
251
252     if (opt::bookkeeping) {
253         std::for_each(neigh.begin(), neigh.end(),
254                       bind(&process::send1_bookkeeping, this, _1));
255         prev_load_broadcast = expected_load;
256     } else {
257         std::for_each(neigh.begin(), neigh.end(),
258                       bind(&process::send1_no_bookkeeping, this, _1));
259         prev_load_broadcast = real_load;
260     }
261     comm.ctrl_flush(false);
262     comm.data_flush(false);
263 }
264
265 void process::receive(double timeout)
266 {
267     message* msg;
268     m_host_t from;
269
270     XBT_DEBUG("%sblocking receive (%g)", "\0non-" + !timeout, timeout);
271     while (may_receive() && (comm.ctrl_recv(msg, from, timeout) ||
272                              comm.data_recv(msg, from, timeout))) {
273         switch (msg->get_type()) {
274         case message::INFO: {
275             neighbor* n = rev_neigh[from];
276             n->set_load(msg->get_amount());
277             break;
278         }
279         case message::CREDIT:
280             expected_load += msg->get_amount();
281             break;
282         case message::LOAD: {
283             double ld = msg->get_amount();
284             real_load += ld;
285             if (finalizing)
286                 total_load_running -= ld;
287             break;
288         }
289         case message::CTRL_CLOSE:
290             ctrl_close_pending--;
291             close_received = true;
292             break;
293         case message::DATA_CLOSE:
294             data_close_pending--;
295             close_received = true;
296             break;
297         }
298         delete msg;
299         timeout = 0.0;          // only wait on first recv
300     }
301     comm.ctrl_flush(false);
302     comm.data_flush(false);
303 }
304
305 void process::finalize1(neighbor& nb)
306 {
307     comm.ctrl_send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
308     comm.data_send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
309 }
310
311 void process::finalize()
312 {
313     using std::tr1::bind;
314     using std::tr1::placeholders::_1;
315
316     finalizing = true;
317     total_load_running -= real_load;
318
319     XBT_DEBUG("send CLOSE to %zu neighbor%s", neigh.size(), ESSE(neigh.size()));
320     std::for_each(neigh.begin(), neigh.end(),
321                   bind(&process::finalize1, this, _1));
322
323     while (may_receive()) {
324         comm.ctrl_flush(false);
325         comm.data_flush(false);
326         XBT_DEBUG("waiting for %d CTRL and %d DATA CLOSE",
327                   ctrl_close_pending, data_close_pending);
328         receive(-1.0);
329     }
330
331     comm.ctrl_flush(true);
332     comm.data_flush(true);
333 }
334
335 #define print_loads_generic(vec, verbose, logp, cat)                    \
336     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
337         using std::tr1::bind;                                           \
338         using std::tr1::placeholders::_1;                               \
339         XBT_XCLOG(cat, logp, "Neighbor loads:");                        \
340         std::for_each(vec.begin(), vec.end(),                           \
341                       bind(&neighbor::print, _1, verbose, logp, cat));  \
342     } else ((void)0)
343
344 void process::print_loads(bool verbose,
345                           e_xbt_log_priority_t logp,
346                           xbt_log_category_t cat) const
347 {
348     print_loads_generic(neigh, verbose, logp, cat);
349 }
350
351 void process::print_loads_p(bool verbose,
352                             e_xbt_log_priority_t logp,
353                             xbt_log_category_t cat) const
354 {
355     print_loads_generic(pneigh, verbose, logp, cat);
356 }
357
358 #undef print_loads_generic
359
360 // Local variables:
361 // mode: c++
362 // End: