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

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