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

Private GIT Repository
daf14348393761cded88e166bbdfd4d313e4622e
[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.remove_if(comm_test_n_destroy);
64     if (wait && !sent_comm.empty()) {
65         msg_comm_t comms[sent_comm.size()];
66         std::copy(sent_comm.begin(), sent_comm.end(), comms);
67         MSG_comm_waitall(comms, sent_comm.size(), -1.0);
68         if (!MSG_WAIT_DESTROYS_COMMS)
69             std::for_each(sent_comm.begin(), sent_comm.end(),
70                           comm_check_n_destroy);
71         sent_comm.clear();
72     }
73 }
74
75 bool communicator::recv(message*& msg, m_host_t& from, double timeout)
76 {
77     XBT_DEBUG("waiting for a message to come");
78     bool recvd = received.pop(msg, from, timeout);
79     if (recvd)
80         XBT_DEBUG("received %s from %s",
81                   msg->to_string().c_str(), MSG_host_get_name(from));
82     return recvd;
83 }
84
85 void communicator::receiver()
86 {
87     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
88     struct channel {
89         msg_comm_t comm;
90         m_task_t task;
91         const char* mbox;
92     };
93     channel chan[] = { { NULL, NULL, host->get_ctrl_mbox() },
94                        { NULL, NULL, host->get_data_mbox() } };
95     const int chan_size = (sizeof chan) / (sizeof chan[0]);
96
97     for (int i = 0 ; i < chan_size ; ++i) {
98         chan[i].comm = MSG_task_irecv(&chan[i].task, chan[i].mbox);
99         xbt_dynar_push(comms, &chan[i].comm);
100     }
101
102     while (!xbt_dynar_is_empty(comms)) {
103
104         int index = MSG_comm_waitany(comms);
105         msg_comm_t finished_comm = xbt_dynar_get_as(comms, index, msg_comm_t);
106         channel* ch;
107
108         for (ch = chan ; ch->comm != finished_comm ; ++ch)
109             /* nop */;
110
111         comm_check_n_destroy(ch->comm);
112         if (strcmp(MSG_task_get_name(ch->task), "finalize")) {
113             XBT_DEBUG("received message on %s", ch->mbox);
114             received.push(ch->task);
115             ch->task = NULL;
116             ch->comm = MSG_task_irecv(&ch->task, ch->mbox);
117             xbt_dynar_set_as(comms, index, msg_comm_t, ch->comm);
118         } else {
119             XBT_DEBUG("received finalize on %s", ch->mbox);
120             MSG_task_destroy(ch->task);
121             ch->task = NULL;
122             ch->comm = NULL;
123             xbt_dynar_remove_at(comms, index, NULL);
124         }
125
126     }
127     xbt_dynar_free(&comms);
128 }
129
130 void communicator::comm_check_n_destroy(msg_comm_t comm)
131 {
132     xbt_assert(MSG_comm_get_status(comm) == MSG_OK);
133     MSG_comm_destroy(comm);
134 }
135
136 bool communicator::comm_test_n_destroy(msg_comm_t comm)
137 {
138     if (MSG_comm_test(comm)) {
139         comm_check_n_destroy(comm);
140         return true;
141     } else
142         return false;
143 }
144
145 // Local variables:
146 // mode: c++
147 // End: