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
62 xbt_assert(type_ == Type::COMM_WAIT, "Unexpected transition type %s", to_c_str(type_));
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;
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)
74 , receiver_(receiver_)
77 CommTestTransition::CommTestTransition(aid_t issuer, int times_considered, std::stringstream& stream)
78 : Transition(Type::COMM_TEST, issuer, times_considered)
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());
84 std::string CommTestTransition::to_string(bool verbose) const
86 return xbt::string_printf("TestComm(from %ld to %ld, mbox=%u)", sender_, receiver_, mbox_);
89 bool CommTestTransition::depends(const Transition* other) const
91 if (other->type_ < type_)
92 return other->depends(this);
94 // Actions executed by the same actor are always dependent
95 if (other->aid_ == aid_)
98 if (dynamic_cast<const CommTestTransition*>(other) != nullptr)
99 return false; // Test & Test are independent
101 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
103 return true; // Timeouts are not considered by the independence theorem, thus assumed dependent
105 /* Wait & Test are independent */
109 return false; // Comm transitions are INDEP with non-comm transitions
112 bool CommTestTransition::reversible_race(const Transition* other) const
114 xbt_assert(type_ == Type::COMM_TEST, "Unexpected transition type %s", to_c_str(type_));
115 return true; // CommTest is always enabled
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_)
122 CommRecvTransition::CommRecvTransition(aid_t issuer, int times_considered, std::stringstream& stream)
123 : Transition(Type::COMM_ASYNC_RECV, issuer, times_considered)
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());
128 std::string CommRecvTransition::to_string(bool verbose) const
130 return xbt::string_printf("iRecv(mbox=%u)", mbox_);
132 bool CommRecvTransition::depends(const Transition* other) const
134 if (other->type_ < type_)
135 return other->depends(this);
137 // Actions executed by the same actor are always dependent
138 if (other->aid_ == aid_)
141 if (const auto* recv = dynamic_cast<const CommRecvTransition*>(other))
142 return mbox_ == recv->mbox_;
144 if (dynamic_cast<const CommSendTransition*>(other) != nullptr)
147 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
148 if (mbox_ != test->mbox_)
151 if ((aid_ != test->sender_) && (aid_ != test->receiver_))
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_)
159 return true; // DEP with other send transitions
162 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
166 if (mbox_ != wait->mbox_)
169 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
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_)
177 return true; // DEP with other wait transitions
180 return false; // Comm transitions are INDEP with non-comm transitions
183 bool CommRecvTransition::reversible_race(const Transition* other) const
185 xbt_assert(type_ == Type::COMM_ASYNC_RECV, "Unexpected transition type %s", to_c_str(type_));
187 return true; // CommRecv is always enabled
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_)
194 CommSendTransition::CommSendTransition(aid_t issuer, int times_considered, std::stringstream& stream)
195 : Transition(Type::COMM_ASYNC_SEND, issuer, times_considered)
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());
200 std::string CommSendTransition::to_string(bool verbose = false) const
202 return xbt::string_printf("iSend(mbox=%u)", mbox_);
205 bool CommSendTransition::depends(const Transition* other) const
207 if (other->type_ < type_)
208 return other->depends(this);
210 // Actions executed by the same actor are always dependent
211 if (other->aid_ == aid_)
214 if (const auto* other_isend = dynamic_cast<const CommSendTransition*>(other))
215 return mbox_ == other_isend->mbox_;
217 if (dynamic_cast<const CommRecvTransition*>(other) != nullptr)
220 if (const auto* test = dynamic_cast<const CommTestTransition*>(other)) {
221 if (mbox_ != test->mbox_)
224 if ((aid_ != test->sender_) && (aid_ != test->receiver_))
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_)
232 return true; // DEP with other test transitions
235 if (const auto* wait = dynamic_cast<const CommWaitTransition*>(other)) {
239 if (mbox_ != wait->mbox_)
242 if ((aid_ != wait->sender_) && (aid_ != wait->receiver_))
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_)
250 return true; // DEP with other wait transitions
253 return false; // Comm transitions are INDEP with non-comm transitions
256 bool CommSendTransition::reversible_race(const Transition* other) const
258 xbt_assert(type_ == Type::COMM_ASYNC_SEND, "Unexpected transition type %s", to_c_str(type_));
260 return true; // CommSend is always enabled
263 } // namespace simgrid::mc