ArmNN  NotReleased
BackendOptions.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "BackendId.hpp"
9 #include <cassert>
10 
11 namespace armnn
12 {
13 
14 
17 {
18 private:
19  template<typename T>
20  struct CheckAllowed
21  {
22  static const bool value = std::is_same<T, int>::value ||
23  std::is_same<T, float>::value ||
24  std::is_same<T, bool>::value ||
25  std::is_same<T, std::string>::value ||
26  std::is_same<T, const char*>::value;
27  };
28 public:
29 
30  // Very basic type safe variant
31  class Var
32  {
33 
34  public:
36  explicit Var(int i) : m_Vals(i), m_Type(VarTypes::Integer) {};
37  explicit Var(float f) : m_Vals(f), m_Type(VarTypes::Float) {};
38  explicit Var(bool b) : m_Vals(b), m_Type(VarTypes::Boolean) {};
39  explicit Var(const char* s) : m_Vals(s), m_Type(VarTypes::String) {};
40  explicit Var(std::string s) : m_Vals(s), m_Type(VarTypes::String) {};
41 
42  //Disallow implicit conversions from types not explicitly allowed below.
43  template<typename DisallowedType>
44  Var(DisallowedType)
45  {
46  static_assert(CheckAllowed<DisallowedType>::value, "Type is not allowed for Var<DisallowedType>.");
47  assert(false && "Unreachable code");
48  }
49 
51  Var(const Var& other)
52  : m_Type(other.m_Type)
53  {
54  switch(m_Type)
55  {
56  case VarTypes::String:
57  {
58  new (&m_Vals.s) std::string(other.m_Vals.s);
59  break;
60  }
61  default:
62  {
63  DoOp(other, [](auto& a, auto& b)
64  {
65  a = b;
66  });
67  break;
68  }
69  }
70  }
71 
73  Var& operator=(const Var& other)
74  {
75  // Destroy existing string
76  if (m_Type == VarTypes::String)
77  {
78  Destruct(m_Vals.s);
79  }
80 
81  m_Type = other.m_Type;
82  switch(m_Type)
83  {
84  case VarTypes::String:
85  {
86 
87  new (&m_Vals.s) std::string(other.m_Vals.s);
88  break;
89  }
90  default:
91  {
92  DoOp(other, [](auto& a, auto& b)
93  {
94  a = b;
95  });
96  break;
97  }
98  }
99 
100  return *this;
101  };
102 
104  bool IsBool() const { return m_Type == VarTypes::Boolean; }
105  bool IsInt() const { return m_Type == VarTypes::Integer; }
106  bool IsFloat() const { return m_Type == VarTypes::Float; }
107  bool IsString() const { return m_Type == VarTypes::String; }
108 
110  bool AsBool() const { assert(IsBool()); return m_Vals.b; }
111  int AsInt() const { assert(IsInt()); return m_Vals.i; }
112  float AsFloat() const { assert(IsFloat()); return m_Vals.f; }
113  std::string AsString() const { assert(IsString()); return m_Vals.s; }
114 
117  {
118  DoOp(*this, [this](auto& a, auto&)
119  {
120  Destruct(a);
121  });
122  }
123  private:
124  template<typename Func>
125  void DoOp(const Var& other, Func func)
126  {
127  if (other.IsBool())
128  {
129  func(m_Vals.b, other.m_Vals.b);
130  }
131  else if (other.IsInt())
132  {
133  func(m_Vals.i, other.m_Vals.i);
134  }
135  else if (other.IsFloat())
136  {
137  func(m_Vals.f, other.m_Vals.f);
138  }
139  else if (other.IsString())
140  {
141  func(m_Vals.s, other.m_Vals.s);
142  }
143  }
144 
145  template<typename Destructable>
146  void Destruct(Destructable& d)
147  {
148  if (std::is_destructible<Destructable>::value)
149  {
150  d.~Destructable();
151  }
152  }
153 
154  private:
156  enum class VarTypes
157  {
158  Boolean,
159  Integer,
160  Float,
161  String,
162  };
163 
164  // Union of potential type values.
165  union Vals
166  {
167  int i;
168  float f;
169  bool b;
170  std::string s;
171 
172  Vals(){}
173  ~Vals(){}
174 
175  explicit Vals(int i) : i(i) {};
176  explicit Vals(float f) : f(f) {};
177  explicit Vals(bool b) : b(b) {};
178  explicit Vals(const char* s) : s(std::string(s)) {}
179  explicit Vals(std::string s) : s(s) {}
180  };
181 
182  Vals m_Vals;
183  VarTypes m_Type;
184  };
185 
187  {
188  public:
189  BackendOption(std::string name, bool value)
190  : m_Name(name), m_Value(value)
191  {}
192  BackendOption(std::string name, int value)
193  : m_Name(name), m_Value(value)
194  {}
195  BackendOption(std::string name, float value)
196  : m_Name(name), m_Value(value)
197  {}
198  BackendOption(std::string name, std::string value)
199  : m_Name(name), m_Value(value)
200  {}
201  BackendOption(std::string name, const char* value)
202  : m_Name(name), m_Value(value)
203  {}
204 
205  template<typename DisallowedType>
206  BackendOption(std::string, DisallowedType)
207  : m_Value(0)
208  {
209  static_assert(CheckAllowed<DisallowedType>::value, "Type is not allowed for BackendOption.");
210  assert(false && "Unreachable code");
211  }
212 
213  BackendOption(const BackendOption& other) = default;
214  BackendOption(BackendOption&& other) = default;
215  BackendOption& operator=(const BackendOption& other) = default;
216  BackendOption& operator=(BackendOption&& other) = default;
217  ~BackendOption() = default;
218 
219  std::string GetName() const { return m_Name; }
220  Var GetValue() const { return m_Value; }
221 
222  private:
223  std::string m_Name;
224  Var m_Value;
225  };
226 
227  explicit BackendOptions(BackendId backend)
228  : m_TargetBackend(backend)
229  {}
230 
231  BackendOptions(BackendId backend, std::initializer_list<BackendOption> options)
232  : m_TargetBackend(backend)
233  , m_Options(options)
234  {}
235 
236  BackendOptions(const BackendOptions& other) = default;
237  BackendOptions(BackendOptions&& other) = default;
238  BackendOptions& operator=(const BackendOptions& other) = default;
239  BackendOptions& operator=(BackendOptions&& other) = default;
240 
241  void AddOption(BackendOption&& option)
242  {
243  m_Options.push_back(option);
244  }
245 
246  void AddOption(const BackendOption& option)
247  {
248  m_Options.push_back(option);
249  }
250 
251  const BackendId& GetBackendId() const noexcept { return m_TargetBackend; }
252  size_t GetOptionCount() const noexcept { return m_Options.size(); }
253  const BackendOption& GetOption(size_t idx) const { return m_Options[idx]; }
254 
255 private:
257  BackendId m_TargetBackend;
258 
260  std::vector<BackendOption> m_Options;
261 };
262 
263 } //namespace armnn
const BackendId & GetBackendId() const noexcept
Var & operator=(const Var &other)
Copy operator.
BackendOption(std::string name, const char *value)
BackendOption(std::string name, std::string value)
std::string AsString() const
BackendOption(std::string name, float value)
BackendOption(std::string, DisallowedType)
BackendOptions(BackendId backend)
void AddOption(const BackendOption &option)
void AddOption(BackendOption &&option)
size_t GetOptionCount() const noexcept
Struct for the users to pass backend specific options.
Var(const Var &other)
Copy Construct.
bool AsBool() const
Value getters.
BackendOptions & operator=(const BackendOptions &other)=default
const BackendOption & GetOption(size_t idx) const
BackendOption(std::string name, bool value)
BackendOption(std::string name, int value)
BackendOptions(BackendId backend, std::initializer_list< BackendOption > options)
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
bool IsBool() const
Type getters.
Var(int i)
Constructors.