Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [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 | */ |
| 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 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | namespace graph |
| 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 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 65 | CLDeviceBackend::CLDeviceBackend() |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 66 | : _context_count(0), _tuner(), _allocator(nullptr), _tuner_file() |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
| 70 | CLDeviceBackend::~CLDeviceBackend() |
| 71 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 72 | if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty()) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 73 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 74 | _tuner.save_to_file(_tuner_file); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | void CLDeviceBackend::set_kernel_tuning(bool enable_tuning) |
| 79 | { |
| 80 | _tuner.set_tune_new_kernels(enable_tuning); |
| 81 | } |
| 82 | |
| 83 | void CLDeviceBackend::initialize_backend() |
| 84 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 85 | // Setup Scheduler |
| 86 | CLScheduler::get().default_init(&_tuner); |
| 87 | |
| 88 | // Create allocator with new context |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 89 | _allocator = support::cpp14::make_unique<CLBufferAllocator>(); |
| 90 | } |
| 91 | |
| 92 | void CLDeviceBackend::release_backend_context(GraphContext &ctx) |
| 93 | { |
| 94 | ARM_COMPUTE_UNUSED(ctx); |
| 95 | _context_count--; |
| 96 | if(_context_count == 0) // No more context using the backend: free resources |
| 97 | { |
| 98 | _allocator = nullptr; |
| 99 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void CLDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 103 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 104 | // Force backend initialization |
| 105 | _context_count++; |
| 106 | if(_context_count == 1) |
| 107 | { |
| 108 | initialize_backend(); |
| 109 | } |
| 110 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 111 | // Setup tuner |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 112 | _tuner_file = ctx.config().tuner_file; |
| 113 | // Load tuner data if available |
| 114 | if(file_exists(_tuner_file)) |
| 115 | { |
| 116 | _tuner.load_from_file(_tuner_file); |
| 117 | } |
| 118 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 119 | set_kernel_tuning(ctx.config().use_tuner); |
| 120 | |
| 121 | // Setup a management backend |
| 122 | if(ctx.memory_management_ctx(Target::CL) == nullptr) |
| 123 | { |
| 124 | MemoryManagerContext mm_ctx; |
| 125 | mm_ctx.target = Target::CL; |
| 126 | mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 127 | mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 128 | mm_ctx.cross_group = std::make_shared<CLMemoryGroup>(mm_ctx.cross_mm); |
| 129 | |
| 130 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | bool CLDeviceBackend::is_backend_supported() |
| 135 | { |
| 136 | return arm_compute::opencl_is_available(); |
| 137 | } |
| 138 | |
| 139 | IAllocator *CLDeviceBackend::backend_allocator() |
| 140 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 141 | return _allocator.get(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor) |
| 145 | { |
| 146 | // Get tensor descriptor |
| 147 | const TensorDescriptor &tensor_desc = tensor.desc(); |
| 148 | ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL); |
| 149 | |
| 150 | // Create backend tensor handle |
| 151 | TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info); |
| 152 | info.set_data_layout(tensor_desc.layout); |
| 153 | auto backend_tensor_handle = support::cpp14::make_unique<CLTensorHandle>(info); |
| 154 | |
| 155 | return std::move(backend_tensor_handle); |
| 156 | } |
| 157 | |
| 158 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) |
| 159 | { |
| 160 | if(parent == nullptr) |
| 161 | { |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent); |
| 166 | } |
| 167 | |
| 168 | std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 169 | { |
| 170 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl); |
| 171 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
| 172 | |
| 173 | // Configure node |
| 174 | return CLFunctionFactory::create(&node, ctx); |
| 175 | } |
| 176 | |
| 177 | arm_compute::Status CLDeviceBackend::validate_node(INode &node) |
| 178 | { |
| 179 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl); |
| 180 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
| 181 | |
| 182 | return CLNodeValidator::validate(&node); |
| 183 | } |
| 184 | |
| 185 | std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity) |
| 186 | { |
| 187 | if(affinity == MemoryManagerAffinity::Offset) |
| 188 | { |
| 189 | ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!"); |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | auto lifetime_mgr = std::make_shared<BlobLifetimeManager>(); |
| 194 | auto pool_mgr = std::make_shared<PoolManager>(); |
| 195 | auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr); |
| 196 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame^] | 197 | mm->set_allocator(_allocator.get()); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 198 | |
| 199 | return mm; |
| 200 | } |
| 201 | } // namespace backends |
| 202 | } // namespace graph |
| 203 | } // namespace arm_compute |