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