blob: 5f0bf3f26362ea7cc1bd339347dc3d92fe2b2a32 [file] [log] [blame]
Georgios Pinitasfbb80542018-03-27 17:15:49 +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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/GLES/GCDeviceBackend.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010025
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/GLES/GCFunctionFactory.h"
33#include "arm_compute/graph/backends/GLES/GCNodeValidator.h"
34#include "arm_compute/graph/backends/GLES/GCTensorHandle.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010035
36#include "arm_compute/core/TensorInfo.h"
37#include "arm_compute/runtime/BlobLifetimeManager.h"
38#include "arm_compute/runtime/GLES_COMPUTE/GCBufferAllocator.h"
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010039#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
Georgios Pinitasfbb80542018-03-27 17:15:49 +010040#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.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 Pinitasfbb80542018-03-27 17:15:49 +010049{
50namespace backends
51{
52/** Register GLES backend */
53static detail::BackendRegistrar<GCDeviceBackend> GCDeviceBackend_registrar(Target::GC);
54
55GCDeviceBackend::GCDeviceBackend()
Georgios Pinitasdf473ea2018-05-31 18:53:52 +010056 : _initialized(false), _allocator()
Georgios Pinitasfbb80542018-03-27 17:15:49 +010057{
58}
59
60void GCDeviceBackend::initialize_backend()
61{
62 // Setup Scheduler
63 GCScheduler::get().default_init();
64}
65
Anthony Barbierb6eb3532018-08-08 13:20:04 +010066void GCDeviceBackend::release_backend_context(GraphContext &ctx)
67{
68 //Nothing to do
69 ARM_COMPUTE_UNUSED(ctx);
70}
71
Georgios Pinitasfbb80542018-03-27 17:15:49 +010072void GCDeviceBackend::setup_backend_context(GraphContext &ctx)
73{
Georgios Pinitasdf473ea2018-05-31 18:53:52 +010074 // Force backend initialization
75 if(!_initialized)
76 {
77 initialize_backend();
78 _initialized = true;
79 }
80
Georgios Pinitasfbb80542018-03-27 17:15:49 +010081 // Setup a management backend
82 if(ctx.memory_management_ctx(Target::GC) == nullptr)
83 {
84 MemoryManagerContext mm_ctx;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010085 mm_ctx.target = Target::GC;
86 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
87 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
88 mm_ctx.cross_group = std::make_shared<GCMemoryGroup>(mm_ctx.cross_mm);
Georgios Pinitas9da19e92018-10-11 15:33:11 +010089 mm_ctx.allocator = &_allocator;
Georgios Pinitasfbb80542018-03-27 17:15:49 +010090
91 ctx.insert_memory_management_ctx(std::move(mm_ctx));
92 }
93}
94
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010095bool GCDeviceBackend::is_backend_supported()
96{
97 return arm_compute::opengles31_is_available();
98}
99
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100100IAllocator *GCDeviceBackend::backend_allocator()
101{
102 return &_allocator;
103}
104
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100105std::unique_ptr<ITensorHandle> GCDeviceBackend::create_tensor(const Tensor &tensor)
106{
107 // Get tensor descriptor
108 const TensorDescriptor &tensor_desc = tensor.desc();
109 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::GC);
110
111 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100112 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100113 info.set_data_layout(tensor_desc.layout);
114 auto backend_tensor_handle = support::cpp14::make_unique<GCTensorHandle>(info);
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100115
116 return std::move(backend_tensor_handle);
117}
118
119std::unique_ptr<ITensorHandle> GCDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
120{
121 ARM_COMPUTE_UNUSED(parent, shape, coords, extend_parent);
122 ARM_COMPUTE_ERROR("GLES backend has no sub-tensor support!");
123 return nullptr;
124}
125
126std::unique_ptr<arm_compute::IFunction> GCDeviceBackend::configure_node(INode &node, GraphContext &ctx)
127{
128 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring GC node with ID : " << node.id() << std::endl);
129 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::GC);
130
131 // Configure node
132 return GCFunctionFactory::create(&node, ctx);
133}
134
135arm_compute::Status GCDeviceBackend::validate_node(INode &node)
136{
137 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating GC node with ID : " << node.id() << std::endl);
138 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::GC);
139
140 return GCNodeValidator::validate(&node);
141}
142
143std::shared_ptr<arm_compute::IMemoryManager> GCDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
144{
145 if(affinity == MemoryManagerAffinity::Offset)
146 {
147 ARM_COMPUTE_LOG_GRAPH_WARNING("GC Backend does not support offset affinity memory management!");
148 return nullptr;
149 }
150
151 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
152 auto pool_mgr = std::make_shared<PoolManager>();
153 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
154
Georgios Pinitasfbb80542018-03-27 17:15:49 +0100155 return mm;
156}
157} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100158} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100159} // namespace arm_compute