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 | |
| 7 | #include <set> |
| 8 | #include <unordered_set> |
| 9 | #include <string> |
| 10 | #include <memory> |
| 11 | |
| 12 | namespace armnn |
| 13 | { |
| 14 | |
| 15 | // |
| 16 | // The Compute enum is now deprecated and it is now |
| 17 | // replaced by BackendId |
| 18 | // |
| 19 | enum class Compute |
| 20 | { |
| 21 | /// CPU Execution: Reference C++ kernels |
| 22 | CpuRef = 0, |
| 23 | /// CPU Execution: NEON: ArmCompute |
| 24 | CpuAcc = 1, |
| 25 | /// GPU Execution: OpenCL: ArmCompute |
| 26 | GpuAcc = 2, |
| 27 | Undefined = 5 |
| 28 | }; |
| 29 | |
| 30 | constexpr char const* GetComputeDeviceAsCString(Compute compute) |
| 31 | { |
| 32 | switch (compute) |
| 33 | { |
| 34 | case armnn::Compute::CpuRef: return "CpuRef"; |
| 35 | case armnn::Compute::CpuAcc: return "CpuAcc"; |
| 36 | case armnn::Compute::GpuAcc: return "GpuAcc"; |
| 37 | default: return "Unknown"; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute) |
| 42 | { |
| 43 | for (const Compute& comp : compute) { |
| 44 | os << GetComputeDeviceAsCString(comp) << " "; |
| 45 | } |
| 46 | return os; |
| 47 | } |
| 48 | |
| 49 | inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute) |
| 50 | { |
| 51 | for (const Compute& comp : compute) { |
| 52 | os << GetComputeDeviceAsCString(comp) << " "; |
| 53 | } |
| 54 | return os; |
| 55 | } |
| 56 | |
| 57 | inline std::ostream& operator<<(std::ostream& os, const Compute& compute) |
| 58 | { |
| 59 | os << GetComputeDeviceAsCString(compute); |
| 60 | return os; |
| 61 | } |
| 62 | |
| 63 | class BackendId final |
| 64 | { |
| 65 | public: |
| 66 | BackendId(const std::string& id) : m_Id{id} {} |
| 67 | BackendId(const char* id) : m_Id{id} {} |
| 68 | BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {} |
| 69 | |
| 70 | operator std::string() const { return m_Id; } |
| 71 | |
| 72 | BackendId& operator=(const std::string& other) |
| 73 | { |
| 74 | m_Id = other; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | BackendId& operator=(Compute compute) |
| 79 | { |
| 80 | BackendId temp{compute}; |
| 81 | std::swap(temp.m_Id, m_Id); |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | bool operator==(const BackendId& other) const |
| 86 | { |
| 87 | return m_Id == other.m_Id; |
| 88 | } |
| 89 | |
| 90 | bool operator<(const BackendId& other) const |
| 91 | { |
| 92 | return m_Id < other.m_Id; |
| 93 | } |
| 94 | |
| 95 | const std::string& Get() const { return m_Id; } |
| 96 | |
| 97 | private: |
| 98 | // backend Id mustn't be empty: |
| 99 | BackendId() = delete; |
| 100 | std::string m_Id; |
| 101 | }; |
| 102 | |
| 103 | template <template <class...> class TContainer> |
| 104 | inline std::ostream& operator<<(std::ostream& os, |
| 105 | const TContainer<BackendId>& ids) |
| 106 | { |
| 107 | os << '['; |
| 108 | for (const auto& id : ids) { os << id.Get() << " "; } |
| 109 | os << ']'; |
| 110 | return os; |
| 111 | } |
| 112 | |
| 113 | using BackendIdSet = std::unordered_set<BackendId>; |
| 114 | |
| 115 | } // namespace armnn |
| 116 | |
| 117 | namespace std |
| 118 | { |
| 119 | |
| 120 | // make BackendId compatible with std hashtables by reusing the hash |
| 121 | // function for strings |
| 122 | template <> |
| 123 | struct hash<armnn::BackendId> |
| 124 | { |
| 125 | std::size_t operator()(const armnn::BackendId& id) const |
| 126 | { |
| 127 | std::hash<std::string> hasher; |
| 128 | return hasher(id.Get()); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | } // namespace std |