Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/backends/CL/CLDeviceBackend.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 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" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 36 | |
| 37 | #include "arm_compute/core/TensorInfo.h" |
| 38 | #include "arm_compute/runtime/BlobLifetimeManager.h" |
| 39 | #include "arm_compute/runtime/CL/CLBufferAllocator.h" |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 40 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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 | |
| 47 | namespace arm_compute |
| 48 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 49 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 50 | { |
| 51 | namespace backends |
| 52 | { |
| 53 | namespace |
| 54 | { |
| 55 | bool file_exists(const std::string &filename) |
| 56 | { |
| 57 | std::ifstream file(filename); |
| 58 | return file.good(); |
| 59 | } |
| 60 | } // namespace |
| 61 | |
| 62 | /** Register CL backend */ |
| 63 | static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL); |
| 64 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 65 | CLDeviceBackend::CLDeviceBackend() |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 66 | : _context_count(0), _tuner(), _allocator(nullptr), _tuner_file() |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
| 70 | CLDeviceBackend::~CLDeviceBackend() |
| 71 | { |
| 72 | // TODO (geopin01) : Shouldn't call non exception safe stuff here |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 73 | if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty()) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 74 | { |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 75 | _tuner.save_to_file(_tuner_file); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | void CLDeviceBackend::set_kernel_tuning(bool enable_tuning) |
| 80 | { |
| 81 | _tuner.set_tune_new_kernels(enable_tuning); |
| 82 | } |
| 83 | |
| 84 | void CLDeviceBackend::initialize_backend() |
| 85 | { |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 86 | // Setup Scheduler |
| 87 | CLScheduler::get().default_init(&_tuner); |
| 88 | |
| 89 | // Create allocator with new context |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 90 | _allocator = support::cpp14::make_unique<CLBufferAllocator>(); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 93 | void CLDeviceBackend::release_backend_context(GraphContext &ctx) |
| 94 | { |
| 95 | ARM_COMPUTE_UNUSED(ctx); |
| 96 | _context_count--; |
| 97 | if(_context_count == 0) // No more context using the backend: free resources |
| 98 | { |
| 99 | _allocator = nullptr; |
| 100 | } |
| 101 | } |
| 102 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 103 | void CLDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 104 | { |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 105 | // Force backend initialization |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 106 | _context_count++; |
| 107 | if(_context_count == 1) |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 108 | { |
| 109 | initialize_backend(); |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 112 | // Setup tuner |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 113 | _tuner_file = ctx.config().tuner_file; |
| 114 | // Load tuner data if available |
| 115 | if(file_exists(_tuner_file)) |
| 116 | { |
| 117 | _tuner.load_from_file(_tuner_file); |
| 118 | } |
| 119 | |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 120 | set_kernel_tuning(ctx.config().use_tuner); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 121 | |
| 122 | // Setup a management backend |
| 123 | if(ctx.memory_management_ctx(Target::CL) == nullptr) |
| 124 | { |
| 125 | MemoryManagerContext mm_ctx; |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 126 | mm_ctx.target = Target::CL; |
| 127 | mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 128 | mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 129 | mm_ctx.cross_group = std::make_shared<CLMemoryGroup>(mm_ctx.cross_mm); |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 130 | mm_ctx.allocator = _allocator.get(); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 131 | |
| 132 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 133 | } |
| 134 | } |
| 135 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 136 | bool CLDeviceBackend::is_backend_supported() |
| 137 | { |
| 138 | return arm_compute::opencl_is_available(); |
| 139 | } |
| 140 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 141 | IAllocator *CLDeviceBackend::backend_allocator() |
| 142 | { |
Georgios Pinitas | df473ea | 2018-05-31 18:53:52 +0100 | [diff] [blame] | 143 | return _allocator.get(); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 146 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor) |
| 147 | { |
| 148 | // Get tensor descriptor |
| 149 | const TensorDescriptor &tensor_desc = tensor.desc(); |
| 150 | ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL); |
| 151 | |
| 152 | // Create backend tensor handle |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 153 | TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info); |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 154 | info.set_data_layout(tensor_desc.layout); |
| 155 | auto backend_tensor_handle = support::cpp14::make_unique<CLTensorHandle>(info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 156 | |
| 157 | return std::move(backend_tensor_handle); |
| 158 | } |
| 159 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 160 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 161 | { |
| 162 | if(parent == nullptr) |
| 163 | { |
| 164 | return nullptr; |
| 165 | } |
| 166 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 167 | return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 171 | { |
| 172 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl); |
| 173 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
| 174 | |
| 175 | // Configure node |
| 176 | return CLFunctionFactory::create(&node, ctx); |
| 177 | } |
| 178 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 179 | arm_compute::Status CLDeviceBackend::validate_node(INode &node) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 180 | { |
| 181 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl); |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 182 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 183 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 184 | return CLNodeValidator::validate(&node); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity) |
| 188 | { |
| 189 | if(affinity == MemoryManagerAffinity::Offset) |
| 190 | { |
| 191 | ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!"); |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
| 195 | auto lifetime_mgr = std::make_shared<BlobLifetimeManager>(); |
| 196 | auto pool_mgr = std::make_shared<PoolManager>(); |
| 197 | auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr); |
| 198 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 199 | return mm; |
| 200 | } |
| 201 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 202 | } // namespace graph |
Anthony Barbier | 7b607dc | 2018-07-13 15:55:24 +0100 | [diff] [blame] | 203 | } // namespace arm_compute |