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

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