blob: af3b7995eb58b9851af8ec6f7e1bf103487ae606 [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
73class BackendId final
74{
75public:
76 BackendId(const std::string& id) : m_Id{id} {}
77 BackendId(const char* id) : m_Id{id} {}
David Becke6488712018-10-16 11:32:20 +010078
79 /// Deprecated function that will be removed together with
80 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010081 BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {}
82
83 operator std::string() const { return m_Id; }
84
85 BackendId& operator=(const std::string& other)
86 {
87 m_Id = other;
88 return *this;
89 }
90
David Becke6488712018-10-16 11:32:20 +010091 /// Deprecated function that will be removed together with
92 /// the Compute enum
David Beck9df2d952018-10-10 15:11:44 +010093 BackendId& operator=(Compute compute)
94 {
95 BackendId temp{compute};
96 std::swap(temp.m_Id, m_Id);
97 return *this;
98 }
99
100 bool operator==(const BackendId& other) const
101 {
102 return m_Id == other.m_Id;
103 }
104
105 bool operator<(const BackendId& other) const
106 {
107 return m_Id < other.m_Id;
108 }
109
110 const std::string& Get() const { return m_Id; }
111
112private:
113 // backend Id mustn't be empty:
114 BackendId() = delete;
115 std::string m_Id;
116};
117
118template <template <class...> class TContainer>
119inline std::ostream& operator<<(std::ostream& os,
120 const TContainer<BackendId>& ids)
121{
122 os << '[';
123 for (const auto& id : ids) { os << id.Get() << " "; }
124 os << ']';
125 return os;
126}
127
128using BackendIdSet = std::unordered_set<BackendId>;
129
130} // namespace armnn
131
132namespace std
133{
134
135// make BackendId compatible with std hashtables by reusing the hash
136// function for strings
137template <>
138struct hash<armnn::BackendId>
139{
140 std::size_t operator()(const armnn::BackendId& id) const
141 {
142 std::hash<std::string> hasher;
143 return hasher(id.Get());
144 }
145};
146
147} // namespace std