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

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