]> 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 <algorithm>
2 #include <tr1/functional>
3 #include <iterator>
4 #include <stdexcept>
5 #include <sstream>
6 #include <xbt/log.h>
7 #include <xbt/time.h>
8 #include "misc.h"
9 #include "options.h"
10 #include "process.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
13
14 process::process(int argc, char* argv[])
15 {
16     if (argc < 2 || !(std::istringstream(argv[1]) >> load))
17         throw std::invalid_argument("bad or missing initial load");
18
19     neigh.assign(argv + 2, argv + argc);
20     expected_load = load;
21
22     ctrl_close_pending = data_close_pending = neigh.size();
23     if (neigh.size() == 1) {
24         comm.next_close_on_ctrl_is_last();
25         comm.next_close_on_data_is_last();
26     }
27     if (neigh.size() > 0)
28         comm.listen();
29
30     e_xbt_log_priority_t logp = xbt_log_priority_verbose;
31     if (!LOG_ISENABLED(logp))
32         return;
33     std::ostringstream oss;
34     oss << neigh.size() << " neighbor";
35     if (!neigh.empty()) {
36         oss << ESSE(neigh.size()) << ": ";
37         std::transform(neigh.begin(), neigh.end() - 1,
38                        std::ostream_iterator<const char*>(oss, ", "),
39                        std::tr1::mem_fn(&neighbor::get_name));
40         oss << neigh.back().get_name();
41     }
42     LOG1(logp, "Got %s.", oss.str().c_str());
43     print_loads(logp);
44 }
45
46 int process::run()
47 {
48     bool one_more = true;
49     unsigned iter = 0;
50
51     INFO1("Initial load: %g", load);
52     VERB0("Starting...");
53     while (one_more) {
54         bool close_received;
55         ++iter;
56
57         if (opt::log_rate && iter % opt::log_rate == 0) {
58             if (opt::bookkeeping)
59                 INFO3("(%u) current load: %g ; expected: %g",
60                       iter, load, expected_load);
61             else
62                 INFO2("(%u) current load: %g",
63                       iter, load);
64         }
65
66         compute();
67         close_received = !receive(false);
68
69         /*
70          *    compute load balancing;
71          *    send tasks to neighbors;
72          */
73
74         comm.flush(false);
75
76         if (opt::exit_on_close && close_received)
77             one_more = false;
78         if (opt::maxiter && iter >= opt::maxiter)
79             one_more = false;
80     }
81     VERB0("Going to finalize...");
82     finalize();
83
84     /* Open Questions :
85      * - definition of load on heterogeneous hosts ?
86      * - how to detect convergence ?
87      * - how to manage link failures ?
88      */
89
90     VERB0("Done.");
91     if (opt::bookkeeping)
92         INFO4("Final load after %d iteration%s: %g ; expected: %g",
93               iter, ESSE(iter), load, expected_load);
94     else
95         INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load);
96     return 0;
97 }
98
99 void process::compute()
100 {
101     // fixme: shall we do something special when duration is 0 ?
102     double duration = opt::comp_cost(load);
103     m_task_t task = MSG_task_create("computation", duration, 0.0, NULL);
104     DEBUG2("compute %g flop%s.", duration, ESSE(duration));
105     MSG_task_execute(task);
106     MSG_task_destroy(task);
107 }
108
109
110 // Returns false if a CLOSE message was received. 
111 bool process::receive(bool wait_for_close)
112 {
113     bool result = true;
114     message* msg;
115     m_host_t from;
116     while ((ctrl_close_pending ||
117             data_close_pending) && comm.recv(msg, from, wait_for_close)) {
118         DEBUG2("received %s from %s",
119                msg->to_string().c_str(), MSG_host_get_name(from));
120         switch (msg->get_type()) {
121         case message::INFO:
122             // fixme: update neighbor
123             // need a map m_host_t -> neighbor&
124             break;
125         case message::CREDIT:
126             expected_load += msg->get_amount();
127             break;
128         case message::LOAD:
129             load += msg->get_amount();
130             break;
131         case message::CTRL_CLOSE:
132             if (--ctrl_close_pending == 1)
133                 comm.next_close_on_ctrl_is_last();
134             DEBUG1("ctrl_close_pending = %d", ctrl_close_pending);
135             result = false;
136             break;
137         case message::DATA_CLOSE:
138             if (--data_close_pending == 1)
139                 comm.next_close_on_data_is_last();
140             DEBUG1("data_close_pending = %d", data_close_pending);
141             result = false;
142             break;
143         }
144         delete msg;
145     }
146     return result;
147 }
148
149 void process::finalize()
150 {
151     DEBUG2("send CLOSE to %d neighbor%s.",
152            (int )neigh.size(), ESSE(neigh.size()));
153     std::vector<neighbor>::iterator n;
154     for (n = neigh.begin() ; n != neigh.end() ; ++n) {
155         comm.send(n->get_ctrl_mbox(), new message(message::CTRL_CLOSE, 0.0));
156         comm.send(n->get_data_mbox(), new message(message::DATA_CLOSE, 0.0));
157     }
158
159     DEBUG2("wait for CLOSE from %d neighbor%s.",
160            (int )neigh.size(), ESSE(neigh.size()));
161     receive(true);
162
163     comm.flush(true);
164 }
165
166 void process::print_loads(e_xbt_log_priority_t logp)
167 {
168     if (!LOG_ISENABLED(logp))
169         return;
170
171     std::ostringstream oss;
172     if (neigh.empty()) {
173         oss << "no neighbor!";
174     } else {
175         std::transform(neigh.begin(), neigh.end() - 1,
176                        std::ostream_iterator<double>(oss, ", "),
177                        std::tr1::mem_fn(&neighbor::get_load));
178         oss << neigh.back().get_load();
179     }
180     LOG1(logp, "Neighbor loads: %s", oss.str().c_str());
181 }
182
183 // Local variables:
184 // mode: c++
185 // End: