blob: bf17f802f287aa9e6f18a1a575e01fec9f4609ce [file] [log] [blame]
Jenkinsb3a371b2018-05-23 11:36:53 +01001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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 */
24#include "arm_compute/graph/backends/CL/CLDeviceBackend.h"
25
26#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"
36
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/CLMemoryGroup.h"
41#include "arm_compute/runtime/CL/CLScheduler.h"
42#include "arm_compute/runtime/MemoryManagerOnDemand.h"
43#include "arm_compute/runtime/PoolManager.h"
44
45#include "support/ToolchainSupport.h"
46
47namespace arm_compute
48{
49namespace graph
50{
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
65/** Tuner export file */
66static const std::string tuner_data_filename = "acl_tuner.csv";
67
68CLDeviceBackend::CLDeviceBackend()
69 : _tuner(), _allocator(cl::Context::getDefault())
70{
71}
72
73CLDeviceBackend::~CLDeviceBackend()
74{
75 if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty())
76 {
77 _tuner.save_to_file(tuner_data_filename);
78 }
79}
80
81void CLDeviceBackend::set_kernel_tuning(bool enable_tuning)
82{
83 _tuner.set_tune_new_kernels(enable_tuning);
84}
85
86void CLDeviceBackend::initialize_backend()
87{
88 // Load tuner data if available
89 if(_tuner.lws_table().empty() && file_exists(tuner_data_filename))
90 {
91 _tuner.load_from_file(tuner_data_filename);
92 }
93
94 // Setup Scheduler
95 CLScheduler::get().default_init(&_tuner);
96
97 // Create allocator with new context
98 _allocator = CLBufferAllocator();
99}
100
101void CLDeviceBackend::setup_backend_context(GraphContext &ctx)
102{
103 // Setup tuner
104 set_kernel_tuning(ctx.config().use_tuner);
105
106 // Setup a management backend
107 if(ctx.memory_management_ctx(Target::CL) == nullptr)
108 {
109 MemoryManagerContext mm_ctx;
110 mm_ctx.target = Target::CL;
111 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
112 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
113 mm_ctx.cross_group = std::make_shared<CLMemoryGroup>(mm_ctx.cross_mm);
114
115 ctx.insert_memory_management_ctx(std::move(mm_ctx));
116 }
117}
118
119bool CLDeviceBackend::is_backend_supported()
120{
121 return arm_compute::opencl_is_available();
122}
123
124IAllocator *CLDeviceBackend::backend_allocator()
125{
126 return &_allocator;
127}
128
129std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor)
130{
131 // Get tensor descriptor
132 const TensorDescriptor &tensor_desc = tensor.desc();
133 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL);
134
135 // Create backend tensor handle
136 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
137 info.set_data_layout(tensor_desc.layout);
138 auto backend_tensor_handle = support::cpp14::make_unique<CLTensorHandle>(info);
139
140 return std::move(backend_tensor_handle);
141}
142
143std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
144{
145 if(parent == nullptr)
146 {
147 return nullptr;
148 }
149
150 return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent);
151}
152
153std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx)
154{
155 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl);
156 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
157
158 // Configure node
159 return CLFunctionFactory::create(&node, ctx);
160}
161
162arm_compute::Status CLDeviceBackend::validate_node(INode &node)
163{
164 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
165 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
166
167 return CLNodeValidator::validate(&node);
168}
169
170std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
171{
172 if(affinity == MemoryManagerAffinity::Offset)
173 {
174 ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!");
175 return nullptr;
176 }
177
178 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
179 auto pool_mgr = std::make_shared<PoolManager>();
180 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
181
182 mm->set_allocator(&_allocator);
183
184 return mm;
185}
186} // namespace backends
187} // namespace graph
188} // namespace arm_compute