blob: d7b54979b67b9818d0e17467717643b8ec55ee1f [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
17//
18// The Compute enum is now deprecated and it is now
David Becke6488712018-10-16 11:32:20 +010019// being replaced by BackendId
David Beck9df2d952018-10-10 15:11:44 +010020//
21enum 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
David Beck33f0ae02018-10-18 15:13:56 +0100114 // 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
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); }
135
David Beck9df2d952018-10-10 15:11:44 +0100136 const std::string& Get() const { return m_Id; }
137
Matteo Martincigh0c2b2892019-08-05 14:12:11 +0100138 bool IsEmpty() const { return m_Id.empty(); }
139 bool IsUndefined() const { return m_Id == GetComputeDeviceAsCString(Compute::Undefined); }
140
David Beck9df2d952018-10-10 15:11:44 +0100141private:
David Beck9df2d952018-10-10 15:11:44 +0100142 std::string m_Id;
143};
144
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000145} // namespace armnn
David Beckf0b48452018-10-19 15:20:56 +0100146
David Beck9df2d952018-10-10 15:11:44 +0100147namespace std
148{
149
150// make BackendId compatible with std hashtables by reusing the hash
Rob Hughes0214d7e2018-11-21 09:55:52 +0000151// function for strings.
152// Note this must come *before* the first use of unordered_set<BackendId>.
David Beck9df2d952018-10-10 15:11:44 +0100153template <>
154struct hash<armnn::BackendId>
155{
Matthew Benthamfa9a5b72019-12-05 10:37:09 +0000156 std::size_t operator()(const armnn::BackendId& id) const noexcept
David Beck9df2d952018-10-10 15:11:44 +0100157 {
158 std::hash<std::string> hasher;
159 return hasher(id.Get());
160 }
161};
162
163} // namespace std
Rob Hughes0214d7e2018-11-21 09:55:52 +0000164
165namespace armnn
166{
167
168inline std::ostream& operator<<(std::ostream& os, const BackendId& id)
169{
170 os << id.Get();
171 return os;
172}
173
174template <template <typename...> class TContainer, typename... TContainerTemplateArgs>
175std::ostream& operator<<(std::ostream& os,
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000176 const TContainer<BackendId, TContainerTemplateArgs...>& ids)
Rob Hughes0214d7e2018-11-21 09:55:52 +0000177{
178 os << '[';
179 for (const auto& id : ids) { os << id << " "; }
180 os << ']';
181 return os;
182}
183
Matteo Martincigh49124022019-01-11 13:25:59 +0000184using BackendIdVector = std::vector<BackendId>;
185using BackendIdSet = std::unordered_set<BackendId>;
Rob Hughes0214d7e2018-11-21 09:55:52 +0000186
187} // namespace armnn
188