ArmNN
 21.02
BackendId.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <memory>
8 #include <ostream>
9 #include <set>
10 #include <string>
11 #include <unordered_set>
12 #include <vector>
13 
14 namespace armnn
15 {
16 
17 ///
18 /// The Compute enum is now deprecated and it is now
19 /// being replaced by BackendId
20 ///
21 enum class Compute
22 {
23  Undefined = 0,
24  /// CPU Execution: Reference C++ kernels
25  CpuRef = 1,
26  /// CPU Execution: NEON: ArmCompute
27  CpuAcc = 2,
28  /// GPU Execution: OpenCL: ArmCompute
29  GpuAcc = 3
30 };
31 
32 /// Deprecated function that will be removed together with
33 /// the Compute enum
34 constexpr char const* GetComputeDeviceAsCString(Compute compute)
35 {
36  switch (compute)
37  {
38  case armnn::Compute::CpuRef: return "CpuRef";
39  case armnn::Compute::CpuAcc: return "CpuAcc";
40  case armnn::Compute::GpuAcc: return "GpuAcc";
41  default: return "Unknown";
42  }
43 }
44 
45 /// Deprecated function that will be removed together with
46 /// the Compute enum
47 inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute)
48 {
49  for (const Compute& comp : compute)
50  {
51  os << GetComputeDeviceAsCString(comp) << " ";
52  }
53  return os;
54 }
55 
56 /// Deprecated function that will be removed together with
57 /// the Compute enum
58 inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute)
59 {
60  for (const Compute& comp : compute)
61  {
62  os << GetComputeDeviceAsCString(comp) << " ";
63  }
64  return os;
65 }
66 
67 /// Deprecated function that will be removed together with
68 /// the Compute enum
69 inline std::ostream& operator<<(std::ostream& os, const Compute& compute)
70 {
71  os << GetComputeDeviceAsCString(compute);
72  return os;
73 }
74 
75 class BackendId final
76 {
77 public:
79  BackendId(const std::string& id) : m_Id{id} {}
80  BackendId(const char* id) : m_Id{id} {}
81 
82 
83  BackendId(const BackendId& other) = default;
84  BackendId(BackendId&& other) = default;
85  BackendId& operator=(const BackendId& other) = default;
86  BackendId& operator=(BackendId&& other) = default;
88 
89  /// Deprecated function that will be removed together with
90  /// the Compute enum
91  BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {}
92 
93  operator std::string() const { return m_Id; }
94  BackendId& operator=(const std::string& other)
95  {
96  m_Id = other;
97  return *this;
98  }
99 
100  /// Deprecated function that will be removed together with
101  /// the Compute enum
103  {
104  BackendId temp{compute};
105  std::swap(temp.m_Id, m_Id);
106  return *this;
107  }
108 
109  bool operator==(const BackendId& other) const
110  {
111  return m_Id == other.m_Id;
112  }
113 
114  /// comparison against objects from which the
115  /// BackendId can be constructed
116  template <typename O>
117  bool operator==(const O& other) const
118  {
119  BackendId temp{other};
120  return *this == temp;
121  }
122 
123  template <typename O>
124  bool operator!=(const O& other) const
125  {
126  return !(*this == other);
127  }
128 
129  bool operator<(const BackendId& other) const
130  {
131  return m_Id < other.m_Id;
132  }
133 
134  bool IsCpuRef() const { return m_Id == GetComputeDeviceAsCString(Compute::CpuRef); }
135 
136  const std::string& Get() const { return m_Id; }
137 
138  bool IsEmpty() const { return m_Id.empty(); }
139  bool IsUndefined() const { return m_Id == GetComputeDeviceAsCString(Compute::Undefined); }
140 
141 private:
142  std::string m_Id;
143 };
144 
145 } // namespace armnn
146 
147 namespace std
148 {
149 
150 /// make BackendId compatible with std hashtables by reusing the hash
151 /// function for strings.
152 /// Note this must come *before* the first use of unordered_set<BackendId>.
153 template <>
154 struct hash<armnn::BackendId>
155 {
156  std::size_t operator()(const armnn::BackendId& id) const noexcept
157  {
158  std::hash<std::string> hasher;
159  return hasher(id.Get());
160  }
161 };
162 
163 } // namespace std
164 
165 namespace armnn
166 {
167 
168 namespace profiling
169 {
170  // Static constant describing ArmNN as a dummy backend
171  static const BackendId BACKEND_ID("ARMNN");
172 } // profiling
173 
174 inline std::ostream& operator<<(std::ostream& os, const BackendId& id)
175 {
176  os << id.Get();
177  return os;
178 }
179 
180 template <template <typename...> class TContainer, typename... TContainerTemplateArgs>
181 std::ostream& operator<<(std::ostream& os,
183 {
184  os << '[';
185  for (const auto& id : ids) { os << id << " "; }
186  os << ']';
187  return os;
188 }
189 
190 using BackendIdVector = std::vector<BackendId>;
191 using BackendIdSet = std::unordered_set<BackendId>;
192 
193 } // namespace armnn
194 
CPU Execution: Reference C++ kernels.
void swap(OriginsDescriptor &first, OriginsDescriptor &second)
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
std::vector< BackendId > BackendIdVector
Definition: BackendId.hpp:190
bool IsCpuRef() const
Definition: BackendId.hpp:134
std::ostream & operator<<(std::ostream &os, const std::vector< Compute > &compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:47
Copyright (c) 2021 ARM Limited and Contributors.
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char > > TContainer
std::size_t operator()(const armnn::BackendId &id) const noexcept
Definition: BackendId.hpp:156
Compute
The Compute enum is now deprecated and it is now being replaced by BackendId.
Definition: BackendId.hpp:21
constexpr char const * GetComputeDeviceAsCString(Compute compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:34
GPU Execution: OpenCL: ArmCompute.
BackendId(const std::string &id)
Definition: BackendId.hpp:79
BackendId(Compute compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:91
bool operator<(const BackendId &other) const
Definition: BackendId.hpp:129
BackendId & operator=(const std::string &other)
Definition: BackendId.hpp:94
BackendId & operator=(Compute compute)
Deprecated function that will be removed together with the Compute enum.
Definition: BackendId.hpp:102
bool IsEmpty() const
Definition: BackendId.hpp:138
BackendId(const char *id)
Definition: BackendId.hpp:80
CPU Execution: NEON: ArmCompute.
const std::string & Get() const
Definition: BackendId.hpp:136
bool operator==(const O &other) const
comparison against objects from which the BackendId can be constructed
Definition: BackendId.hpp:117
bool operator!=(const O &other) const
Definition: BackendId.hpp:124
bool IsUndefined() const
Definition: BackendId.hpp:139
bool operator==(const BackendId &other) const
Definition: BackendId.hpp:109