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

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