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