blob: 770cca5d42bbafbebda66bb205081a673256df05 [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/GLES/GCDeviceBackend.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/GLES/GCFunctionFactory.h"
33#include "arm_compute/graph/backends/GLES/GCNodeValidator.h"
34#include "arm_compute/graph/backends/GLES/GCTensorHandle.h"
35
36#include "arm_compute/core/TensorInfo.h"
37#include "arm_compute/runtime/BlobLifetimeManager.h"
38#include "arm_compute/runtime/GLES_COMPUTE/GCBufferAllocator.h"
39#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
40#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{
48namespace graph
49{
50namespace backends
51{
52/** Register GLES backend */
53static detail::BackendRegistrar<GCDeviceBackend> GCDeviceBackend_registrar(Target::GC);
54
55GCDeviceBackend::GCDeviceBackend()
56 : _allocator()
57{
58}
59
60void GCDeviceBackend::initialize_backend()
61{
62 // Setup Scheduler
63 GCScheduler::get().default_init();
64}
65
66void GCDeviceBackend::setup_backend_context(GraphContext &ctx)
67{
68 // Setup a management backend
69 if(ctx.memory_management_ctx(Target::GC) == nullptr)
70 {
71 MemoryManagerContext mm_ctx;
72 mm_ctx.target = Target::GC;
73 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
74 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer);
75 mm_ctx.cross_group = std::make_shared<GCMemoryGroup>(mm_ctx.cross_mm);
76
77 ctx.insert_memory_management_ctx(std::move(mm_ctx));
78 }
79}
80
81bool GCDeviceBackend::is_backend_supported()
82{
83 return arm_compute::opengles31_is_available();
84}
85
86IAllocator *GCDeviceBackend::backend_allocator()
87{
88 return &_allocator;
89}
90
91std::unique_ptr<ITensorHandle> GCDeviceBackend::create_tensor(const Tensor &tensor)
92{
93 // Get tensor descriptor
94 const TensorDescriptor &tensor_desc = tensor.desc();
95 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::GC);
96
97 // Create backend tensor handle
98 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
99 info.set_data_layout(tensor_desc.layout);
100 auto backend_tensor_handle = support::cpp14::make_unique<GCTensorHandle>(info);
101
102 return std::move(backend_tensor_handle);
103}
104
105std::unique_ptr<ITensorHandle> GCDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
106{
107 ARM_COMPUTE_UNUSED(parent, shape, coords, extend_parent);
108 ARM_COMPUTE_ERROR("GLES backend has no sub-tensor support!");
109 return nullptr;
110}
111
112std::unique_ptr<arm_compute::IFunction> GCDeviceBackend::configure_node(INode &node, GraphContext &ctx)
113{
114 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring GC node with ID : " << node.id() << std::endl);
115 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::GC);
116
117 // Configure node
118 return GCFunctionFactory::create(&node, ctx);
119}
120
121arm_compute::Status GCDeviceBackend::validate_node(INode &node)
122{
123 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating GC node with ID : " << node.id() << std::endl);
124 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::GC);
125
126 return GCNodeValidator::validate(&node);
127}
128
129std::shared_ptr<arm_compute::IMemoryManager> GCDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
130{
131 if(affinity == MemoryManagerAffinity::Offset)
132 {
133 ARM_COMPUTE_LOG_GRAPH_WARNING("GC Backend does not support offset affinity memory management!");
134 return nullptr;
135 }
136
137 auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
138 auto pool_mgr = std::make_shared<PoolManager>();
139 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
140
141 mm->set_allocator(&_allocator);
142
143 return mm;
144}
145} // namespace backends
146} // namespace graph
147} // namespace arm_compute