blob: 37cbcd72d7945c6f2e7745a9582208dd113b68cf [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
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 */
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"
41#include "arm_compute/runtime/MemoryManagerOnDemand.h"
42#include "arm_compute/runtime/PoolManager.h"
43
44#include "support/ToolchainSupport.h"
45
46namespace arm_compute
47{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010048namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000049{
50namespace backends
51{
52namespace
53{
54bool file_exists(const std::string &filename)
55{
56 std::ifstream file(filename);
57 return file.good();
58}
59} // namespace
60
61/** Register CL backend */
62static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL);
63
64/** Tuner export file */
65static const std::string tuner_data_filename = "acl_tuner.csv";
66
67CLDeviceBackend::CLDeviceBackend()
68 : _tuner(), _allocator(cl::Context::getDefault())
69{
70}
71
72CLDeviceBackend::~CLDeviceBackend()
73{
74 // TODO (geopin01) : Shouldn't call non exception safe stuff here
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
Georgios Pinitas9a8c6722018-03-21 17:52:35 +0000104 set_kernel_tuning(ctx.config().use_tuner);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000105
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.mm = create_memory_manager(MemoryManagerAffinity::Buffer);
112
113 ctx.insert_memory_management_ctx(std::move(mm_ctx));
114 }
115}
116
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100117bool CLDeviceBackend::is_backend_supported()
118{
119 return arm_compute::opencl_is_available();
120}
121
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000122std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor)
123{
124 // Get tensor descriptor
125 const TensorDescriptor &tensor_desc = tensor.desc();
126 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL);
127
128 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100129 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100130 info.set_data_layout(tensor_desc.layout);
131 auto backend_tensor_handle = support::cpp14::make_unique<CLTensorHandle>(info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000132
133 return std::move(backend_tensor_handle);
134}
135
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000136std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137{
138 if(parent == nullptr)
139 {
140 return nullptr;
141 }
142
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000143 return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000144}
145
146std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx)
147{
148 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl);
149 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
150
151 // Configure node
152 return CLFunctionFactory::create(&node, ctx);
153}
154
Georgios Pinitas28705162018-03-21 20:10:53 +0000155arm_compute::Status CLDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000156{
157 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000158 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000159
Georgios Pinitas28705162018-03-21 20:10:53 +0000160 return CLNodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000161}
162
163std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
164{
165 if(affinity == MemoryManagerAffinity::Offset)
166 {
167 ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!");
168 return nullptr;
169 }
170
171 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
172 auto pool_mgr = std::make_shared<PoolManager>();
173 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
174
175 mm->set_allocator(&_allocator);
176
177 return mm;
178}
179} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100180} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000181} // namespace arm_compute