ArmNN
 22.11
ArmnnDevice.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #define LOG_TAG "arm-armnn-sl"
7 
8 #include "ArmnnDevice.hpp"
9 
10 #include <LegacyUtils.h>
11 #include <OperationsUtils.h>
12 
13 #include <log/log.h>
14 
15 #include <memory>
16 #include <string>
17 
18 #ifdef __ANDROID__
19 #include <android/log.h>
20 #endif
21 
22 namespace
23 {
24 
25 std::string GetBackendString(const armnn_driver::DriverOptions& options)
26 {
27  std::stringstream backends;
28  for (auto&& b : options.GetBackends())
29  {
30  backends << b << " ";
31  }
32  return backends.str();
33 }
34 
35 } // anonymous namespace
36 
37 namespace armnn_driver
38 {
39 
40 using namespace android::nn;
41 
43  : m_Runtime(nullptr, nullptr)
44  , m_ClTunedParameters(nullptr)
45  , m_Options(std::move(options))
46 {
47  // First check if the DriverOptions is happy.
48  if (options.ShouldExit())
49  {
50  // Is this a good or bad exit?
51  if (options.GetExitCode() != EXIT_SUCCESS)
52  {
53  throw armnn::InvalidArgumentException("ArmnnDevice: Insufficient or illegal options specified.");
54  }
55  else
56  {
57  throw armnn::InvalidArgumentException("ArmnnDevice: Nothing to do.");
58  }
59  }
60 
61  initVLogMask();
62  VLOG(DRIVER) << "ArmnnDevice::ArmnnDevice()";
63 
64 #ifdef __ANDROID__
65  __android_log_print(ANDROID_LOG_DEBUG, "ARMNN_SL", "ArmnnDevice::ArmnnDevice()");
66 #endif
67 
70  {
71  SetMinimumLogSeverity(android::base::VERBOSE);
72  }
73  else
74  {
75  SetMinimumLogSeverity(android::base::INFO);
76  }
77  armnn::IRuntime::CreationOptions runtimeOptions;
78 
79  if (std::find(m_Options.GetBackends().begin(),
80  m_Options.GetBackends().end(),
82  {
83  try
84  {
85  if (!m_Options.GetClTunedParametersFile().empty())
86  {
89  try
90  {
92  }
93  catch (std::exception& error)
94  {
95  // This is only a warning because the file won't exist the first time you are generating it.
96  VLOG(DRIVER) << "ArmnnDevice: Failed to load CL tuned parameters file "
97  << m_Options.GetClTunedParametersFile().c_str() << " : " << error.what();
98  }
100  }
101  }
102  catch (const armnn::ClRuntimeUnavailableException& error)
103  {
104  VLOG(DRIVER) << "ArmnnDevice: Failed to setup CL runtime: %s. Device will be unavailable." << error.what();
105  }
106  catch (std::exception& error)
107  {
108  VLOG(DRIVER) << "ArmnnDevice: Unknown exception: %s. Device will be unavailable." << error.what();
109  }
110  }
112  m_Runtime = armnn::IRuntime::Create(runtimeOptions);
113 
114  std::vector<armnn::BackendId> backends;
115 
116  if (m_Runtime)
117  {
118  const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends();
119  for (auto &backend : m_Options.GetBackends())
120  {
121  if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend())
122  {
123  VLOG(DRIVER) << "ArmnnDevice: Requested unknown backend " << backend.Get().c_str();
124  }
125  else
126  {
127  backends.push_back(backend);
128  }
129  }
130  }
131 
132  if (backends.empty())
133  {
134  // No known backend specified
135  throw armnn::InvalidArgumentException("ArmnnDevice: No known backend specified.");
136  }
137 
138  m_Options.SetBackends(backends);
139  VLOG(DRIVER) << "ArmnnDevice: Created device with the following backends: " << GetBackendString(m_Options).c_str();
140 
141 #ifdef __ANDROID__
142  __android_log_print(ANDROID_LOG_DEBUG,
143  "ARMNN_SL",
144  "ArmnnDevice: Created device with the following backends: %s",
145  GetBackendString(m_Options).c_str());
146 #endif
147 }
148 
149 } // namespace armnn_driver
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:49
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
Definition: Utils.cpp:18
static IGpuAccTunedParametersPtr Create(Mode mode, TuningLevel tunerMode)
armnn::IGpuAccTunedParametersPtr m_ClTunedParameters
Definition: ArmnnDevice.hpp:24
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
bool IsVerboseLoggingEnabled() const
armnn::IRuntimePtr m_Runtime
Definition: ArmnnDevice.hpp:23
std::shared_ptr< IGpuAccTunedParameters > m_GpuAccTunedParameters
If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads...
Definition: IRuntime.hpp:98
armnn::IGpuAccTunedParameters::Mode GetClTunedParametersMode() const
ArmnnDevice(DriverOptions options)
Definition: ArmnnDevice.cpp:42
GPU Execution: OpenCL: ArmCompute.
bool m_EnableGpuProfiling
Setting this flag will allow the user to obtain GPU profiling information from the runtime...
Definition: IRuntime.hpp:101
armnn::IGpuAccTunedParameters::TuningLevel GetClTuningLevel() const
void SetBackends(const std::vector< armnn::BackendId > &backends)
const std::vector< armnn::BackendId > & GetBackends() const
const std::string & GetClTunedParametersFile() const
Helper classes.
Definition: ArmnnDevice.cpp:37