blob: 45e2ca8d86a307f523ed595111d7586195e8c8d5 [file] [log] [blame]
David Beck9df2d952018-10-10 15:11:44 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#pragma once
6
Matthew Bentham313e1c82019-03-25 17:37:47 +00007#include <memory>
David Beck3e9e1152018-10-17 14:17:50 +01008#include <ostream>
David Beck9df2d952018-10-10 15:11:44 +01009#include <set>
David Beck9df2d952018-10-10 15:11:44 +010010#include <string>
Matthew Bentham313e1c82019-03-25 17:37:47 +000011#include <unordered_set>
David Beck3e9e1152018-10-17 14:17:50 +010012#include <vector>
David Beck9df2d952018-10-10 15:11:44 +010013
14namespace armnn
15{
16
Ryan OShea2bbfaa72020-02-12 16:15:27 +000017///
18/// The Compute enum is now deprecated and it is now
19/// being replaced by BackendId
20///
David Beck9df2d952018-10-10 15:11:44 +010021enum class Compute
22{
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000023 Undefined = 0,
David Beck9df2d952018-10-10 15:11:44 +010024 /// CPU Execution: Reference C++ kernels
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000025 CpuRef = 1,
David Beck9df2d952018-10-10 15:11:44 +010026 /// CPU Execution: NEON: ArmCompute
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000027 CpuAcc = 2,
David Beck9df2d952018-10-10 15:11:44 +010028 /// GPU Execution: OpenCL: ArmCompute
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000029 GpuAcc = 3
David Beck9df2d952018-10-10 15:11:44 +010030};
31
David Becke6488712018-10-16 11:32:20 +010032/// Deprecated function that will be removed together with
33/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010034constexpr 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
David Becke6488712018-10-16 11:32:20 +010045/// Deprecated function that will be removed together with
46/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010047inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute)
48{
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000049 for (const Compute& comp : compute)
50 {
David Beck9df2d952018-10-10 15:11:44 +010051 os << GetComputeDeviceAsCString(comp) << " ";
52 }
53 return os;
54}
55
David Becke6488712018-10-16 11:32:20 +010056/// Deprecated function that will be removed together with
57/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010058inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute)
59{
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000060 for (const Compute& comp : compute)
61 {
David Beck9df2d952018-10-10 15:11:44 +010062 os << GetComputeDeviceAsCString(comp) << " ";
63 }
64 return os;
65}
66
David Becke6488712018-10-16 11:32:20 +010067/// Deprecated function that will be removed together with
68/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010069inline std::ostream& operator<<(std::ostream& os, const Compute& compute)
70{
71 os << GetComputeDeviceAsCString(compute);
72 return os;
73}
74
75class BackendId final
76{
77public:
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000078 BackendId() : m_Id(GetComputeDeviceAsCString(Compute::Undefined)) {}
David Beck9df2d952018-10-10 15:11:44 +010079 BackendId(const std::string& id) : m_Id{id} {}
80 BackendId(const char* id) : m_Id{id} {}
David Becke6488712018-10-16 11:32:20 +010081
Derek Lamberti836b27b2019-11-20 10:51:57 +000082
83 BackendId(const BackendId& other) = default;
84 BackendId(BackendId&& other) = default;
85 BackendId& operator=(const BackendId& other) = default;
86 BackendId& operator=(BackendId&& other) = default;
87 ~BackendId(){}
88
David Becke6488712018-10-16 11:32:20 +010089 /// Deprecated function that will be removed together with
90 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010091 BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {}
92
93 operator std::string() const { return m_Id; }
David Beck9df2d952018-10-10 15:11:44 +010094 BackendId& operator=(const std::string& other)
95 {
96 m_Id = other;
97 return *this;
98 }
99
David Becke6488712018-10-16 11:32:20 +0100100 /// Deprecated function that will be removed together with
101 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +0100102 BackendId& operator=(Compute compute)
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
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000114 /// comparison against objects from which the
115 /// BackendId can be constructed
David Beck33f0ae02018-10-18 15:13:56 +0100116 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
David Beck9df2d952018-10-10 15:11:44 +0100129 bool operator<(const BackendId& other) const
130 {
131 return m_Id < other.m_Id;
132 }
133
Matteo Martincighadddddb2019-01-24 14:06:23 +0000134 bool IsCpuRef() const { return m_Id == GetComputeDeviceAsCString(Compute::CpuRef); }
Pablo Tello5f3dc252021-09-01 15:36:23 +0100135 bool IsCpuAcc() const { return m_Id == GetComputeDeviceAsCString(Compute::CpuAcc); }
136 bool IsGpuAcc() const { return m_Id == GetComputeDeviceAsCString(Compute::GpuAcc); }
Matteo Martincighadddddb2019-01-24 14:06:23 +0000137
David Beck9df2d952018-10-10 15:11:44 +0100138 const std::string& Get() const { return m_Id; }
139
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100140 bool IsEmpty() const { return m_Id.empty(); }
141 bool IsUndefined() const { return m_Id == GetComputeDeviceAsCString(Compute::Undefined); }
142
David Beck9df2d952018-10-10 15:11:44 +0100143private:
David Beck9df2d952018-10-10 15:11:44 +0100144 std::string m_Id;
145};
146
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000147} // namespace armnn
David Beckf0b48452018-10-19 15:20:56 +0100148
David Beck9df2d952018-10-10 15:11:44 +0100149namespace std
150{
151
Ryan OShea2bbfaa72020-02-12 16:15:27 +0000152/// make BackendId compatible with std hashtables by reusing the hash
153/// function for strings.
154/// Note this must come *before* the first use of unordered_set<BackendId>.
David Beck9df2d952018-10-10 15:11:44 +0100155template <>
156struct hash<armnn::BackendId>
157{
Matthew Benthamfa9a5b72019-12-05 10:37:09 +0000158 std::size_t operator()(const armnn::BackendId& id) const noexcept
David Beck9df2d952018-10-10 15:11:44 +0100159 {
160 std::hash<std::string> hasher;
161 return hasher(id.Get());
162 }
163};
164
165} // namespace std
Rob Hughes0214d7e2018-11-21 09:55:52 +0000166
167namespace armnn
168{
169
Keith Davise394bd92019-12-02 15:12:19 +0000170namespace profiling
171{
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000172// Static constant describing ArmNN as a dummy backend
173static const BackendId BACKEND_ID("ARMNN");
Keith Davise394bd92019-12-02 15:12:19 +0000174} // profiling
175
Rob Hughes0214d7e2018-11-21 09:55:52 +0000176inline std::ostream& operator<<(std::ostream& os, const BackendId& id)
177{
178 os << id.Get();
179 return os;
180}
181
182template <template <typename...> class TContainer, typename... TContainerTemplateArgs>
183std::ostream& operator<<(std::ostream& os,
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000184 const TContainer<BackendId, TContainerTemplateArgs...>& ids)
Rob Hughes0214d7e2018-11-21 09:55:52 +0000185{
186 os << '[';
187 for (const auto& id : ids) { os << id << " "; }
188 os << ']';
189 return os;
190}
191
Matteo Martincigh49124022019-01-11 13:25:59 +0000192using BackendIdVector = std::vector<BackendId>;
193using BackendIdSet = std::unordered_set<BackendId>;
Rob Hughes0214d7e2018-11-21 09:55:52 +0000194
195} // namespace armnn