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

Private GIT Repository
Define neighbor::add_load, and use it.
[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_exit = 0.0;
19
20 process::process(int argc, char* argv[])
21 {
22     if (argc < 2 || !(std::istringstream(argv[1]) >> load))
23         throw std::invalid_argument("bad or missing initial load parameter");
24
25     neigh.assign(argv + 2, argv + argc);
26
27     pneigh.reserve(neigh.size());
28     for (unsigned i = 0 ; i < neigh.size() ; i++) {
29         neighbor* ptr = &neigh[i];
30         m_host_t host = MSG_get_host_by_name(ptr->get_name());
31         pneigh.push_back(ptr);
32         rev_neigh.insert(std::make_pair(host, ptr));
33     }
34
35     expected_load = load;
36     total_load_init += load;
37
38     ctrl_close_pending = data_close_pending = neigh.size();
39     if (neigh.size() == 1) {
40         comm.next_close_on_ctrl_is_last();
41         comm.next_close_on_data_is_last();
42     }
43     if (neigh.size() > 0)
44         comm.listen();
45
46     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
47     if (!LOG_ISENABLED(logp))
48         return;
49     std::ostringstream oss;
50     oss << neigh.size() << " neighbor";
51     if (!neigh.empty()) {
52         oss << ESSE(neigh.size()) << ": ";
53         std::transform(neigh.begin(), neigh.end() - 1,
54                        std::ostream_iterator<const char*>(oss, ", "),
55                        std::tr1::mem_fn(&neighbor::get_name));
56         oss << neigh.back().get_name();
57     }
58     LOG1(logp, "Got %s.", oss.str().c_str());
59     print_loads(true, logp);
60 }
61
62 process::~process()
63 {
64     total_load_exit += load;
65 }
66
67 int process::run()
68 {
69     INFO1("Initial load: %g", load);
70     VERB0("Starting...");
71     // first send() to inform neighbors about our load (force it)
72     prev_load_broadcast = -1;
73     iter = 0;
74     bool one_more = true;
75     do {
76         ++iter;
77
78         if (opt::log_rate && iter % opt::log_rate == 0) {
79             if (opt::bookkeeping)
80                 INFO3("(%u) current load: %g ; expected: %g",
81                       iter, load, expected_load);
82             else
83                 INFO2("(%u) current load: %g",
84                       iter, load);
85         }
86         print_loads(true, xbt_log_priority_debug);
87
88         if (opt::bookkeeping)
89             expected_load -= load_balance(expected_load);
90         else
91             load -= load_balance(load);
92
93         send();
94         compute();
95
96 // NDS for Need To Send
97 #define NDS ((opt::bookkeeping ? expected_load : load) != prev_load_broadcast)
98         bool can_recv;
99         do {
100             // General idea: do not iterate if there is nothing to
101             // compute, nor to send.
102
103             // fixme: review this chunk, and remove this NDS macro!
104
105             bool recv_wait = (load == 0 && !NDS);
106             bool close_received = !receive(recv_wait? WAIT: NO_WAIT);
107
108             if (opt::exit_on_close && close_received)
109                 one_more = false;
110             else if (opt::maxiter && iter >= opt::maxiter)
111                 one_more = false;
112             
113             can_recv = (ctrl_close_pending || data_close_pending);
114
115         } while (one_more && can_recv && load == 0 && !NDS);
116 #undef NDS
117
118     } while (one_more);
119     VERB0("Going to finalize...");
120     finalize();
121
122     /* Open Questions :
123      * - definition of load on heterogeneous hosts ?
124      * - how to detect convergence ?
125      * - how to manage link failures ?
126      */
127
128     VERB0("Done.");
129     INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
130     if (opt::bookkeeping)
131         INFO1("Expected load: %g", expected_load);
132     return 0;
133 }
134
135 double process::sum_of_to_send() const
136 {
137     using namespace std::tr1;
138     using namespace std::tr1::placeholders;
139
140     return std::accumulate(neigh.begin(), neigh.end(), 0.0,
141                            bind(std::plus<double>(),
142                                 _1, bind(&neighbor::get_to_send, _2)));
143 }
144
145 double process::load_balance(double /*my_load*/)
146 {
147     if (iter == 1)
148         WARN0("process::load_balance is a no-op!");
149     return 0.0;
150 }
151
152 void process::compute()
153 {
154     if (load > 0.0) {
155         double duration = opt::comp_cost(load);
156         m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
157         DEBUG2("compute %g flop%s.", duration, ESSE(duration));
158         MSG_task_execute(task);
159         MSG_task_destroy(task);
160     } else {
161         DEBUG0("nothing to compute !");
162     }
163 }
164
165 void process::send1_no_bookkeeping(neighbor& nb)
166 {
167     if (load != prev_load_broadcast)
168         comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
169     double load_to_send = nb.get_to_send();
170     if (load_to_send > 0.0) {
171         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
172         nb.set_to_send(0.0);
173     }
174 }
175
176 void process::send1_bookkeeping(neighbor& nb)
177 {
178     if (expected_load != prev_load_broadcast)
179         comm.send(nb.get_ctrl_mbox(),
180                   new message(message::INFO, expected_load));
181     double load_to_send;
182     double new_debt;
183     double debt_to_send = nb.get_to_send();
184     if (debt_to_send > 0.0) {
185         comm.send(nb.get_ctrl_mbox(),
186                   new message(message::CREDIT, debt_to_send));
187         nb.set_to_send(0.0);
188         new_debt = nb.get_debt() + debt_to_send;
189     } else {
190         new_debt = nb.get_debt();
191     }
192     if (load <= new_debt) {
193         load_to_send = load;
194         nb.set_debt(new_debt - load_to_send);
195         load = 0.0;
196     } else {
197         load_to_send = new_debt;
198         nb.set_debt(0.0);
199         load -= load_to_send;
200     }
201     if (load_to_send > 0.0)
202         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
203 }
204
205 void process::send()
206 {
207     using namespace std::tr1;
208     using namespace std::tr1::placeholders;
209
210     if (opt::bookkeeping) {
211         std::for_each(neigh.begin(), neigh.end(),
212                       bind(&process::send1_bookkeeping, this, _1));
213         prev_load_broadcast = expected_load;
214     } else {
215         std::for_each(neigh.begin(), neigh.end(),
216                       bind(&process::send1_no_bookkeeping, this, _1));
217         prev_load_broadcast = load;
218     }
219 }
220
221 bool process::receive(recv_wait_mode wait)
222 {
223     // DEBUG1("go for receive(%s)",
224     //        "NO_WAIT\0WAIT\0\0\0\0WAIT_FOR_CLOSE" + 8 * wait);
225     bool result = true;
226     message* msg;
227     m_host_t from;
228     bool do_recv = ctrl_close_pending || data_close_pending;
229     while (do_recv && comm.recv(msg, from, wait)) {
230         switch (msg->get_type()) {
231         case message::INFO: {
232             neighbor* n = rev_neigh[from];
233             n->set_load(msg->get_amount());
234             break;
235         }
236         case message::CREDIT:
237             expected_load += msg->get_amount();
238             break;
239         case message::LOAD:
240             load += msg->get_amount();
241             break;
242         case message::CTRL_CLOSE:
243             if (--ctrl_close_pending == 1)
244                 comm.next_close_on_ctrl_is_last();
245             DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
246             result = false;
247             break;
248         case message::DATA_CLOSE:
249             if (--data_close_pending == 1)
250                 comm.next_close_on_data_is_last();
251             DEBUG1("data_close_pending = %d", data_close_pending);
252             result = false;
253             break;
254         }
255         delete msg;
256         do_recv = (wait == WAIT_FOR_CLOSE) &&
257             (ctrl_close_pending || data_close_pending);
258     }
259     return result;
260 }
261
262 void process::finalize1(neighbor& nb)
263 {
264     comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
265     comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));    
266 }
267
268 void process::finalize()
269 {
270     using namespace std::tr1;
271     using namespace std::tr1::placeholders;
272
273     DEBUG2("send CLOSE to %d neighbor%s.",
274            (int )neigh.size(), ESSE(neigh.size()));
275     std::for_each(neigh.begin(), neigh.end(),
276                   bind(&process::finalize1, this, _1));
277
278     DEBUG2("wait for CLOSE from %d neighbor%s.",
279            (int )neigh.size(), ESSE(neigh.size()));
280     receive(WAIT_FOR_CLOSE);
281
282     comm.flush(true);
283 }
284
285 #define print_loads_generic(vec, verbose, logp, cat)                    \
286     if (_XBT_LOG_ISENABLEDV((*cat), logp)) {                            \
287         using namespace std::tr1;                                       \
288         using namespace std::tr1::placeholders;                         \
289         XCLOG0(cat, logp, "Neighbor loads:");                           \
290         std::for_each(vec.begin(), vec.end(),                           \
291                       bind(&neighbor::print, _1, verbose, logp, cat));  \
292     } else ((void)0)
293
294 void process::print_loads(bool verbose,
295                           e_xbt_log_priority_t logp,
296                           xbt_log_category_t cat) const
297 {
298     print_loads_generic(neigh, verbose, logp, cat);
299 }
300
301 void process::print_loads_p(bool verbose,
302                             e_xbt_log_priority_t logp,
303                             xbt_log_category_t cat) const
304 {
305     print_loads_generic(pneigh, verbose, logp, cat);
306 }
307
308 #undef print_loads_generic
309
310 // Local variables:
311 // mode: c++
312 // End: