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

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