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

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