aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/BackendId.hpp
blob: af3b7995eb58b9851af8ec6f7e1bf103487ae606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once

#include <ostream>
#include <set>
#include <unordered_set>
#include <string>
#include <memory>
#include <vector>

namespace armnn
{

//
// The Compute enum is now deprecated and it is now
// being replaced by BackendId
//
enum class Compute
{
    /// CPU Execution: Reference C++ kernels
    CpuRef      = 0,
    /// CPU Execution: NEON: ArmCompute
    CpuAcc      = 1,
    /// GPU Execution: OpenCL: ArmCompute
    GpuAcc      = 2,
    Undefined   = 5
};

/// Deprecated function that will be removed together with
/// the Compute enum
constexpr char const* GetComputeDeviceAsCString(Compute compute)
{
    switch (compute)
    {
        case armnn::Compute::CpuRef: return "CpuRef";
        case armnn::Compute::CpuAcc: return "CpuAcc";
        case armnn::Compute::GpuAcc: return "GpuAcc";
        default:                     return "Unknown";
    }
}

/// Deprecated function that will be removed together with
/// the Compute enum
inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute)
{
    for (const Compute& comp : compute) {
        os << GetComputeDeviceAsCString(comp) << " ";
    }
    return os;
}

/// Deprecated function that will be removed together with
/// the Compute enum
inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute)
{
    for (const Compute& comp : compute) {
        os << GetComputeDeviceAsCString(comp) << " ";
    }
    return os;
}

/// Deprecated function that will be removed together with
/// the Compute enum
inline std::ostream& operator<<(std::ostream& os, const Compute& compute)
{
    os << GetComputeDeviceAsCString(compute);
    return os;
}

class BackendId final
{
public:
    BackendId(const std::string& id) : m_Id{id} {}
    BackendId(const char* id) : m_Id{id} {}

    /// Deprecated function that will be removed together with
    /// the Compute enum
    BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {}

    operator std::string() const { return m_Id; }

    BackendId& operator=(const std::string& other)
    {
        m_Id = other;
        return *this;
    }

    /// Deprecated function that will be removed together with
    /// the Compute enum
    BackendId& operator=(Compute compute)
    {
        BackendId temp{compute};
        std::swap(temp.m_Id, m_Id);
        return *this;
    }

    bool operator==(const BackendId& other) const
    {
        return m_Id == other.m_Id;
    }

    bool operator<(const BackendId& other) const
    {
        return m_Id < other.m_Id;
    }

    const std::string& Get() const { return m_Id; }

private:
    // backend Id mustn't be empty:
    BackendId() = delete;
    std::string m_Id;
};

template <template <class...> class TContainer>
inline std::ostream& operator<<(std::ostream& os,
                                const TContainer<BackendId>& ids)
{
    os << '[';
    for (const auto& id : ids) { os << id.Get() << " "; }
    os << ']';
    return os;
}

using BackendIdSet = std::unordered_set<BackendId>;

} // namespace armnn

namespace std
{

// make BackendId compatible with std hashtables by reusing the hash
// function for strings
template <>
struct hash<armnn::BackendId>
{
    std::size_t operator()(const armnn::BackendId& id) const
    {
        std::hash<std::string> hasher;
        return hasher(id.Get());
    }
};

} // namespace std