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 | { |
| 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 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 | { |
| 49 | for (const Compute& comp : compute) { |
| 50 | os << GetComputeDeviceAsCString(comp) << " "; |
| 51 | } |
| 52 | return os; |
| 53 | } |
| 54 | |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 55 | /// Deprecated function that will be removed together with |
| 56 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 57 | inline 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 Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 65 | /// Deprecated function that will be removed together with |
| 66 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 67 | inline std::ostream& operator<<(std::ostream& os, const Compute& compute) |
| 68 | { |
| 69 | os << GetComputeDeviceAsCString(compute); |
| 70 | return os; |
| 71 | } |
| 72 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 73 | struct UninitializedBackendId {}; |
| 74 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 75 | class BackendId final |
| 76 | { |
| 77 | public: |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 78 | BackendId() { GetComputeDeviceAsCString(Compute::Undefined); } |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 79 | BackendId(UninitializedBackendId) { GetComputeDeviceAsCString(Compute::Undefined); } |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 80 | BackendId(const std::string& id) : m_Id{id} {} |
| 81 | BackendId(const char* id) : m_Id{id} {} |
David Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 82 | |
| 83 | /// Deprecated function that will be removed together with |
| 84 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 85 | 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 Beck | e648871 | 2018-10-16 11:32:20 +0100 | [diff] [blame] | 95 | /// Deprecated function that will be removed together with |
| 96 | /// the Compute enum |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 97 | 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 Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 109 | // 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 Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 124 | 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 | |
| 131 | private: |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 132 | std::string m_Id; |
| 133 | }; |
| 134 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 135 | inline std::ostream& operator<<(std::ostream& os, const BackendId& id) |
| 136 | { |
| 137 | os << id.Get(); |
| 138 | return os; |
| 139 | } |
| 140 | |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 141 | template <template <class...> class TContainer> |
| 142 | inline std::ostream& operator<<(std::ostream& os, |
| 143 | const TContainer<BackendId>& ids) |
| 144 | { |
| 145 | os << '['; |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 146 | for (const auto& id : ids) { os << id << " "; } |
David Beck | 9df2d95 | 2018-10-10 15:11:44 +0100 | [diff] [blame] | 147 | os << ']'; |
| 148 | return os; |
| 149 | } |
| 150 | |
| 151 | using BackendIdSet = std::unordered_set<BackendId>; |
| 152 | |
| 153 | } // namespace armnn |
| 154 | |
| 155 | namespace std |
| 156 | { |
| 157 | |
| 158 | // make BackendId compatible with std hashtables by reusing the hash |
| 159 | // function for strings |
| 160 | template <> |
| 161 | struct 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 |