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

Private GIT Repository
Define opt::min_comp_iter_duration.
[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 void communicator::receiver1(msg_comm_t& comm, m_task_t& task, const char* mbox)
168 {
169     MSG_comm_destroy(comm);
170     if (strcmp(MSG_task_get_name(task), "finalize")) {
171         XBT_DEBUG("received message on %s", mbox);
172         xbt_mutex_acquire(mutex);
173         received.push(task);
174         xbt_cond_signal(cond);
175         xbt_mutex_release(mutex);
176         task = NULL;
177         comm = MSG_task_irecv(&task, mbox);
178     } else {
179         XBT_DEBUG("received finalize on %s", mbox);
180         MSG_task_destroy(task);
181         task = NULL;
182         comm = NULL;
183     }
184 }
185
186 int communicator::receiver()
187 {
188     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
189     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
190     XBT_DEBUG("receiver ready");
191     xbt_mutex_acquire(mutex);
192     xbt_cond_signal(cond);      // signal master that we are ready
193     xbt_mutex_release(mutex);
194
195     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
196     while (ctrl_comm || data_comm) {
197
198         if (ctrl_comm)
199             xbt_dynar_push(comms, &ctrl_comm);
200         if (data_comm)
201             xbt_dynar_push(comms, &data_comm);
202         int recvd = MSG_comm_waitany(comms);
203         msg_comm_t comm = xbt_dynar_get_as(comms, recvd, msg_comm_t);
204         xbt_dynar_reset(comms);
205
206         if (comm == ctrl_comm)
207             receiver1(ctrl_comm, ctrl_task, get_ctrl_mbox());
208         else if (comm == data_comm)
209             receiver1(data_comm, data_task, get_data_mbox());
210         else {
211             XBT_ERROR("Handling unknown comm -- %p", comm);
212             MSG_comm_destroy(comm);
213         }
214     }
215     xbt_dynar_free(&comms);
216     return 0;
217 }
218
219 // Local variables:
220 // mode: c++
221 // End: