Logo AND Algorithmique Numérique Distribuée

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