blob: 72248bca34577f3452f8b6a5e2e98d84f4da11da [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
David Beck3e9e1152018-10-17 14:17:50 +01007#include <ostream>
David Beck9df2d952018-10-10 15:11:44 +01008#include <set>
9#include <unordered_set>
10#include <string>
11#include <memory>
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{
23 /// CPU Execution: Reference C++ kernels
24 CpuRef = 0,
25 /// CPU Execution: NEON: ArmCompute
26 CpuAcc = 1,
27 /// GPU Execution: OpenCL: ArmCompute
28 GpuAcc = 2,
29 Undefined = 5
30};
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{
49 for (const Compute& comp : compute) {
50 os << GetComputeDeviceAsCString(comp) << " ";
51 }
52 return os;
53}
54
David Becke6488712018-10-16 11:32:20 +010055/// Deprecated function that will be removed together with
56/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010057inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute)
58{
59 for (const Compute& comp : compute) {
60 os << GetComputeDeviceAsCString(comp) << " ";
61 }
62 return os;
63}
64
David Becke6488712018-10-16 11:32:20 +010065/// Deprecated function that will be removed together with
66/// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010067inline std::ostream& operator<<(std::ostream& os, const Compute& compute)
68{
69 os << GetComputeDeviceAsCString(compute);
70 return os;
71}
72
David Beck33f0ae02018-10-18 15:13:56 +010073struct UninitializedBackendId {};
74
David Beck9df2d952018-10-10 15:11:44 +010075class BackendId final
76{
77public:
David Beckf0b48452018-10-19 15:20:56 +010078 BackendId() { GetComputeDeviceAsCString(Compute::Undefined); }
David Beck33f0ae02018-10-18 15:13:56 +010079 BackendId(UninitializedBackendId) { GetComputeDeviceAsCString(Compute::Undefined); }
David Beck9df2d952018-10-10 15:11:44 +010080 BackendId(const std::string& id) : m_Id{id} {}
81 BackendId(const char* id) : m_Id{id} {}
David Becke6488712018-10-16 11:32:20 +010082
83 /// Deprecated function that will be removed together with
84 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010085 BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {}
86
87 operator std::string() const { return m_Id; }
88
89 BackendId& operator=(const std::string& other)
90 {
91 m_Id = other;
92 return *this;
93 }
94
David Becke6488712018-10-16 11:32:20 +010095 /// Deprecated function that will be removed together with
96 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010097 BackendId& operator=(Compute compute)
98 {
99 BackendId temp{compute};
100 std::swap(temp.m_Id, m_Id);
101 return *this;
102 }
103
104 bool operator==(const BackendId& other) const
105 {
106 return m_Id == other.m_Id;
107 }
108
David Beck33f0ae02018-10-18 15:13:56 +0100109 // comparison against objects from which the
110 // BackendId can be constructed
111 template <typename O>
112 bool operator==(const O& other) const
113 {
114 BackendId temp{other};
115 return *this == temp;
116 }
117
118 template <typename O>
119 bool operator!=(const O& other) const
120 {
121 return !(*this == other);
122 }
123
David Beck9df2d952018-10-10 15:11:44 +0100124 bool operator<(const BackendId& other) const
125 {
126 return m_Id < other.m_Id;
127 }
128
129 const std::string& Get() const { return m_Id; }
130
131private:
David Beck9df2d952018-10-10 15:11:44 +0100132 std::string m_Id;
133};
134
David Beckf0b48452018-10-19 15:20:56 +0100135inline std::ostream& operator<<(std::ostream& os, const BackendId& id)
136{
137 os << id.Get();
138 return os;
139}
140
David Beck9df2d952018-10-10 15:11:44 +0100141template <template <class...> class TContainer>
142inline std::ostream& operator<<(std::ostream& os,
143 const TContainer<BackendId>& ids)
144{
145 os << '[';
David Beckf0b48452018-10-19 15:20:56 +0100146 for (const auto& id : ids) { os << id << " "; }
David Beck9df2d952018-10-10 15:11:44 +0100147 os << ']';
148 return os;
149}
150
151using BackendIdSet = std::unordered_set<BackendId>;
152
153} // namespace armnn
154
155namespace std
156{
157
158// make BackendId compatible with std hashtables by reusing the hash
159// function for strings
160template <>
161struct hash<armnn::BackendId>
162{
163 std::size_t operator()(const armnn::BackendId& id) const
164 {
165 std::hash<std::string> hasher;
166 return hasher(id.Get());
167 }
168};
169
170} // namespace std