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

Private GIT Repository
fd01ac91b585074fa2b891daaf3dec8f0ac8deb6
[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 void communicator::receiver()
79 {
80     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
81     struct channel {
82         msg_comm_t comm;
83         m_task_t task;
84         const char* mbox;
85     };
86     channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
87                        { NULL, NULL, host->get_data_mbox() } };
88     const int chan_size = (sizeof chan) / (sizeof chan[0]);
89
90     for (int i = 0 ; i < chan_size ; ++i) {
91         chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
92         xbt_dynar_push(comms, &chan[i].comm);
93     }
94
95     while (!xbt_dynar_is_empty(comms)) {
96
97         int index = MSG_comm_waitany(comms);
98         msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
99         channel* ch;
100
101         for (ch = chan ; ch->comm != finished_comm ; ++ch)
102             /* nop */;
103
104         comm_check_n_destroy(ch->comm);
105         if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
106             XBT_DEBUG("received message on %s", ch->mbox);
107             received.push(ch->task);
108             ch->task = NULL;
109             ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
110             xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
111         } else {
112             XBT_DEBUG("received finalize on %s", ch->mbox);
113             MSG_task_destroy(ch->task);
114             ch->task = NULL;
115             ch->comm = NULL;
116             xbt_dynar_remove_at(comms, index, NULL);
117         }
118
119     }
120     xbt_dynar_free(&comms);
121 }
122
123 void communicator::comm_check_n_destroy(msg_comm_t comm)
124 {
125     xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
126     MSG_comm_destroy(comm);
127 }
128
129 bool communicator::comm_test_n_destroy(msg_comm_t comm)
130 {
131     if (MSG_comm_test(comm)) {
132         comm_check_n_destroy(comm);
133         return true;
134     } else
135         return false;
136 }
137
138 // Local variables:
139 // mode: c++
140 // End: