blob: b2d58e35be9167cccb7e195a3e0fcbdc95e1cb16 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/CL/CLDeviceBackend.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/GraphContext.h"
28#include "arm_compute/graph/INode.h"
29#include "arm_compute/graph/Logger.h"
30#include "arm_compute/graph/Tensor.h"
31#include "arm_compute/graph/backends/BackendRegistrar.h"
32#include "arm_compute/graph/backends/CL/CLFunctionFactory.h"
33#include "arm_compute/graph/backends/CL/CLNodeValidator.h"
34#include "arm_compute/graph/backends/CL/CLSubTensorHandle.h"
35#include "arm_compute/graph/backends/CL/CLTensorHandle.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000036
Pablo Tellodb8485a2019-09-24 11:03:47 +010037#include "arm_compute/core/CL/CLCoreRuntimeContext.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000038#include "arm_compute/core/TensorInfo.h"
39#include "arm_compute/runtime/BlobLifetimeManager.h"
40#include "arm_compute/runtime/CL/CLBufferAllocator.h"
41#include "arm_compute/runtime/CL/CLScheduler.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010042#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010043#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000044#include "arm_compute/runtime/MemoryManagerOnDemand.h"
45#include "arm_compute/runtime/PoolManager.h"
46
47#include "support/ToolchainSupport.h"
48
49namespace arm_compute
50{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010051namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000052{
53namespace backends
54{
55namespace
56{
57bool file_exists(const std::string &filename)
58{
59 std::ifstream file(filename);
60 return file.good();
61}
62} // namespace
63
64/** Register CL backend */
65static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL);
66
Georgios Pinitasd8734b52017-12-22 15:27:52 +000067CLDeviceBackend::CLDeviceBackend()
Pablo Telloee8cf322019-11-13 10:44:09 +000068 : _context_count(0), _tuner(), _allocator(nullptr), _tuner_file()
Georgios Pinitasd8734b52017-12-22 15:27:52 +000069{
70}
71
72CLDeviceBackend::~CLDeviceBackend()
73{
74 // TODO (geopin01) : Shouldn't call non exception safe stuff here
Anthony Barbier7b607dc2018-07-13 15:55:24 +010075 if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty())
Georgios Pinitasd8734b52017-12-22 15:27:52 +000076 {
Anthony Barbier7b607dc2018-07-13 15:55:24 +010077 _tuner.save_to_file(_tuner_file);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000078 }
79}
80
81void CLDeviceBackend::set_kernel_tuning(bool enable_tuning)
82{
83 _tuner.set_tune_new_kernels(enable_tuning);
84}
85
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010086void CLDeviceBackend::set_kernel_tuning_mode(CLTunerMode tuning_mode)
87{
88 _tuner.set_tuner_mode(tuning_mode);
89}
90
Georgios Pinitasd8734b52017-12-22 15:27:52 +000091void CLDeviceBackend::initialize_backend()
92{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000093 // Setup Scheduler
94 CLScheduler::get().default_init(&_tuner);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000095 // Create allocator with new context
Pablo Telloee8cf322019-11-13 10:44:09 +000096 _allocator = support::cpp14::make_unique<CLBufferAllocator>(nullptr /* legacy path for CLCoreRuntimeContext */);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000097}
98
Anthony Barbierb6eb3532018-08-08 13:20:04 +010099void CLDeviceBackend::release_backend_context(GraphContext &ctx)
100{
101 ARM_COMPUTE_UNUSED(ctx);
102 _context_count--;
103 if(_context_count == 0) // No more context using the backend: free resources
104 {
105 _allocator = nullptr;
106 }
107}
108
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000109void CLDeviceBackend::setup_backend_context(GraphContext &ctx)
110{
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100111 // Force backend initialization
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100112 _context_count++;
113 if(_context_count == 1)
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100114 {
115 initialize_backend();
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100116 }
117
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000118 // Setup tuner
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100119 _tuner_file = ctx.config().tuner_file;
120 // Load tuner data if available
121 if(file_exists(_tuner_file))
122 {
123 _tuner.load_from_file(_tuner_file);
124 }
125
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000126 set_kernel_tuning(ctx.config().use_tuner);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100127 set_kernel_tuning_mode(ctx.config().tuner_mode);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000128
129 // Setup a management backend
130 if(ctx.memory_management_ctx(Target::CL) == nullptr)
131 {
132 MemoryManagerContext mm_ctx;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100133 mm_ctx.target = Target::CL;
134 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
135 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100136 mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100137 mm_ctx.allocator = _allocator.get();
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000138
139 ctx.insert_memory_management_ctx(std::move(mm_ctx));
140 }
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100141
142 // Create function level weights manager
143 if(ctx.weights_management_ctx(Target::CL) == nullptr)
144 {
145 WeightsManagerContext wm_ctx;
146 wm_ctx.target = Target::CL;
147 wm_ctx.wm = create_weights_manager();
148
149 ctx.insert_weights_management_ctx(std::move(wm_ctx));
150 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000151}
152
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100153bool CLDeviceBackend::is_backend_supported()
154{
155 return arm_compute::opencl_is_available();
156}
157
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100158IAllocator *CLDeviceBackend::backend_allocator()
159{
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100160 return _allocator.get();
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100161}
162
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000163std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor)
164{
165 // Get tensor descriptor
166 const TensorDescriptor &tensor_desc = tensor.desc();
167 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL);
168
169 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100170 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100171 info.set_data_layout(tensor_desc.layout);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000172
Georgios Pinitas0b192e82020-02-20 17:09:28 +0000173 return support::cpp14::make_unique<CLTensorHandle>(info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000174}
175
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000176std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000177{
178 if(parent == nullptr)
179 {
180 return nullptr;
181 }
182
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000183 return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000184}
185
186std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx)
187{
188 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl);
189 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
190
191 // Configure node
192 return CLFunctionFactory::create(&node, ctx);
193}
194
Georgios Pinitas28705162018-03-21 20:10:53 +0000195arm_compute::Status CLDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000196{
197 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000198 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000199
Georgios Pinitas28705162018-03-21 20:10:53 +0000200 return CLNodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000201}
202
203std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
204{
205 if(affinity == MemoryManagerAffinity::Offset)
206 {
207 ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!");
208 return nullptr;
209 }
210
211 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
212 auto pool_mgr = std::make_shared<PoolManager>();
213 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
214
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000215 return mm;
216}
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100217
218std::shared_ptr<arm_compute::IWeightsManager> CLDeviceBackend::create_weights_manager()
219{
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100220 auto weights_mgr = std::make_shared<IWeightsManager>();
221 return weights_mgr;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100222}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000223} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100224} // namespace graph
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100225} // namespace arm_compute