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

Private GIT Repository
No return value for communicator::receiver().
[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(), MSG_comm_destroy);
141         sent_comm.clear();
142     }
143 }
144
145 bool communicator::comm_test_n_destroy(msg_comm_t comm)
146 {
147     if (MSG_comm_test(comm)) {
148         MSG_comm_destroy(comm);
149         return true;
150     } else
151         return false;
152 }
153
154 int communicator::receiver_wrapper(int, char* [])
155 {
156     communicator* comm;
157     comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
158     comm->receiver();
159
160     XBT_DEBUG("terminate");
161     xbt_mutex_acquire(comm->receiver_mutex);
162     comm->receiver_thread = NULL;
163     xbt_cond_signal(comm->receiver_cond);
164     xbt_mutex_release(comm->receiver_mutex);
165
166     return 0;
167 }
168
169 void communicator::receiver1(msg_comm_t& comm, m_task_t& task, const char* mbox)
170 {
171     MSG_comm_destroy(comm);
172     if (strcmp(MSG_task_get_name(task), "finalize")) {
173         XBT_DEBUG("received message on %s", mbox);
174         xbt_mutex_acquire(receiver_mutex);
175         received.push(task);
176         xbt_cond_signal(receiver_cond);
177         xbt_mutex_release(receiver_mutex);
178         task = NULL;
179         comm = MSG_task_irecv(&task, mbox);
180     } else {
181         XBT_DEBUG("received finalize on %s", mbox);
182         MSG_task_destroy(task);
183         task = NULL;
184         comm = NULL;
185     }
186 }
187
188 void communicator::receiver()
189 {
190     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
191     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
192     XBT_DEBUG("receiver ready");
193     xbt_mutex_acquire(receiver_mutex);
194     xbt_cond_signal(receiver_cond); // signal master that we are ready
195     xbt_mutex_release(receiver_mutex);
196
197     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
198     while (ctrl_comm || data_comm) {
199
200         if (ctrl_comm)
201             xbt_dynar_push(comms, &ctrl_comm);
202         if (data_comm)
203             xbt_dynar_push(comms, &data_comm);
204         int index = MSG_comm_waitany(comms);
205         msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
206         xbt_dynar_reset(comms);
207
208         if (finished_comm == ctrl_comm)
209             receiver1(ctrl_comm, ctrl_task, get_ctrl_mbox());
210         else if (finished_comm == data_comm)
211             receiver1(data_comm, data_task, get_data_mbox());
212         else
213             THROW1(0, 0, "Cannot handle unknown comm -- %p", finished_comm);
214     }
215     xbt_dynar_free(&comms);
216 }
217
218 // Local variables:
219 // mode: c++
220 // End: