TVM  0.9.4
ProblemComputationData.h
Go to the documentation of this file.
1 /* Copyright 2017-2020 CNRS-AIST JRL and CNRS-UM LIRMM */
2 
3 #pragma once
4 
5 #include <tvm/api.h>
6 #include <tvm/defs.h>
7 
8 #include <tvm/VariableVector.h>
11 
12 #include <queue>
13 #include <vector>
14 
15 namespace tvm::scheme::internal
16 {
17 
27 {
28 public:
29  class EventQueue : public std::queue<ProblemDefinitionEvent>
30  {
31  public:
32  using std::queue<ProblemDefinitionEvent>::queue;
33 
34  using std::queue<ProblemDefinitionEvent>::c;
35  };
36 
37  virtual ~ProblemComputationData() = default;
38 
39  int solverId() const;
40 
44  bool addVariable(VariablePtr var);
48  void addVariable(const VariableVector & vars);
53  bool removeVariable(Variable * v);
57  void removeVariable(const VariableVector & vars);
59  const VariableVector & variables() const;
60 
62  void setVariablesToSolution();
63 
67  void addEvent(const ProblemDefinitionEvent & e);
69  ProblemDefinitionEvent popEvent();
71  bool hasEvents() const;
73  const EventQueue::container_type & events() const;
75  void clearEvents();
77  void clearVariables();
78 
79 protected:
80  ProblemComputationData(int solverId);
82 
85 
88 
91 
92 private:
93  int solverId_;
94 };
95 
96 inline int ProblemComputationData::solverId() const { return solverId_; }
97 
99 
101 {
102  for(const auto & v : vars.variables())
103  addVariable(v);
104 }
105 
107 
109 {
110  for(const auto & v : vars.variables())
111  removeVariable(v.get());
112 }
113 
115 
117 
119 {
120  // By default, the event will be effectively added.
121  bool add = true;
122 
123  // If this event is a task removal, it is useless to process other events that
124  // would be in the event queue for the same task. We remove those events from
125  // the queue.
127  {
128  for(auto it = events_.c.begin(); it != events_.c.end();)
129  {
130  if(it->emitter() == e.emitter() && it->type() != ProblemDefinitionEvent::Type::TaskRemoval)
131  {
132  // In case the addition of the same task is in the event queue: since
133  // we are removing it, it is not necessary to add the removal event.
135  {
136  assert(add && "The task was added twice");
137  add = false;
138  }
139  it = events_.c.erase(it);
140  }
141  else
142  {
143  ++it;
144  }
145  }
146  }
147  if(add)
148  {
149  events_.push(e);
150  }
151 }
152 
154 {
155  assert(hasEvents());
156  auto e = events_.front();
157  events_.pop();
158  return e;
159 }
160 
161 inline bool ProblemComputationData::hasEvents() const { return !events_.empty(); }
162 
163 inline const ProblemComputationData::EventQueue::container_type & ProblemComputationData::events() const
164 { return events_.c; }
165 
167 
169 
170 inline ProblemComputationData::ProblemComputationData(int solverId) : solverId_(solverId) {}
171 } // namespace tvm::scheme::internal
#define TVM_DLLAPI
Definition: api.h:35
Definition: VariableVector.h:41
const std::vector< VariablePtr > & variables() const
Definition: Variable.h:49
Definition: VariableCountingVector.h:30
bool remove(const Variable &v)
const VariableVector & variables() const
Definition: ProblemComputationData.h:30
Definition: ProblemComputationData.h:27
void addEvent(const ProblemDefinitionEvent &e)
Definition: ProblemComputationData.h:118
const EventQueue::container_type & events() const
Definition: ProblemComputationData.h:163
EventQueue events_
Definition: ProblemComputationData.h:90
tvm::internal::VariableCountingVector variables_
Definition: ProblemComputationData.h:87
ProblemDefinitionEvent popEvent()
Definition: ProblemComputationData.h:153
const VariableVector & variables() const
Definition: ProblemComputationData.h:114
bool removeVariable(Variable *v)
Definition: ProblemComputationData.h:106
void clearVariables()
Definition: ProblemComputationData.h:168
void setVariablesToSolution()
Definition: ProblemComputationData.h:116
void clearEvents()
Definition: ProblemComputationData.h:166
bool hasEvents() const
Definition: ProblemComputationData.h:161
bool addVariable(VariablePtr var)
Definition: ProblemComputationData.h:98
virtual void setVariablesToSolution_(tvm::internal::VariableCountingVector &x)=0
int solverId() const
Definition: ProblemComputationData.h:96
Definition: ProblemDefinitionEvent.h:19
void const * emitter() const
Definition: ProblemDefinitionEvent.h:48
Type type() const
Definition: ProblemDefinitionEvent.h:47
Definition: Assignment.h:23
std::shared_ptr< Variable > VariablePtr
Definition: defs.h:65