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

Private GIT Repository
Interchange lb_iter and comp_iter, and improve process main loop.
[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, bool wait)
87 {
88     if (wait) {
89         xbt_mutex_acquire(mutex);
90         while (received.empty()) {
91             DEBUG0("waiting for a message to come");
92             xbt_cond_wait(cond, mutex);
93         }
94         xbt_mutex_release(mutex);
95     }
96
97     if (received.empty())
98         return false;
99
100     m_task_t task = received.front();
101     received.pop();
102     msg = (message* )MSG_task_get_data(task);
103     from = MSG_task_get_source(task);
104     MSG_task_destroy(task);
105
106     DEBUG2("received %s from %s",
107            msg->to_string().c_str(), MSG_host_get_name(from));
108
109     return true;
110 }
111
112 void communicator::flush(bool wait)
113 {
114     using std::tr1::bind;
115     using std::tr1::placeholders::_1;
116
117     sent_comm.remove_if(comm_test_n_destroy);
118     if (wait && !sent_comm.empty()) {
119         xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
120         while (!sent_comm.empty()) {
121             std::for_each(sent_comm.begin(), sent_comm.end(),
122                           bind(xbt_dynar_push,
123                                comms, bind(misc::address<msg_comm_t>(), _1)));
124             MSG_comm_waitany(comms);
125             xbt_dynar_reset(comms);
126             sent_comm.remove_if(comm_test_n_destroy);
127         }
128         xbt_dynar_free(&comms);
129     }
130 }
131
132 bool communicator::comm_test_n_destroy(msg_comm_t comm)
133 {
134     if (MSG_comm_test(comm)) {
135         MSG_comm_destroy(comm);
136         return true;
137     } else
138         return false;
139 }
140
141 int communicator::receiver_wrapper(int, char* [])
142 {
143     communicator* comm;
144     comm = (communicator* )MSG_process_get_data(MSG_process_self());
145     int result = comm->receiver();
146
147     DEBUG0("terminate");
148     xbt_mutex_acquire(comm->mutex);
149     comm->receiver_process = NULL;
150     xbt_cond_signal(comm->cond);
151     xbt_mutex_release(comm->mutex);
152
153     return result;
154 }
155
156 int communicator::receiver()
157 {
158     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
159     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
160     DEBUG0("receiver ready");
161     xbt_mutex_acquire(mutex);
162     xbt_cond_signal(cond);      // signal master that we are ready
163     xbt_mutex_release(mutex);
164
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                 xbt_mutex_acquire(mutex);
179                 received.push(ctrl_task);
180                 xbt_mutex_release(mutex);
181                 ctrl_task = NULL;
182                 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
183             } else {
184                 DEBUG0("received finalize from ctrl");
185                 MSG_task_destroy(ctrl_task);
186                 ctrl_task = NULL;
187                 ctrl_comm = NULL;
188             }
189         }
190
191         if (data_comm && comm_test_n_destroy(data_comm)) {
192             if (strcmp(MSG_task_get_name(data_task), "finalize")) {
193                 DEBUG0("received message from data");
194                 xbt_mutex_acquire(mutex);
195                 received.push(data_task);
196                 xbt_mutex_release(mutex);
197                 data_task = NULL;
198                 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
199             } else {
200                 DEBUG0("received finalize from data");
201                 MSG_task_destroy(data_task);
202                 data_task = NULL;
203                 data_comm = NULL;
204             }
205         }
206         xbt_mutex_acquire(mutex);
207         if (!received.empty())
208             xbt_cond_signal(cond);
209         xbt_mutex_release(mutex);
210     }
211     xbt_dynar_free(&comms);
212     return 0;
213 }
214
215 // Local variables:
216 // mode: c++
217 // End: