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

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