ArmNN  NotReleased
BackendSettings.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/BackendId.hpp>
9 #include <vector>
10 
11 namespace armnn
12 {
13 
15 {
20 
21  BackendSettings() = default;
22 
23  BackendSettings(const BackendIdVector& preferredBackends,
24  const IDeviceSpec& deviceSpec)
25  {
26  Initialize(preferredBackends, deviceSpec);
27  }
28 
30  : m_PreferredBackends(other.m_PreferredBackends)
31  , m_SupportedBackends(other.m_SupportedBackends)
32  , m_SelectedBackends(other.m_SelectedBackends)
33  , m_IgnoredBackends(other.m_IgnoredBackends)
34  {
35  }
36 
37  bool IsBackendPreferred(const BackendId& backend) const
38  {
39  return IsBackendInCollection(backend, m_PreferredBackends);
40  }
41 
42  bool IsBackendSupported(const BackendId& backend) const
43  {
44  return IsBackendInCollection(backend, m_SupportedBackends);
45  }
46 
47  bool IsBackendSelected(const BackendId& backend) const
48  {
49  return IsBackendInCollection(backend, m_SelectedBackends);
50  }
51 
52  bool IsBackendIgnored(const BackendId& backend) const
53  {
54  return IsBackendInCollection(backend, m_IgnoredBackends);
55  }
56 
57  bool IsCpuRefUsed() const
58  {
59  BackendId cpuBackendId(Compute::CpuRef);
60  return IsBackendSupported(cpuBackendId) && IsBackendPreferred(cpuBackendId);
61  }
62 
64  {
65  BackendIdVector availablePreferredBackends;
66  for (const BackendId& backend : m_PreferredBackends)
67  {
68  if (IsBackendSupported(backend) && !IsBackendIgnored(backend))
69  {
70  availablePreferredBackends.push_back(backend);
71  }
72  }
73  return availablePreferredBackends;
74  }
75 
76 private:
77  void Initialize(const BackendIdVector& preferredBackends,
78  const IDeviceSpec& deviceSpec)
79  {
80  // Copy preferred backends from input
81  m_PreferredBackends = preferredBackends;
82 
83  // Obtain list of supported backends
84  const DeviceSpec& spec = *boost::polymorphic_downcast<const DeviceSpec*>(&deviceSpec);
85  m_SupportedBackends = spec.GetSupportedBackends();
86  }
87 
88  template<typename Collection>
89  bool IsBackendInCollection(const BackendId& backend, const Collection& collection) const
90  {
91  return std::find(collection.begin(), collection.end(), backend) != collection.end();
92  }
93 };
94 
95 } //namespace armnn
BackendIdSet m_SupportedBackends
BackendSettings(const BackendSettings &other)
BackendIdSet m_SelectedBackends
bool IsBackendSupported(const BackendId &backend) const
virtual const BackendIdSet & GetSupportedBackends() const override
Definition: DeviceSpec.hpp:25
BackendSettings(const BackendIdVector &preferredBackends, const IDeviceSpec &deviceSpec)
bool IsBackendPreferred(const BackendId &backend) const
bool IsBackendIgnored(const BackendId &backend) const
bool IsBackendSelected(const BackendId &backend) const
CPU Execution: Reference C++ kernels.
std::vector< BackendId > BackendIdVector
Definition: BackendId.hpp:190
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
BackendIdVector m_PreferredBackends
BackendIdVector GetAvailablePreferredBackends() const
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:158
BackendIdSet m_IgnoredBackends