Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
361ed36e2f1d7a93fcd2e7759cb20ae39332c48d
[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 namespace simgrid::mc::udpor {
10
11 UnfoldingEvent::UnfoldingEvent(std::initializer_list<const UnfoldingEvent*> init_list)
12     : UnfoldingEvent(EventSet(std::move(init_list)))
13 {
14 }
15
16 UnfoldingEvent::UnfoldingEvent(EventSet immediate_causes, std::shared_ptr<Transition> transition)
17     : associated_transition(std::move(transition)), immediate_causes(std::move(immediate_causes))
18 {
19 }
20
21 bool UnfoldingEvent::operator==(const UnfoldingEvent& other) const
22 {
23   // Must be run by the same actor
24   if (associated_transition->aid_ != other.associated_transition->aid_)
25     return false;
26
27   // If run by the same actor, must be the same _step_ of that actor's
28   // execution
29
30   // TODO: Add in information to determine which step in the sequence this actor was executed
31
32   // All unfolding event objects are created in reference to
33   // an Unfolding object which owns them. Hence, the references
34   // they contain to other events in the unfolding can
35   // be used as intrinsic identities (i.e. we don't need to
36   // recursively check if each of our causes has a `==` in
37   // the other event's causes)
38   return this->immediate_causes == other.immediate_causes;
39 }
40
41 EventSet UnfoldingEvent::get_history() const
42 {
43   return History(this).get_all_events();
44 }
45
46 bool UnfoldingEvent::related_to(const UnfoldingEvent* other) const
47 {
48   return this->in_history_of(other) or other->in_history_of(this);
49 }
50
51 bool UnfoldingEvent::in_history_of(const UnfoldingEvent* other) const
52 {
53   return History(other).contains(this);
54 }
55
56 bool UnfoldingEvent::conflicts_with(const UnfoldingEvent* other) const
57 {
58   // Events that have a causal relation never are in conflict
59   // in an unfolding structure. Two events in conflict must
60   // not be contained in each other's histories
61   if (related_to(other)) {
62     return false;
63   }
64
65   const EventSet my_history      = get_history();
66   const EventSet other_history   = other->get_history();
67   const EventSet unique_to_me    = my_history.subtracting(other_history);
68   const EventSet unique_to_other = other_history.subtracting(my_history);
69
70   const bool conflicts_with_me    = std::any_of(unique_to_me.begin(), unique_to_me.end(),
71                                                 [&](const UnfoldingEvent* e) { return e->is_dependent_with(other); });
72   const bool conflicts_with_other = std::any_of(unique_to_other.begin(), unique_to_other.end(),
73                                                 [&](const UnfoldingEvent* e) { return e->is_dependent_with(this); });
74   return conflicts_with_me or conflicts_with_other;
75 }
76
77 bool UnfoldingEvent::conflicts_with(const Configuration& config) const
78 {
79   // A configuration is itself already conflict-free. Thus, it is
80   // simply a matter of testing whether or not the transition associated
81   // with the event is dependent with any already in `config` that are
82   // OUTSIDE this event's history (in an unfolding, events only conflict
83   // if they are not related)
84   const EventSet potential_conflicts = config.get_events().subtracting(get_history());
85   return std::any_of(potential_conflicts.cbegin(), potential_conflicts.cend(),
86                      [&](const UnfoldingEvent* e) { return this->is_dependent_with(e); });
87 }
88
89 bool UnfoldingEvent::immediately_conflicts_with(const UnfoldingEvent* other) const
90 {
91   // Computes "this #ⁱ other"
92   // They have to be in conflict at a minimum
93   if (not conflicts_with(other)) {
94     return false;
95   }
96
97   auto combined_events = History(EventSet{this, other}).get_all_events();
98
99   // See the definition of immediate conflicts in the original paper on UDPOR
100   {
101     combined_events.remove(this);
102     if (not combined_events.is_valid_configuration()) {
103       return false;
104     }
105     combined_events.insert(this);
106   }
107
108   {
109     combined_events.remove(other);
110     if (not combined_events.is_valid_configuration()) {
111       return false;
112     }
113     combined_events.insert(other);
114   }
115
116   return true;
117 }
118
119 bool UnfoldingEvent::is_dependent_with(const Transition* t) const
120 {
121   return associated_transition->depends(t);
122 }
123
124 bool UnfoldingEvent::is_dependent_with(const UnfoldingEvent* other) const
125 {
126   return is_dependent_with(other->associated_transition.get());
127 }
128
129 } // namespace simgrid::mc::udpor