Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix conflict detection between configs + history
[simgrid.git] / src / mc / explo / udpor / UnfoldingEvent.cpp
1 /* Copyright (c) 2008-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/explo/udpor/UnfoldingEvent.hpp"
7 #include "src/mc/explo/udpor/History.hpp"
8
9 #include <xbt/string.hpp>
10
11 namespace simgrid::mc::udpor {
12
13 UnfoldingEvent::UnfoldingEvent(std::initializer_list<const UnfoldingEvent*> init_list)
14     : UnfoldingEvent(EventSet(std::move(init_list)))
15 {
16 }
17
18 UnfoldingEvent::UnfoldingEvent(EventSet immediate_causes, std::shared_ptr<Transition> transition)
19     : associated_transition(std::move(transition)), immediate_causes(std::move(immediate_causes))
20 {
21 }
22
23 bool UnfoldingEvent::operator==(const UnfoldingEvent& other) const
24 {
25   // Intrinsic identity check
26   if (this == &other) {
27     return true;
28   }
29   // Two events are equivalent iff:
30   // 1. they have the same action
31   // 2. they have the same history
32   //
33   // NOTE: All unfolding event objects are created in reference to
34   // an `Unfolding` object which owns them. Hence, the references
35   // they contain to other events in the unfolding can
36   // be used as intrinsic identities (i.e. we don't need to
37   // recursively check if each of our causes has a `==` in
38   // the other event's causes)
39   return associated_transition->aid_ == other.associated_transition->aid_ &&
40          associated_transition->type_ == other.associated_transition->type_ &&
41          associated_transition->times_considered_ == other.associated_transition->times_considered_ &&
42          this->immediate_causes == other.immediate_causes;
43 }
44
45 std::string UnfoldingEvent::to_string() const
46 {
47   std::string dependencies_string;
48
49   dependencies_string += "[";
50   for (const auto* e : immediate_causes) {
51     dependencies_string += e->to_string();
52   }
53   dependencies_string += "]";
54
55   return xbt::string_printf("Actor %ld: %s (%zu dependencies: %s)", associated_transition->aid_,
56                             associated_transition->to_string().c_str(), immediate_causes.size(),
57                             dependencies_string.c_str());
58 }
59
60 EventSet UnfoldingEvent::get_history() const
61 {
62   EventSet local_config = get_local_config();
63   local_config.remove(this);
64   return local_config;
65 }
66
67 EventSet UnfoldingEvent::get_local_config() const
68 {
69   return History(this).get_all_events();
70 }
71
72 bool UnfoldingEvent::related_to(const UnfoldingEvent* other) const
73 {
74   return this->in_history_of(other) or other->in_history_of(this);
75 }
76
77 bool UnfoldingEvent::in_history_of(const UnfoldingEvent* other) const
78 {
79   return History(other).contains(this);
80 }
81
82 bool UnfoldingEvent::conflicts_with(const UnfoldingEvent* other) const
83 {
84   // Events that have a causal relation never are in conflict
85   // in an unfolding structure. Two events in conflict must
86   // not be contained in each other's histories
87   if (related_to(other)) {
88     return false;
89   }
90
91   const EventSet my_history      = get_local_config();
92   const EventSet other_history   = other->get_local_config();
93   const EventSet unique_to_me    = my_history.subtracting(other_history);
94   const EventSet unique_to_other = other_history.subtracting(my_history);
95
96   const bool conflicts_with_me    = std::any_of(unique_to_me.begin(), unique_to_me.end(),
97                                                 [&](const UnfoldingEvent* e) { return e->is_dependent_with(other); });
98   const bool conflicts_with_other = std::any_of(unique_to_other.begin(), unique_to_other.end(),
99                                                 [&](const UnfoldingEvent* e) { return e->is_dependent_with(this); });
100   return conflicts_with_me or conflicts_with_other;
101 }
102
103 bool UnfoldingEvent::conflicts_with_any(const EventSet& events) const
104 {
105   return std::any_of(events.begin(), events.end(), [&](const auto e) { return e->conflicts_with(this); });
106 }
107
108 bool UnfoldingEvent::immediately_conflicts_with(const UnfoldingEvent* other) const
109 {
110   // They have to be in conflict at a minimum
111   if (not conflicts_with(other)) {
112     return false;
113   }
114
115   auto combined_events = History(EventSet{this, other}).get_all_events();
116
117   // See the definition of immediate conflicts in the original paper on UDPOR
118   {
119     combined_events.remove(this);
120     if (not combined_events.is_valid_configuration()) {
121       return false;
122     }
123     combined_events.insert(this);
124   }
125
126   {
127     combined_events.remove(other);
128     if (not combined_events.is_valid_configuration()) {
129       return false;
130     }
131     combined_events.insert(other);
132   }
133
134   return true;
135 }
136
137 bool UnfoldingEvent::is_dependent_with(const Transition* t) const
138 {
139   return associated_transition->depends(t);
140 }
141
142 bool UnfoldingEvent::is_dependent_with(const UnfoldingEvent* other) const
143 {
144   return is_dependent_with(other->associated_transition.get());
145 }
146
147 } // namespace simgrid::mc::udpor