ArmNN
 25.11
Loading...
Searching...
No Matches
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
14namespace armnn
15{
16
17///
18/// The Compute enum is now deprecated and it is now
19/// being replaced by BackendId
20///
21enum class Compute
22{
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
32};
33
34/// Deprecated function that will be removed together with
35/// the Compute enum
36constexpr 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
50inline 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
61inline 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
72inline std::ostream& operator<<(std::ostream& os, const Compute& compute)
73{
74 os << GetComputeDeviceAsCString(compute);
75 return os;
76}
77
78class BackendId final
79{
80public:
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(); }
145
146private:
147 std::string m_Id;
148};
149
150} // namespace armnn
151
152namespace 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>.
158template <>
159struct 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
170namespace armnn
171{
172
173namespace profiling
174{
175// Static constant describing ArmNN as a dummy backend
176static const BackendId BACKEND_ID("ARMNN");
177} // profiling
178
179inline std::ostream& operator<<(std::ostream& os, const BackendId& id)
180{
181 os << id.Get();
182 return os;
183}
184
185template <template <typename...> class TContainer, typename... TContainerTemplateArgs>
186std::ostream& operator<<(std::ostream& os,
188{
189 os << '[';
190 for (const auto& id : ids) { os << id << " "; }
191 os << ']';
192 return os;
193}
194
195using BackendIdVector = std::vector<BackendId>;
196using BackendIdSet = std::unordered_set<BackendId>;
197
198} // namespace armnn
BackendId(const std::string &id)
Definition BackendId.hpp:82
bool IsGpuAcc() const
bool operator<(const BackendId &other) const
BackendId(const char *id)
Definition BackendId.hpp:83
BackendId(BackendId &&other)=default
BackendId & operator=(const std::string &other)
Definition BackendId.hpp:97
BackendId & operator=(const BackendId &other)=default
BackendId(const BackendId &other)=default
BackendId & operator=(Compute compute)
Deprecated function that will be removed together with the Compute enum.
BackendId & operator=(BackendId &&other)=default
const std::string & Get() const
bool IsEmpty() const
bool IsCpuRef() const
BackendId(Compute compute)
Deprecated function that will be removed together with the Compute enum.
Definition BackendId.hpp:94
bool operator==(const BackendId &other) const
bool IsCpuAcc() const
bool operator==(const O &other) const
comparison against objects from which the BackendId can be constructed
bool IsUndefined() const
bool operator!=(const O &other) const
Copyright (c) 2021 ARM Limited and Contributors.
std::unordered_set< BackendId > BackendIdSet
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
Compute
The Compute enum is now deprecated and it is now being replaced by BackendId.
Definition BackendId.hpp:22
@ CpuAcc
CPU Execution: NEON: ArmCompute.
Definition BackendId.hpp:27
@ CpuRef
CPU Execution: Reference C++ kernels.
Definition BackendId.hpp:25
@ TosaRef
CPU Execution: TOSA Reference Model.
Definition BackendId.hpp:31
@ GpuAcc
GPU Execution: OpenCL: ArmCompute.
Definition BackendId.hpp:29
mapbox::util::variant< std::vector< float >, std::vector< int >, std::vector< unsigned char >, std::vector< int8_t > > TContainer
std::size_t operator()(const armnn::BackendId &id) const noexcept