1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
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. */
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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_trans_comm, mc_transition,
17 "Logging specific to MC transitions about communications");
19 namespace simgrid::mc {
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)
28 , receiver_(receiver_)
31 CommWaitTransition::CommWaitTransition(aid_t issuer, int times_considered, std::stringstream& stream)
32 : Transition(Type::COMM_WAIT, issuer, times_considered)
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());
38 std::string CommWaitTransition::to_string(bool verbose) const
40 return xbt::string_printf("WaitComm(from %ld to %ld, mbox=%u, %s)", sender_, receiver_, mbox_,
41 (timeout_ ? "timeout" : "no timeout"));
43 bool CommWaitTransition::depends(const Transition* other) const
45 if (other->type_ < type_)
46 return other->depends(this);
48 // Actions executed by the same actor are always dependent
49 if (other->aid_ == aid_)
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
57 return false; // Comm transitions are INDEP with non-comm transitions
60 bool CommWaitTransition::reversible_race(const Transition* other) const
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;
67 xbt_die("Unexpected transition type %s", to_c_str(type_));
71 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, unsigned comm_, aid_t sender_,
72 aid_t receiver_, unsigned mbox_)
73 : Transition(Type::COMM_TEST, issuer, times_considered)
77 , receiver_(receiver_)
80 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
81 : Transition(Type::COMM_TEST, issuer, times_considered)
83 xbt_assert(stream >> comm_ >> sender_ >> receiver_ >> mbox_ >> call_location_);
84 XBT_DEBUG("CommTestTransition comm:%u, sender:%ld receiver:%ld mbox:%u call_loc:%s", comm_, sender_, receiver_, mbox_,
85 call_location_.c_str());
87 std::string CommTestTransition::to_string(bool verbose) const
89 return xbt::string_printf("TestComm(from %ld to %ld, mbox=%u)", sender_, receiver_, mbox_);
92 bool CommTestTransition::depends(const Transition* other) const
94 if (other->type_ < type_)
95 return other->depends(this);
97 // Actions executed by the same actor are always dependent
98 if (other->aid_ == aid_)
101 if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
102 return false; // Test & Test are independent
104 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
106 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
108 /* Wait & Test are independent */
112 return false; // Comm transitions are INDEP with non-comm transitions
115 bool CommTestTransition::reversible_race(const Transition* other) const
118 case Type::COMM_TEST:
119 return true; // CommTest is always enabled
121 xbt_die("Unexpected transition type %s", to_c_str(type_));
125 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, unsigned comm_, unsigned mbox_, int tag_)
126 : Transition(Type::COMM_ASYNC_RECV, issuer, times_considered), comm_(comm_), mbox_(mbox_), tag_(tag_)
129 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
130 : Transition(Type::COMM_ASYNC_RECV, issuer, times_considered)
132 xbt_assert(stream >> comm_ >> mbox_ >> tag_ >> call_location_);
133 XBT_DEBUG("CommRecvTransition comm:%u, mbox:%u tag:%d call_loc:%s", comm_, mbox_, tag_, call_location_.c_str());
135 std::string CommRecvTransition::to_string(bool verbose) const
137 return xbt::string_printf("iRecv(mbox=%u)", mbox_);
139 bool CommRecvTransition::depends(const Transition* other) const
141 if (other->type_ < type_)
142 return other->depends(this);
144 // Actions executed by the same actor are always dependent
145 if (other->aid_ == aid_)
148 if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
149 return mbox_ == recv->mbox_;
151 if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
154 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
155 if (mbox_ != test->mbox_)
158 if ((aid_ != test->sender_) && (aid_ != test->receiver_))
161 // If the test is checking a paired comm already, we're independent!
162 // If we happen to make up that pair, then we're dependent...
163 if (test->comm_ != comm_)
166 return true; // DEP with other send transitions
169 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
173 if (mbox_ != wait->mbox_)
176 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
179 // If the wait is waiting on a paired comm already, we're independent!
180 // If we happen to make up that pair, then we're dependent...
181 if ((aid_ != wait->aid_) && wait->comm_ != comm_)
184 return true; // DEP with other wait transitions
187 return false; // Comm transitions are INDEP with non-comm transitions
190 bool CommRecvTransition::reversible_race(const Transition* other) const
193 case Type::COMM_ASYNC_RECV:
194 return true; // CommRecv is always enabled
196 xbt_die("Unexpected transition type %s", to_c_str(type_));
200 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, unsigned comm_, unsigned mbox_, int tag_)
201 : Transition(Type::COMM_ASYNC_SEND, issuer, times_considered), comm_(comm_), mbox_(mbox_), tag_(tag_)
204 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
205 : Transition(Type::COMM_ASYNC_SEND, issuer, times_considered)
207 xbt_assert(stream >> comm_ >> mbox_ >> tag_ >> call_location_);
208 XBT_DEBUG("SendTransition comm:%u mbox:%u tag:%d call_loc:%s", comm_, mbox_, tag_, call_location_.c_str());
210 std::string CommSendTransition::to_string(bool verbose = false) const
212 return xbt::string_printf("iSend(mbox=%u)", mbox_);
215 bool CommSendTransition::depends(const Transition* other) const
217 if (other->type_ < type_)
218 return other->depends(this);
220 // Actions executed by the same actor are always dependent
221 if (other->aid_ == aid_)
224 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
225 return mbox_ == other_isend->mbox_;
227 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
230 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
231 if (mbox_ != test->mbox_)
234 if ((aid_ != test->sender_) && (aid_ != test->receiver_))
237 // If the test is checking a paired comm already, we're independent!
238 // If we happen to make up that pair, then we're dependent...
239 if (test->comm_ != comm_)
242 return true; // DEP with other test transitions
245 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
249 if (mbox_ != wait->mbox_)
252 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
255 // If the wait is waiting on a paired comm already, we're independent!
256 // If we happen to make up that pair, then we're dependent...
257 if ((aid_ != wait->aid_) && wait->comm_ != comm_)
260 return true; // DEP with other wait transitions
263 return false; // Comm transitions are INDEP with non-comm transitions
266 bool CommSendTransition::reversible_race(const Transition* other) const
269 case Type::COMM_ASYNC_SEND:
270 return true; // CommSend is always enabled
272 xbt_die("Unexpected transition type %s", to_c_str(type_));
276 } // namespace simgrid::mc