David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 7 | #include <ostream> |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 8 | #include <set> |
| 9 | #include <unordered_set> |
| 10 | #include <string> |
| 11 | #include <memory> |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 12 | #include <vector> |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | // |
| 18 | // The Compute enum is now deprecated and it is now |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 19 | // being replaced by BackendId |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 20 | // |
| 21 | enum class Compute |
| 22 | { |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 23 | Undefined = 0, |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 24 | /// CPU Execution: Reference C++ kernels |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 25 | CpuRef = 1, |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 26 | /// CPU Execution: NEON: ArmCompute |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 27 | CpuAcc = 2, |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 28 | /// GPU Execution: OpenCL: ArmCompute |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 29 | GpuAcc = 3 |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 32 | /// Deprecated function that will be removed together with |
| 33 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 34 | constexpr 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 Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 45 | /// Deprecated function that will be removed together with |
| 46 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 47 | inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute) |
| 48 | { |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 49 | for (const Compute& comp : compute) |
| 50 | { |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 51 | os << GetComputeDeviceAsCString(comp) << " "; |
| 52 | } |
| 53 | return os; |
| 54 | } |
| 55 | |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 56 | /// Deprecated function that will be removed together with |
| 57 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 58 | inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute) |
| 59 | { |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 60 | for (const Compute& comp : compute) |
| 61 | { |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 62 | os << GetComputeDeviceAsCString(comp) << " "; |
| 63 | } |
| 64 | return os; |
| 65 | } |
| 66 | |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 67 | /// Deprecated function that will be removed together with |
| 68 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 69 | inline std::ostream& operator<<(std::ostream& os, const Compute& compute) |
| 70 | { |
| 71 | os << GetComputeDeviceAsCString(compute); |
| 72 | return os; |
| 73 | } |
| 74 | |
| 75 | class BackendId final |
| 76 | { |
| 77 | public: |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 78 | BackendId() : m_Id(GetComputeDeviceAsCString(Compute::Undefined)) {} |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 79 | BackendId(const std::string& id) : m_Id{id} {} |
| 80 | BackendId(const char* id) : m_Id{id} {} |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 81 | |
| 82 | /// Deprecated function that will be removed together with |
| 83 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 84 | BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {} |
| 85 | |
| 86 | operator std::string() const { return m_Id; } |
| 87 | |
| 88 | BackendId& operator=(const std::string& other) |
| 89 | { |
| 90 | m_Id = other; |
| 91 | return *this; |
| 92 | } |
| 93 | |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 94 | /// Deprecated function that will be removed together with |
| 95 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 96 | BackendId& operator=(Compute compute) |
| 97 | { |
| 98 | BackendId temp{compute}; |
| 99 | std::swap(temp.m_Id, m_Id); |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | bool operator==(const BackendId& other) const |
| 104 | { |
| 105 | return m_Id == other.m_Id; |
| 106 | } |
| 107 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 108 | // comparison against objects from which the |
| 109 | // BackendId can be constructed |
| 110 | template <typename O> |
| 111 | bool operator==(const O& other) const |
| 112 | { |
| 113 | BackendId temp{other}; |
| 114 | return *this == temp; |
| 115 | } |
| 116 | |
| 117 | template <typename O> |
| 118 | bool operator!=(const O& other) const |
| 119 | { |
| 120 | return !(*this == other); |
| 121 | } |
| 122 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 123 | bool operator<(const BackendId& other) const |
| 124 | { |
| 125 | return m_Id < other.m_Id; |
| 126 | } |
| 127 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 128 | bool IsCpuRef() const { return m_Id == GetComputeDeviceAsCString(Compute::CpuRef); } |
| 129 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 130 | const std::string& Get() const { return m_Id; } |
| 131 | |
| 132 | private: |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 133 | std::string m_Id; |
| 134 | }; |
| 135 | |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 136 | } // namespace armnn |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 137 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 138 | namespace std |
| 139 | { |
| 140 | |
| 141 | // make BackendId compatible with std hashtables by reusing the hash |
Rob Hughes | 0214d7e | 2018-11-21 09:55:52 +0000 | [diff] [blame] | 142 | // function for strings. |
| 143 | // Note this must come *before* the first use of unordered_set<BackendId>. |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 144 | template <> |
| 145 | struct hash<armnn::BackendId> |
| 146 | { |
| 147 | std::size_t operator()(const armnn::BackendId& id) const |
| 148 | { |
| 149 | std::hash<std::string> hasher; |
| 150 | return hasher(id.Get()); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | } // namespace std |
Rob Hughes | 0214d7e | 2018-11-21 09:55:52 +0000 | [diff] [blame] | 155 | |
| 156 | namespace armnn |
| 157 | { |
| 158 | |
| 159 | inline std::ostream& operator<<(std::ostream& os, const BackendId& id) |
| 160 | { |
| 161 | os << id.Get(); |
| 162 | return os; |
| 163 | } |
| 164 | |
| 165 | template <template <typename...> class TContainer, typename... TContainerTemplateArgs> |
| 166 | std::ostream& operator<<(std::ostream& os, |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 167 | const TContainer<BackendId, TContainerTemplateArgs...>& ids) |
Rob Hughes | 0214d7e | 2018-11-21 09:55:52 +0000 | [diff] [blame] | 168 | { |
| 169 | os << '['; |
| 170 | for (const auto& id : ids) { os << id << " "; } |
| 171 | os << ']'; |
| 172 | return os; |
| 173 | } |
| 174 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 175 | using BackendIdVector = std::vector<BackendId>; |
| 176 | using BackendIdSet = std::unordered_set<BackendId>; |
Rob Hughes | 0214d7e | 2018-11-21 09:55:52 +0000 | [diff] [blame] | 177 | |
| 178 | } // namespace armnn |
| 179 | |