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

Private GIT Repository
Use a separate thread to handle incoming messages.
[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 const int communicator::send_count_before_flush = 4;
25
26 communicator::communicator()
27     : host((hostdata* )MSG_host_get_data(MSG_host_self()))
28     , mutex(xbt_mutex_init())
29     , cond(xbt_cond_init())
30     , send_counter(0)
31     , ctrl_task(NULL)
32     , ctrl_comm(NULL)
33     , data_task(NULL)
34     , data_comm(NULL)
35 {
36     xbt_mutex_acquire(mutex);
37     receiver_process =
38         MSG_process_create("receiver", communicator::receiver_wrapper,
39                            this, MSG_host_self());
40     xbt_mutex_release(mutex);
41 }
42
43 communicator::~communicator()
44 {
45     m_task_t task;
46
47     DEBUG0("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     DEBUG0("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     DEBUG0("wait for receiver to terminate");
56     xbt_mutex_acquire(mutex);
57     while (receiver_process)
58         xbt_cond_wait(cond, mutex);
59     xbt_mutex_release(mutex);
60
61     if (ctrl_comm)
62         WARN0("ctrl_comm is pending!");
63     if (data_comm)
64         WARN0("data_comm is pending!");
65     if (!received.empty())
66         WARN2("lost %lu received message%s!",
67               (unsigned long )received.size(), ESSE(received.size()));
68     if (!sent_comm.empty())
69         WARN2("lost %lu sent message%s!",
70               (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
71
72     xbt_cond_destroy(cond);
73     xbt_mutex_destroy(mutex);
74 }
75
76 void communicator::send(const char* dest, message* msg)
77 {
78     DEBUG2("send %s to %s", msg->to_string().c_str(), dest);
79     double msg_size = sizeof *msg;
80     if (msg->get_type() == message::LOAD)
81         msg_size += opt::comm_cost(msg->get_amount());
82     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
83     msg_comm_t comm = MSG_task_isend(task, dest);
84     sent_comm.push_back(comm);
85
86     if (++send_counter >= send_count_before_flush) {
87         flush(false);
88         send_counter = 0;
89     }
90 }
91
92 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
93 {
94     if (wait) {
95         DEBUG0("suspend main process on recv");
96         xbt_mutex_acquire(mutex);
97         while (received.empty())
98             xbt_cond_wait(cond, mutex);
99         xbt_mutex_release(mutex);
100     }
101
102     if (received.empty())
103         return false;
104
105     m_task_t task = received.front();
106     received.pop();
107     msg = (message* )MSG_task_get_data(task);
108     from = MSG_task_get_source(task);
109     MSG_task_destroy(task);
110
111     DEBUG2("received %s from %s",
112            msg->to_string().c_str(), MSG_host_get_name(from));
113
114     return true;
115 }
116
117 void communicator::flush(bool wait)
118 {
119     using std::tr1::bind;
120     using std::tr1::placeholders::_1;
121
122     sent_comm.remove_if(comm_test_n_destroy);
123     if (wait && !sent_comm.empty()) {
124         xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
125         while (!sent_comm.empty()) {
126             std::for_each(sent_comm.begin(), sent_comm.end(),
127                           bind(xbt_dynar_push,
128                                comms, bind(misc::address<msg_comm_t>(), _1)));
129             MSG_comm_waitany(comms);
130             xbt_dynar_reset(comms);
131             sent_comm.remove_if(comm_test_n_destroy);
132         }
133         xbt_dynar_free(&comms);
134     }
135 }
136
137 bool communicator::comm_test_n_destroy(msg_comm_t comm)
138 {
139     if (MSG_comm_test(comm)) {
140         MSG_comm_destroy(comm);
141         return true;
142     } else
143         return false;
144 }
145
146 int communicator::receiver_wrapper(int, char* [])
147 {
148     communicator* comm;
149     comm = (communicator* )MSG_process_get_data(MSG_process_self());
150     int result = comm->receiver();
151
152     DEBUG0("terminate");
153     xbt_mutex_acquire(comm->mutex);
154     comm->receiver_process = NULL;
155     xbt_cond_signal(comm->cond);
156     xbt_mutex_release(comm->mutex);
157
158     return result;
159 }
160
161 int communicator::receiver()
162 {
163     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
164     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
165     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
166     while (ctrl_comm || data_comm) {
167
168         if (ctrl_comm)
169             xbt_dynar_push(comms, &ctrl_comm);
170         if (data_comm)
171             xbt_dynar_push(comms, &data_comm);
172         MSG_comm_waitany(comms);
173         xbt_dynar_reset(comms);
174
175         if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
176             if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
177                 DEBUG0("received message from ctrl");
178                 received.push(ctrl_task);
179                 ctrl_task = NULL;
180                 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
181             } else {
182                 DEBUG0("received finalize from ctrl");
183                 MSG_task_destroy(ctrl_task);
184                 ctrl_task = NULL;
185                 ctrl_comm = NULL;
186             }
187         }
188
189         if (data_comm && comm_test_n_destroy(data_comm)) {
190             if (strcmp(MSG_task_get_name(data_task), "finalize")) {
191                 DEBUG0("received message from data");
192                 received.push(data_task);
193                 data_task = NULL;
194                 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
195             } else {
196                 DEBUG0("received finalize from data");
197                 MSG_task_destroy(data_task);
198                 data_task = NULL;
199                 data_comm = NULL;
200             }
201         }
202         xbt_mutex_acquire(mutex);
203         xbt_cond_signal(cond);
204         xbt_mutex_release(mutex);
205     }
206     xbt_dynar_free(&comms);
207     return 0;
208 }
209
210 // Local variables:
211 // mode: c++
212 // End: