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

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