blob: 9971e4fc9a677c25f41f1a00363e7c103acee54c [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +01002 * Copyright (c) 2018-2019 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
37#include "arm_compute/core/TensorInfo.h"
38#include "arm_compute/runtime/BlobLifetimeManager.h"
39#include "arm_compute/runtime/CL/CLBufferAllocator.h"
40#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010041#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000042#include "arm_compute/runtime/MemoryManagerOnDemand.h"
43#include "arm_compute/runtime/PoolManager.h"
44
45#include "support/ToolchainSupport.h"
46
47namespace arm_compute
48{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010049namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000050{
51namespace backends
52{
53namespace
54{
55bool file_exists(const std::string &filename)
56{
57 std::ifstream file(filename);
58 return file.good();
59}
60} // namespace
61
62/** Register CL backend */
63static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL);
64
Georgios Pinitasd8734b52017-12-22 15:27:52 +000065CLDeviceBackend::CLDeviceBackend()
Anthony Barbierb6eb3532018-08-08 13:20:04 +010066 : _context_count(0), _tuner(), _allocator(nullptr), _tuner_file()
Georgios Pinitasd8734b52017-12-22 15:27:52 +000067{
68}
69
70CLDeviceBackend::~CLDeviceBackend()
71{
72 // TODO (geopin01) : Shouldn't call non exception safe stuff here
Anthony Barbier7b607dc2018-07-13 15:55:24 +010073 if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty())
Georgios Pinitasd8734b52017-12-22 15:27:52 +000074 {
Anthony Barbier7b607dc2018-07-13 15:55:24 +010075 _tuner.save_to_file(_tuner_file);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000076 }
77}
78
79void CLDeviceBackend::set_kernel_tuning(bool enable_tuning)
80{
81 _tuner.set_tune_new_kernels(enable_tuning);
82}
83
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +010084void CLDeviceBackend::set_kernel_tuning_mode(CLTunerMode tuning_mode)
85{
86 _tuner.set_tuner_mode(tuning_mode);
87}
88
Georgios Pinitasd8734b52017-12-22 15:27:52 +000089void CLDeviceBackend::initialize_backend()
90{
Georgios Pinitasd8734b52017-12-22 15:27:52 +000091 // Setup Scheduler
92 CLScheduler::get().default_init(&_tuner);
93
94 // Create allocator with new context
Georgios Pinitasdf473ea2018-05-31 18:53:52 +010095 _allocator = support::cpp14::make_unique<CLBufferAllocator>();
Georgios Pinitasd8734b52017-12-22 15:27:52 +000096}
97
Anthony Barbierb6eb3532018-08-08 13:20:04 +010098void CLDeviceBackend::release_backend_context(GraphContext &ctx)
99{
100 ARM_COMPUTE_UNUSED(ctx);
101 _context_count--;
102 if(_context_count == 0) // No more context using the backend: free resources
103 {
104 _allocator = nullptr;
105 }
106}
107
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000108void CLDeviceBackend::setup_backend_context(GraphContext &ctx)
109{
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100110 // Force backend initialization
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100111 _context_count++;
112 if(_context_count == 1)
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100113 {
114 initialize_backend();
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100115 }
116
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000117 // Setup tuner
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100118 _tuner_file = ctx.config().tuner_file;
119 // Load tuner data if available
120 if(file_exists(_tuner_file))
121 {
122 _tuner.load_from_file(_tuner_file);
123 }
124
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000125 set_kernel_tuning(ctx.config().use_tuner);
Vidhya Sudhan Loganathan050471e2019-04-25 09:27:24 +0100126 set_kernel_tuning_mode(ctx.config().tuner_mode);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127
128 // Setup a management backend
129 if(ctx.memory_management_ctx(Target::CL) == nullptr)
130 {
131 MemoryManagerContext mm_ctx;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100132 mm_ctx.target = Target::CL;
133 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
134 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100135 mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
Georgios Pinitas9da19e92018-10-11 15:33:11 +0100136 mm_ctx.allocator = _allocator.get();
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137
138 ctx.insert_memory_management_ctx(std::move(mm_ctx));
139 }
140}
141
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100142bool CLDeviceBackend::is_backend_supported()
143{
144 return arm_compute::opencl_is_available();
145}
146
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100147IAllocator *CLDeviceBackend::backend_allocator()
148{
Georgios Pinitasdf473ea2018-05-31 18:53:52 +0100149 return _allocator.get();
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100150}
151
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000152std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor)
153{
154 // Get tensor descriptor
155 const TensorDescriptor &tensor_desc = tensor.desc();
156 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL);
157
158 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100159 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100160 info.set_data_layout(tensor_desc.layout);
161 auto backend_tensor_handle = support::cpp14::make_unique<CLTensorHandle>(info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000162
163 return std::move(backend_tensor_handle);
164}
165
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000166std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000167{
168 if(parent == nullptr)
169 {
170 return nullptr;
171 }
172
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000173 return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000174}
175
176std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx)
177{
178 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl);
179 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
180
181 // Configure node
182 return CLFunctionFactory::create(&node, ctx);
183}
184
Georgios Pinitas28705162018-03-21 20:10:53 +0000185arm_compute::Status CLDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000186{
187 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000188 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000189
Georgios Pinitas28705162018-03-21 20:10:53 +0000190 return CLNodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000191}
192
193std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
194{
195 if(affinity == MemoryManagerAffinity::Offset)
196 {
197 ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!");
198 return nullptr;
199 }
200
201 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
202 auto pool_mgr = std::make_shared<PoolManager>();
203 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
204
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000205 return mm;
206}
207} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100208} // namespace graph
Anthony Barbier7b607dc2018-07-13 15:55:24 +0100209} // namespace arm_compute