Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / mc / transition / TransitionComm.cpp
1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/transition/TransitionComm.hpp"
7 #include "simgrid/config.h"
8 #include "src/mc/api/RemoteApp.hpp"
9 #include "src/mc/api/State.hpp"
10 #include "xbt/asserts.h"
11 #include "xbt/string.hpp"
12
13 #include <inttypes.h>
14 #include <sstream>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_comm, mc_transition,
17                                 "Logging specific to MC transitions about communications");
18
19 namespace simgrid::mc {
20
21 CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, bool timeout_, unsigned comm_, aid_t sender_,
22                                        aid_t receiver_, unsigned mbox_)
23     : Transition(Type::COMM_WAIT, issuer, times_considered)
24     , timeout_(timeout_)
25     , comm_(comm_)
26     , mbox_(mbox_)
27     , sender_(sender_)
28     , receiver_(receiver_)
29 {
30 }
31 CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, std::stringstream& stream)
32     : Transition(Type::COMM_WAIT, issuer, times_considered)
33 {
34   xbt_assert(stream >> timeout_ >> comm_ >> sender_ >> receiver_ >> mbox_ >> call_location_);
35   XBT_DEBUG("CommWaitTransition %s comm:%u, sender:%ld receiver:%ld mbox:%u call_loc:%s",
36             (timeout_ ? "timeout" : "no-timeout"), comm_, sender_, receiver_, mbox_, call_location_.c_str());
37 }
38 std::string CommWaitTransition::to_string(bool verbose) const
39 {
40   return xbt::string_printf("WaitComm(from %ld to %ld, mbox=%u, %s)", sender_, receiver_, mbox_,
41                             (timeout_ ? "timeout" : "no timeout"));
42 }
43 bool CommWaitTransition::depends(const Transition* other) const
44 {
45   if (other->type_ < type_)
46     return other->depends(this);
47
48   // Actions executed by the same actor are always dependent
49   if (other->aid_ == aid_)
50     return true;
51
52   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
53     if (timeout_ || wait->timeout_)
54       return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
55   }
56
57   return false; // Comm transitions are INDEP with non-comm transitions
58 }
59
60 bool CommWaitTransition::reversible_race(const Transition* other) const
61 {
62   xbt_assert(type_ == Type::COMM_WAIT, "Unexpected transition type %s", to_c_str(type_));
63
64   // If the other event is a communication event, then we are not reversible; otherwise we are reversible.
65   return other->type_ != Transition::Type::COMM_ASYNC_SEND && other->type_ != Transition::Type::COMM_ASYNC_RECV;
66 }
67
68 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, unsigned comm_, aid_t sender_,
69                                        aid_t receiver_, unsigned mbox_)
70     : Transition(Type::COMM_TEST, issuer, times_considered)
71     , comm_(comm_)
72     , mbox_(mbox_)
73     , sender_(sender_)
74     , receiver_(receiver_)
75 {
76 }
77 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
78     : Transition(Type::COMM_TEST, issuer, times_considered)
79 {
80   xbt_assert(stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> call_location_);
81   XBT_DEBUG("CommTestTransition comm:%u, sender:%ld receiver:%ld mbox:%u call_loc:%s", comm_, sender_, receiver_, mbox_,
82             call_location_.c_str());
83 }
84 std::string CommTestTransition::to_string(bool verbose) const
85 {
86   return xbt::string_printf("TestComm(from %ld to %ld, mbox=%u)", sender_, receiver_, mbox_);
87 }
88
89 bool CommTestTransition::depends(const Transition* other) const
90 {
91   if (other->type_ < type_)
92     return other->depends(this);
93
94   // Actions executed by the same actor are always dependent
95   if (other->aid_ == aid_)
96     return true;
97
98   if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
99     return false; // Test & Test are independent
100
101   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
102     if (wait->timeout_)
103       return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
104
105     /* Wait & Test are independent */
106     return false;
107   }
108
109   return false; // Comm transitions are INDEP with non-comm transitions
110 }
111
112 bool CommTestTransition::reversible_race(const Transition* other) const
113 {
114   xbt_assert(type_ == Type::COMM_TEST, "Unexpected transition type %s", to_c_str(type_));
115   return true; // CommTest is always enabled
116 }
117
118 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, unsigned comm_, unsigned mbox_, int tag_)
119     : Transition(Type::COMM_ASYNC_RECV, issuer, times_considered), comm_(comm_), mbox_(mbox_), tag_(tag_)
120 {
121 }
122 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
123     : Transition(Type::COMM_ASYNC_RECV, issuer, times_considered)
124 {
125   xbt_assert(stream >> comm_ >> mbox_ >> tag_ >> call_location_);
126   XBT_DEBUG("CommRecvTransition comm:%u, mbox:%u tag:%d call_loc:%s", comm_, mbox_, tag_, call_location_.c_str());
127 }
128 std::string CommRecvTransition::to_string(bool verbose) const
129 {
130   return xbt::string_printf("iRecv(mbox=%u)", mbox_);
131 }
132 bool CommRecvTransition::depends(const Transition* other) const
133 {
134   if (other->type_ < type_)
135     return other->depends(this);
136
137   // Actions executed by the same actor are always dependent
138   if (other->aid_ == aid_)
139     return true;
140
141   if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
142     return mbox_ == recv->mbox_;
143
144   if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
145     return false;
146
147   if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
148     if (mbox_ != test->mbox_)
149       return false;
150
151     if ((aid_ != test->sender_) && (aid_ != test->receiver_))
152       return false;
153
154     // If the test is checking a paired comm already, we're independent!
155     // If we happen to make up that pair, then we're dependent...
156     if (test->comm_ != comm_)
157       return false;
158
159     return true; // DEP with other send transitions
160   }
161
162   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
163     if (wait->timeout_)
164       return true;
165
166     if (mbox_ != wait->mbox_)
167       return false;
168
169     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
170       return false;
171
172     // If the wait is waiting on a paired comm already, we're independent!
173     // If we happen to make up that pair, then we're dependent...
174     if ((aid_ != wait->aid_) && wait->comm_ != comm_)
175       return false;
176
177     return true; // DEP with other wait transitions
178   }
179
180   return false; // Comm transitions are INDEP with non-comm transitions
181 }
182
183 bool CommRecvTransition::reversible_race(const Transition* other) const
184 {
185   xbt_assert(type_ == Type::COMM_ASYNC_RECV, "Unexpected transition type %s", to_c_str(type_));
186
187   return true; // CommRecv is always enabled
188 }
189
190 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, unsigned comm_, unsigned mbox_, int tag_)
191     : Transition(Type::COMM_ASYNC_SEND, issuer, times_considered), comm_(comm_), mbox_(mbox_), tag_(tag_)
192 {
193 }
194 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
195     : Transition(Type::COMM_ASYNC_SEND, issuer, times_considered)
196 {
197   xbt_assert(stream >> comm_ >> mbox_ >> tag_ >> call_location_);
198   XBT_DEBUG("SendTransition comm:%u mbox:%u tag:%d call_loc:%s", comm_, mbox_, tag_, call_location_.c_str());
199 }
200 std::string CommSendTransition::to_string(bool verbose = false) const
201 {
202   return xbt::string_printf("iSend(mbox=%u)", mbox_);
203 }
204
205 bool CommSendTransition::depends(const Transition* other) const
206 {
207   if (other->type_ < type_)
208     return other->depends(this);
209
210   // Actions executed by the same actor are always dependent
211   if (other->aid_ == aid_)
212     return true;
213
214   if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
215     return mbox_ == other_isend->mbox_;
216
217   if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
218     return false;
219
220   if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
221     if (mbox_ != test->mbox_)
222       return false;
223
224     if ((aid_ != test->sender_) && (aid_ != test->receiver_))
225       return false;
226
227     // If the test is checking a paired comm already, we're independent!
228     // If we happen to make up that pair, then we're dependent...
229     if (test->comm_ != comm_)
230       return false;
231
232     return true; // DEP with other test transitions
233   }
234
235   if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
236     if (wait->timeout_)
237       return true;
238
239     if (mbox_ != wait->mbox_)
240       return false;
241
242     if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
243       return false;
244
245     // If the wait is waiting on a paired comm already, we're independent!
246     // If we happen to make up that pair, then we're dependent...
247     if ((aid_ != wait->aid_) && wait->comm_ != comm_)
248       return false;
249
250     return true; // DEP with other wait transitions
251   }
252
253   return false; // Comm transitions are INDEP with non-comm transitions
254 }
255
256 bool CommSendTransition::reversible_race(const Transition* other) const
257 {
258   xbt_assert(type_ == Type::COMM_ASYNC_SEND, "Unexpected transition type %s", to_c_str(type_));
259
260   return true; // CommSend is always enabled
261 }
262
263 } // namespace simgrid::mc