• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
11 #include <armnn/utility/PolymorphicDowncast.hpp>
12 
13 #include <vector>
14 
15 namespace armnn
16 {
17 
18 struct BackendSettings
19 {
20     BackendIdVector m_PreferredBackends;
21     BackendIdSet    m_SupportedBackends;
22     BackendIdSet    m_SelectedBackends;
23     BackendIdSet    m_IgnoredBackends;
24 
25     BackendSettings() = default;
26 
BackendSettingsarmnn::BackendSettings27     BackendSettings(const BackendIdVector& preferredBackends,
28                     const IDeviceSpec& deviceSpec)
29     {
30         Initialize(preferredBackends, deviceSpec);
31     }
32 
BackendSettingsarmnn::BackendSettings33     BackendSettings(const BackendSettings& other)
34         : m_PreferredBackends(other.m_PreferredBackends)
35         , m_SupportedBackends(other.m_SupportedBackends)
36         , m_SelectedBackends(other.m_SelectedBackends)
37         , m_IgnoredBackends(other.m_IgnoredBackends)
38     {
39     }
40 
IsBackendPreferredarmnn::BackendSettings41     bool IsBackendPreferred(const BackendId& backend) const
42     {
43         return IsBackendInCollection(backend, m_PreferredBackends);
44     }
45 
IsBackendSupportedarmnn::BackendSettings46     bool IsBackendSupported(const BackendId& backend) const
47     {
48         return IsBackendInCollection(backend, m_SupportedBackends);
49     }
50 
IsBackendSelectedarmnn::BackendSettings51     bool IsBackendSelected(const BackendId& backend) const
52     {
53         return IsBackendInCollection(backend, m_SelectedBackends);
54     }
55 
IsBackendIgnoredarmnn::BackendSettings56     bool IsBackendIgnored(const BackendId& backend) const
57     {
58         return IsBackendInCollection(backend, m_IgnoredBackends);
59     }
60 
IsCpuRefUsedarmnn::BackendSettings61     bool IsCpuRefUsed() const
62     {
63         BackendId cpuBackendId(Compute::CpuRef);
64         return IsBackendSupported(cpuBackendId) && IsBackendPreferred(cpuBackendId);
65     }
66 
GetAvailablePreferredBackendsarmnn::BackendSettings67     BackendIdVector GetAvailablePreferredBackends() const
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:
Initializearmnn::BackendSettings81     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);
89         m_SupportedBackends = spec.GetSupportedBackends();
90     }
91 
92     template<typename Collection>
IsBackendInCollectionarmnn::BackendSettings93     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
100