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

Private GIT Repository
Wip++...
[loba.git] / process.cpp
1 #include "process.h"
2
3 #include <algorithm>
4 #include <tr1/functional>
5 #include <iterator>
6 #include <stdexcept>
7 #include <sstream>
8 #include <xbt/log.h>
9 #include <xbt/time.h>
10 #include "misc.h"
11 #include "options.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
14
15 process::process(int argc, char* argv[])
16 {
17     if (argc < 2 || !(std::istringstream(argv[1]) >> load))
18         throw std::invalid_argument("bad or missing initial load");
19
20     neigh.assign(argv + 2, argv + argc);
21
22     std::for_each(neigh.begin(), neigh.end(),
23                   std::tr1::bind(&process::insert_neighbor_in_map,
24                                  this, std::tr1::placeholders::_1));
25
26     expected_load = load;
27
28     ctrl_close_pending = data_close_pending = neigh.size();
29     if (neigh.size() == 1) {
30         comm.next_close_on_ctrl_is_last();
31         comm.next_close_on_data_is_last();
32     }
33     if (neigh.size() > 0)
34         comm.listen();
35
36     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
37     if (!LOG_ISENABLED(logp))
38         return;
39     std::ostringstream oss;
40     oss << neigh.size() << " neighbor";
41     if (!neigh.empty()) {
42         oss << ESSE(neigh.size()) << ": ";
43         std::transform(neigh.begin(), neigh.end() - 1,
44                        std::ostream_iterator<const char*>(oss, ", "),
45                        std::tr1::mem_fn(&neighbor::get_name));
46         oss << neigh.back().get_name();
47     }
48     LOG1(logp, "Got %s.", oss.str().c_str());
49     print_loads(logp);
50 }
51
52 int process::run()
53 {
54     bool one_more = true;
55
56     INFO1("Initial load: %g", load);
57     VERB0("Starting...");
58     iter = 0;
59     while (one_more) {
60         ++iter;
61
62         if (opt::log_rate && iter % opt::log_rate == 0) {
63             if (opt::bookkeeping)
64                 INFO3("(%u) current load: %g ; expected: %g",
65                       iter, load, expected_load);
66             else
67                 INFO2("(%u) current load: %g",
68                       iter, load);
69         }
70
71         compute();
72
73         bool close_received = !receive(false);
74
75         if (opt::bookkeeping)
76             expected_load -= load_balance(expected_load);
77         else
78             load -= load_balance(load);
79
80         send();
81         comm.flush(false);
82
83         if (opt::exit_on_close && close_received)
84             one_more = false;
85         if (opt::maxiter && iter >= opt::maxiter)
86             one_more = false;
87     }
88     VERB0("Going to finalize...");
89     finalize();
90
91     /* Open Questions :
92      * - definition of load on heterogeneous hosts ?
93      * - how to detect convergence ?
94      * - how to manage link failures ?
95      */
96
97     VERB0("Done.");
98     if (opt::bookkeeping)
99         INFO4("Final load after %d iteration%s: %g ; expected: %g",
100               iter, ESSE(iter), load, expected_load);
101     else
102         INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
103     return 0;
104 }
105
106 void process::compute()
107 {
108     // fixme: shall we do something special when duration is 0 ?
109     double duration = opt::comp_cost(load);
110     m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
111     DEBUG2("compute %g flop%s.", duration, ESSE(duration));
112     MSG_task_execute(task);
113     MSG_task_destroy(task);
114 }
115
116 double process::load_balance(double /*my_load*/)
117 {
118     return 0.0;
119 }
120
121 void process::send1_no_bookkeeping(neighbor& nb)
122 {
123     comm.send(nb.get_ctrl_mbox(), new message(message::INFO, load));
124     double load_to_send = nb.get_to_send();
125     if (load_to_send > 0.0) {
126         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
127         nb.set_to_send(0.0);
128     }
129 }
130
131 void process::send1_bookkeeping(neighbor& nb)
132 {
133     comm.send(nb.get_ctrl_mbox(), new message(message::INFO, expected_load));
134     double load_to_send;
135     double new_debt;
136     double debt_to_send = nb.get_to_send();
137     if (debt_to_send > 0.0) {
138         comm.send(nb.get_ctrl_mbox(),
139                   new message(message::CREDIT, debt_to_send));
140         nb.set_to_send(0.0);
141         new_debt = nb.get_debt() + debt_to_send;
142     } else {
143         new_debt = nb.get_debt();
144     }
145     if (load <= new_debt) {
146         load_to_send = load;
147         nb.set_debt(new_debt - load_to_send);
148         load = 0.0;
149     } else {
150         load_to_send = new_debt;
151         nb.set_debt(0.0);
152         load -= load_to_send;
153     }
154     if (load_to_send > 0.0)
155         comm.send(nb.get_data_mbox(), new message(message::LOAD, load_to_send));
156 }
157
158 void process::send()
159 {
160     // fixme: shall we send data at all iterations?
161     if (opt::bookkeeping) {
162         std::for_each(neigh.begin(), neigh.end(),
163                       std::tr1::bind(&process::send1_bookkeeping,
164                                      this, std::tr1::placeholders::_1));
165     } else {
166         std::for_each(neigh.begin(), neigh.end(),
167                       std::tr1::bind(&process::send1_no_bookkeeping,
168                                      this, std::tr1::placeholders::_1));
169     }
170 }
171
172 // Returns false if a CLOSE message was received. 
173 bool process::receive(bool wait_for_close)
174 {
175     bool result = true;
176     message* msg;
177     m_host_t from;
178     while ((ctrl_close_pending ||
179             data_close_pending) && comm.recv(msg, from, wait_for_close)) {
180         DEBUG2("received %s from %s",
181                msg->to_string().c_str(), MSG_host_get_name(from));
182         switch (msg->get_type()) {
183         case message::INFO: {
184             neighbor* n = rev_neigh[from];
185             n->set_load(msg->get_amount());
186             break;
187         }
188         case message::CREDIT:
189             expected_load += msg->get_amount();
190             break;
191         case message::LOAD:
192             load += msg->get_amount();
193             break;
194         case message::CTRL_CLOSE:
195             if (--ctrl_close_pending == 1)
196                 comm.next_close_on_ctrl_is_last();
197             DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
198             result = false;
199             break;
200         case message::DATA_CLOSE:
201             if (--data_close_pending == 1)
202                 comm.next_close_on_data_is_last();
203             DEBUG1("data_close_pending = %d", data_close_pending);
204             result = false;
205             break;
206         }
207         delete msg;
208     }
209     return result;
210 }
211
212 void process::finalize1(neighbor& nb)
213 {
214     comm.send(nb.get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
215     comm.send(nb.get_data_mbox(), new message(message::DATA_CLOSE, 0.0));    
216 }
217
218 void process::finalize()
219 {
220     DEBUG2("send CLOSE to %d neighbor%s.",
221            (int )neigh.size(), ESSE(neigh.size()));
222     std::for_each(neigh.begin(), neigh.end(),
223                   std::tr1::bind(&process::finalize1,
224                                  this, std::tr1::placeholders::_1));
225
226     DEBUG2("wait for CLOSE from %d neighbor%s.",
227            (int )neigh.size(), ESSE(neigh.size()));
228     receive(true);
229
230     comm.flush(true);
231 }
232
233 void process::print_loads(e_xbt_log_priority_t logp)
234 {
235     if (!LOG_ISENABLED(logp))
236         return;
237
238     std::ostringstream oss;
239     if (neigh.empty()) {
240         oss << "no neighbor!";
241     } else {
242         std::transform(neigh.begin(), neigh.end() - 1,
243                        std::ostream_iterator<double>(oss, ", "),
244                        std::tr1::mem_fn(&neighbor::get_load));
245         oss << neigh.back().get_load();
246     }
247     LOG1(logp, "Neighbor loads: %s", oss.str().c_str());
248 }
249
250 void process::insert_neighbor_in_map(neighbor& nb)
251 {
252     neighbor* nbp = &nb;
253     m_host_t host = MSG_get_host_by_name(nb.get_name());
254     rev_neigh.insert(std::make_pair(host, nbp));
255 }
256
257 // Local variables:
258 // mode: c++
259 // End: