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

Private GIT Repository
3516b68146ee8844a3c8ec8c33fad1b3c19befc6
[loba.git] / communicator.cpp
1 #include <algorithm>
2 #include <msg/msg.h>
3 #include <xbt/log.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(comm);
6
7 #include "misc.h"
8 #include "options.h"
9 #include "simgrid_features.h"
10 #include "tracing.h"
11
12 #include "communicator.h"
13
14 communicator::communicator()
15     : host(static_cast<hostdata*>(MSG_host_get_data(MSG_host_self())))
16 {
17     receiver_mutex.acquire();
18     receiver_thread =
19         MSG_process_create("receiver", communicator::receiver_wrapper,
20                            this, MSG_host_self());
21     receiver_cond.wait(receiver_mutex); // wait for the receiver to be ready
22     receiver_mutex.release();
23 }
24
25 communicator::~communicator()
26 {
27     m_task_t task;
28
29     XBT_DEBUG("send finalize to receiver/ctrl");
30     task = MSG_task_create("finalize", 0.0, 0, NULL);
31     MSG_task_send(task, host->get_ctrl_mbox());
32
33     XBT_DEBUG("send finalize to receiver/data");
34     task = MSG_task_create("finalize", 0.0, 0, NULL);
35     MSG_task_send(task, host->get_data_mbox());
36
37     receiver_mutex.acquire();
38     while (receiver_thread) {
39         XBT_DEBUG("waiting for receiver to terminate");
40         receiver_cond.wait(receiver_mutex);
41     }
42     receiver_mutex.release();
43
44     if (!received.empty())
45         XBT_WARN("lost %zu received message%s!",
46                  received.size(), ESSE(received.size()));
47     if (!sent_comm.empty())
48         XBT_WARN("lost %zu sent message%s!",
49                  sent_comm.size(), ESSE(sent_comm.size()));
50 }
51
52 void communicator::send(const char* dest, message* msg)
53 {
54     XBT_DEBUG("send %s to %s", msg->to_string().c_str(), dest);
55     double msg_size = sizeof *msg;
56     if (msg->get_type() == message::LOAD)
57         msg_size += opt::comm_cost(msg->get_amount());
58     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
59     TRACE_msg_set_task_category(task,
60                                 msg->get_type() == message::LOAD ?
61                                 TRACE_CAT_DATA : TRACE_CAT_CTRL);
62     msg_comm_t comm = MSG_task_isend(task, dest);
63     sent_comm.push_back(comm);
64 }
65
66 void communicator::flush(bool wait)
67 {
68     sent_comm.remove_if(comm_test_n_destroy);
69     if (wait && !sent_comm.empty()) {
70         msg_comm_t comms[sent_comm.size()];
71         std::copy(sent_comm.begin(), sent_comm.end(), comms);
72         MSG_comm_waitall(comms, sent_comm.size(), -1.0);
73         if (!MSG_WAIT_DESTROYS_COMMS)
74             std::for_each(sent_comm.begin(), sent_comm.end(),
75                           comm_check_n_destroy);
76         sent_comm.clear();
77     }
78 }
79
80 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
81 {
82     XBT_DEBUG("waiting for a message to come");
83     bool recvd = received.pop(msg, from, timeout);
84     if (recvd)
85         XBT_DEBUG("received %s from %s",
86                   msg->to_string().c_str(), MSG_host_get_name(from));
87     return recvd;
88 }
89
90 int communicator::receiver_wrapper(int, char* [])
91 {
92     communicator* comm;
93     comm = static_cast<communicator*>(MSG_process_get_data(MSG_process_self()));
94     comm->receiver();
95
96     XBT_DEBUG("terminate");
97     comm->receiver_mutex.acquire();
98     comm->receiver_thread = NULL;
99     comm->receiver_cond.signal();
100     comm->receiver_mutex.release();
101
102     return 0;
103 }
104
105 void communicator::receiver()
106 {
107     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
108     struct channel {
109         msg_comm_t comm;
110         m_task_t task;
111         const char* mbox;
112     };
113     channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
114                        { NULL, NULL, host->get_data_mbox() } };
115     const int chan_size = (sizeof chan) / (sizeof chan[0]);
116
117     for (int i = 0 ; i < chan_size ; ++i) {
118         chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
119         xbt_dynar_push(comms, &chan[i].comm);
120     }
121
122     XBT_DEBUG("receiver ready");
123     receiver_mutex.acquire();
124     receiver_cond.signal();     // signal master that we are ready
125     receiver_mutex.release();
126
127     while (!xbt_dynar_is_empty(comms)) {
128
129         int index = MSG_comm_waitany(comms);
130         msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
131         channel* ch;
132
133         for (ch = chan ; ch->comm != finished_comm ; ++ch)
134             /* nop */;
135
136         comm_check_n_destroy(ch->comm);
137         if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
138             XBT_DEBUG("received message on %s", ch->mbox);
139             received.push(ch->task);
140             ch->task = NULL;
141             ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
142             xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
143         } else {
144             XBT_DEBUG("received finalize on %s", ch->mbox);
145             MSG_task_destroy(ch->task);
146             ch->task = NULL;
147             ch->comm = NULL;
148             xbt_dynar_remove_at(comms, index, NULL);
149         }
150
151     }
152     xbt_dynar_free(&comms);
153 }
154
155 void communicator::comm_check_n_destroy(msg_comm_t comm)
156 {
157     xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
158     MSG_comm_destroy(comm);
159 }
160
161 bool communicator::comm_test_n_destroy(msg_comm_t comm)
162 {
163     if (MSG_comm_test(comm)) {
164         comm_check_n_destroy(comm);
165         return true;
166     } else
167         return false;
168 }
169
170 // Local variables:
171 // mode: c++
172 // End: