ArmNN
 24.08
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 "DeviceSpec.hpp"
9 
10 #include <armnn/BackendId.hpp>
12 
13 #include <vector>
14 
15 namespace armnn
16 {
17 
19 {
24 
25  BackendSettings() = default;
26 
27  BackendSettings(const BackendIdVector& preferredBackends,
28  const IDeviceSpec& deviceSpec)
29  {
30  Initialize(preferredBackends, deviceSpec);
31  }
32 
38  {
39  }
40 
41  bool IsBackendPreferred(const BackendId& backend) const
42  {
43  return IsBackendInCollection(backend, m_PreferredBackends);
44  }
45 
46  bool IsBackendSupported(const BackendId& backend) const
47  {
48  return IsBackendInCollection(backend, m_SupportedBackends);
49  }
50 
51  bool IsBackendSelected(const BackendId& backend) const
52  {
53  return IsBackendInCollection(backend, m_SelectedBackends);
54  }
55 
56  bool IsBackendIgnored(const BackendId& backend) const
57  {
58  return IsBackendInCollection(backend, m_IgnoredBackends);
59  }
60 
61  bool IsCpuRefUsed() const
62  {
63  BackendId cpuBackendId(Compute::CpuRef);
64  return IsBackendSupported(cpuBackendId) && IsBackendPreferred(cpuBackendId);
65  }
66 
68  {
69  BackendIdVector availablePreferredBackends;
70  for (const BackendId& backend : m_PreferredBackends)
71  {
72  if (IsBackendSupported(backend) && !IsBackendIgnored(backend))
73  {
74  availablePreferredBackends.push_back(backend);
75  }
76  }
77  return availablePreferredBackends;
78  }
79 
80 private:
81  void Initialize(const BackendIdVector& preferredBackends,
82  const IDeviceSpec& deviceSpec)
83  {
84  // Copy preferred backends from input
85  m_PreferredBackends = preferredBackends;
86 
87  // Obtain list of supported backends
88  const DeviceSpec& spec = *PolymorphicDowncast<const DeviceSpec*>(&deviceSpec);
90  }
91 
92  template<typename Collection>
93  bool IsBackendInCollection(const BackendId& backend, const Collection& collection) const
94  {
95  return std::find(collection.begin(), collection.end(), backend) != collection.end();
96  }
97 };
98 
99 } //namespace armnn
armnn::BackendSettings
Definition: BackendSettings.hpp:18
DeviceSpec.hpp
armnn::BackendSettings::m_IgnoredBackends
BackendIdSet m_IgnoredBackends
Definition: BackendSettings.hpp:23
armnn::Compute::CpuRef
@ CpuRef
CPU Execution: Reference C++ kernels.
armnn::BackendIdSet
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
BackendId.hpp
armnn::BackendSettings::m_SelectedBackends
BackendIdSet m_SelectedBackends
Definition: BackendSettings.hpp:22
armnn::BackendSettings::IsBackendIgnored
bool IsBackendIgnored(const BackendId &backend) const
Definition: BackendSettings.hpp:56
armnn::BackendSettings::m_SupportedBackends
BackendIdSet m_SupportedBackends
Definition: BackendSettings.hpp:21
armnn::BackendSettings::BackendSettings
BackendSettings(const BackendIdVector &preferredBackends, const IDeviceSpec &deviceSpec)
Definition: BackendSettings.hpp:27
PolymorphicDowncast.hpp
armnn::BackendSettings::GetAvailablePreferredBackends
BackendIdVector GetAvailablePreferredBackends() const
Definition: BackendSettings.hpp:67
armnn::BackendSettings::BackendSettings
BackendSettings()=default
armnn::BackendSettings::BackendSettings
BackendSettings(const BackendSettings &other)
Definition: BackendSettings.hpp:33
armnn::DeviceSpec
Definition: DeviceSpec.hpp:14
armnn::BackendSettings::IsCpuRefUsed
bool IsCpuRefUsed() const
Definition: BackendSettings.hpp:61
armnn::BackendSettings::IsBackendSelected
bool IsBackendSelected(const BackendId &backend) const
Definition: BackendSettings.hpp:51
armnn::BackendId
Definition: BackendId.hpp:75
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::IDeviceSpec
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:299
armnn::BackendIdVector
std::vector< BackendId > BackendIdVector
Definition: BackendId.hpp:192
armnn::BackendSettings::IsBackendSupported
bool IsBackendSupported(const BackendId &backend) const
Definition: BackendSettings.hpp:46
armnn::BackendSettings::m_PreferredBackends
BackendIdVector m_PreferredBackends
Definition: BackendSettings.hpp:20
armnn::BackendSettings::IsBackendPreferred
bool IsBackendPreferred(const BackendId &backend) const
Definition: BackendSettings.hpp:41
armnn::DeviceSpec::GetSupportedBackends
virtual const BackendIdSet & GetSupportedBackends() const override
Definition: DeviceSpec.hpp:25