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

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