TVM  0.9.4
SchemeAbilities.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 
10 
11 #include <iostream>
12 #include <map>
13 #include <vector>
14 
15 namespace tvm
16 {
17 
18 namespace scheme
19 {
20 
21 namespace internal
22 {
23 
25 inline constexpr int GeneralLevel = -1;
27 inline constexpr int NoLimit = GeneralLevel;
28 
33 {
34 public:
40  LevelAbilities(bool inequality, const std::vector<requirements::ViolationEvaluationType> & types);
41 
47  template<class RequirementsPtr>
48  void check(const ConstraintPtr & c, const RequirementsPtr & req, bool emitWarnings = true) const;
49 
50 private:
51  bool inequalities_;
52  std::vector<requirements::ViolationEvaluationType> evaluationTypes_;
53 };
54 
59 {
60 public:
74  SchemeAbilities(int numberOfLevels, const std::map<int, LevelAbilities> & abilities, bool scalarization = false);
75 
81  template<class RequirementsPtr>
82  void check(const ConstraintPtr & c, const RequirementsPtr & req, bool emitWarnings = true) const;
83 
84 private:
85  int numberOfLevels_;
86  bool scalarization_;
87  std::map<int, LevelAbilities> abilities_;
88 };
89 
90 template<class RequirementsPtr>
91 inline void LevelAbilities::check(const ConstraintPtr & c, const RequirementsPtr & req, bool /*emitWarnings*/) const
92 {
93  // checking the constraint type
94  if(c->type() != constraint::Type::EQUAL && !inequalities_)
95  throw std::runtime_error("This level does not handle inequality constraints.");
96 
97  // checking the evaluation type
98  auto it = std::find(evaluationTypes_.begin(), evaluationTypes_.end(), req->violationEvaluation().value());
99  if(it == evaluationTypes_.end())
100  throw std::runtime_error("This level does not handle the required violation evaluation value.");
101 }
102 
103 template<class RequirementsPtr>
104 inline void SchemeAbilities::check(const ConstraintPtr & c, const RequirementsPtr & req, bool emitWarnings) const
105 {
106  // check priority level value
107  int p = req->priorityLevel().value();
108  if(numberOfLevels_ > 0 && p >= numberOfLevels_)
109  {
110  if(scalarization_)
111  {
112  if(emitWarnings)
113  {
114  std::cout << "Warning: required priority level (" << p
115  << ") cannot be handled as a strict hierarchy level by the resolution scheme. "
116  << "Using scalarization to revert to weighted approach.";
117  }
118  }
119  else
120  {
121  std::stringstream s;
122  s << "This resolution scheme can handle priorities level up to" << numberOfLevels_ - 1
123  << ". It cannot process required level (" << p << ")." << std::endl;
124  throw std::runtime_error(s.str());
125  }
126  }
127 
128  // check abilities of the required priority level
129  auto it = abilities_.find(p);
130  if(it != abilities_.end())
131  it->second.check(c, req, emitWarnings);
132  else
133  abilities_.at(GeneralLevel).check(c, req, emitWarnings);
134 }
135 
136 } // namespace internal
137 
138 } // namespace scheme
139 
140 } // namespace tvm
#define TVM_DLLAPI
Definition: api.h:35
Definition: SchemeAbilities.h:33
LevelAbilities(bool inequality, const std::vector< requirements::ViolationEvaluationType > &types)
void check(const ConstraintPtr &c, const RequirementsPtr &req, bool emitWarnings=true) const
Definition: SchemeAbilities.h:91
Definition: SchemeAbilities.h:59
SchemeAbilities(int numberOfLevels, const std::map< int, LevelAbilities > &abilities, bool scalarization=false)
void check(const ConstraintPtr &c, const RequirementsPtr &req, bool emitWarnings=true) const
Definition: SchemeAbilities.h:104
constexpr int NoLimit
Definition: SchemeAbilities.h:27
constexpr int GeneralLevel
Definition: SchemeAbilities.h:25
Definition: Clock.h:12
std::shared_ptr< constraint::abstract::Constraint > ConstraintPtr
Definition: defs.h:56