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

Private GIT Repository
Move final outputs into destructor.
[loba.git] / communicator.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <tr1/functional>
4 #include <sstream>
5 #include <msg/msg.h>
6 #include <xbt/log.h>
7
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
9
10 #include "misc.h"
11 #include "options.h"
12 #include "tracing.h"
13
14 #include "communicator.h"
15
16 std::string message::to_string()
17 {
18     static const char* str[] = { "INFO", "CREDIT", "LOAD",
19                                  "CTRL_CLOSE", "DATA_CLOSE" };
20     std::ostringstream oss;
21     oss << str[type] << ": " << amount;
22     return oss.str();
23 }
24
25 communicator::communicator()
26     : host((hostdata* )MSG_host_get_data(MSG_host_self()))
27     , mutex(xbt_mutex_init())
28     , cond(xbt_cond_init())
29     , ctrl_task(NULL)
30     , ctrl_comm(NULL)
31     , data_task(NULL)
32     , data_comm(NULL)
33 {
34     xbt_mutex_acquire(mutex);
35     receiver_process =
36         MSG_process_create("receiver", communicator::receiver_wrapper,
37                            this, MSG_host_self());
38     xbt_cond_wait(cond, mutex); // wait for the receiver to be ready
39     xbt_mutex_release(mutex);
40 }
41
42 communicator::~communicator()
43 {
44     m_task_t task;
45
46     DEBUG0("send finalize to receiver/ctrl");
47     task = MSG_task_create("finalize", 0.0, 0, NULL);
48     MSG_task_send(task, get_ctrl_mbox());
49
50     DEBUG0("send finalize to receiver/data");
51     task = MSG_task_create("finalize", 0.0, 0, NULL);
52     MSG_task_send(task, get_data_mbox());
53
54     xbt_mutex_acquire(mutex);
55     while (receiver_process) {
56         DEBUG0("waiting for receiver to terminate");
57         xbt_cond_wait(cond, mutex);
58     }
59     xbt_mutex_release(mutex);
60
61     if (ctrl_comm)
62         WARN0("ctrl_comm is pending!");
63     if (data_comm)
64         WARN0("data_comm is pending!");
65     if (!received.empty())
66         WARN2("lost %lu received message%s!",
67               (unsigned long )received.size(), ESSE(received.size()));
68     if (!sent_comm.empty())
69         WARN2("lost %lu sent message%s!",
70               (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
71
72     xbt_cond_destroy(cond);
73     xbt_mutex_destroy(mutex);
74 }
75
76 void communicator::send(const char* dest, message* msg)
77 {
78     DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
79     double msg_size = sizeof *msg;
80     if (msg->get_type() == message::LOAD)
81         msg_size += opt::comm_cost(msg->get_amount());
82     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
83     TRACE_msg_set_task_category(task,
84                                 msg->get_type() == message::LOAD ?
85                                 TRACE_CAT_DATA : TRACE_CAT_CTRL);
86     msg_comm_t comm = MSG_task_isend(task, dest);
87     sent_comm.push_back(comm);
88 }
89
90 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
91 {
92     if (timeout != 0) {
93         volatile double deadline =
94             timeout > 0 ? MSG_get_clock() + timeout : 0.0;
95         xbt_mutex_acquire(mutex);
96         while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
97             xbt_ex_t e;
98             DEBUG0("waiting for a message to come");
99             TRY {
100                 if (deadline)
101                     xbt_cond_timedwait(cond, mutex, deadline - MSG_get_clock());
102                 else
103                     xbt_cond_wait(cond, mutex);
104             }
105             CATCH (e) {
106                 if (e.category != timeout_error)
107                     RETHROW;
108                 xbt_ex_free(e);
109             }
110         }
111         xbt_mutex_release(mutex);
112     }
113
114     if (received.empty())
115         return false;
116
117     m_task_t task = received.front();
118     received.pop();
119     msg = (message* )MSG_task_get_data(task);
120     from = MSG_task_get_source(task);
121     MSG_task_destroy(task);
122
123     DEBUG2("received %s from %s",
124            msg->to_string().c_str(), MSG_host_get_name(from));
125
126     return true;
127 }
128
129 void communicator::flush(bool wait)
130 {
131     using std::tr1::bind;
132     using std::tr1::placeholders::_1;
133
134     sent_comm.remove_if(comm_test_n_destroy);
135     if (wait && !sent_comm.empty()) {
136         xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
137         while (!sent_comm.empty()) {
138             std::for_each(sent_comm.begin(), sent_comm.end(),
139                           bind(xbt_dynar_push,
140                                comms, bind(misc::address<msg_comm_t>(), _1)));
141             MSG_comm_waitany(comms);
142             xbt_dynar_reset(comms);
143             sent_comm.remove_if(comm_test_n_destroy);
144         }
145         xbt_dynar_free(&comms);
146     }
147 }
148
149 bool communicator::comm_test_n_destroy(msg_comm_t comm)
150 {
151     if (MSG_comm_test(comm)) {
152         MSG_comm_destroy(comm);
153         return true;
154     } else
155         return false;
156 }
157
158 int communicator::receiver_wrapper(int, char* [])
159 {
160     communicator* comm;
161     comm = (communicator* )MSG_process_get_data(MSG_process_self());
162     int result = comm->receiver();
163
164     DEBUG0("terminate");
165     xbt_mutex_acquire(comm->mutex);
166     comm->receiver_process = NULL;
167     xbt_cond_signal(comm->cond);
168     xbt_mutex_release(comm->mutex);
169
170     return result;
171 }
172
173 int communicator::receiver()
174 {
175     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
176     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
177     DEBUG0("receiver ready");
178     xbt_mutex_acquire(mutex);
179     xbt_cond_signal(cond);      // signal master that we are ready
180     xbt_mutex_release(mutex);
181
182     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
183     while (ctrl_comm || data_comm) {
184
185         if (ctrl_comm)
186             xbt_dynar_push(comms, &ctrl_comm);
187         if (data_comm)
188             xbt_dynar_push(comms, &data_comm);
189         MSG_comm_waitany(comms);
190         xbt_dynar_reset(comms);
191
192         if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
193             if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
194                 DEBUG0("received message from ctrl");
195                 xbt_mutex_acquire(mutex);
196                 received.push(ctrl_task);
197                 xbt_mutex_release(mutex);
198                 ctrl_task = NULL;
199                 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
200             } else {
201                 DEBUG0("received finalize from ctrl");
202                 MSG_task_destroy(ctrl_task);
203                 ctrl_task = NULL;
204                 ctrl_comm = NULL;
205             }
206         }
207
208         if (data_comm && comm_test_n_destroy(data_comm)) {
209             if (strcmp(MSG_task_get_name(data_task), "finalize")) {
210                 DEBUG0("received message from data");
211                 xbt_mutex_acquire(mutex);
212                 received.push(data_task);
213                 xbt_mutex_release(mutex);
214                 data_task = NULL;
215                 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
216             } else {
217                 DEBUG0("received finalize from data");
218                 MSG_task_destroy(data_task);
219                 data_task = NULL;
220                 data_comm = NULL;
221             }
222         }
223         xbt_mutex_acquire(mutex);
224         if (!received.empty())
225             xbt_cond_signal(cond);
226         xbt_mutex_release(mutex);
227     }
228     xbt_dynar_free(&comms);
229     return 0;
230 }
231
232 // Local variables:
233 // mode: c++
234 // End: