arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 7 | #include <armnn/backends/IBackendInternal.hpp> |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 8 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 9 | #include <arm_compute/core/Types.h> |
| 10 | #include <arm_compute/runtime/CL/CLBufferAllocator.h> |
| 11 | |
| 12 | #include <aclCommon/BaseMemoryManager.hpp> |
| 13 | #include <arm_compute/runtime/CL/CLMemoryRegion.h> |
| 14 | |
| 15 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 16 | #include <CL/cl_ext.h> |
| 17 | |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 18 | // System includes for mapping and unmapping memory |
| 19 | #include <sys/mman.h> |
| 20 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 21 | namespace armnn |
| 22 | { |
| 23 | |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 24 | // add new capabilities here.. |
| 25 | const BackendCapabilities gpuAccCapabilities("GpuAcc", |
| 26 | { |
| 27 | {"NonConstWeights", false}, |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 28 | {"AsyncExecution", false}, |
| 29 | {"ProtectedContentAllocation", true} |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 30 | }); |
Sadik Armagan | aede8ca | 2021-03-31 16:12:13 +0100 | [diff] [blame] | 31 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 32 | class ClBackend : public IBackendInternal |
| 33 | { |
| 34 | public: |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 35 | ClBackend() : m_CustomAllocator(nullptr) {}; |
| 36 | ClBackend(std::shared_ptr<ICustomAllocator> allocator) |
| 37 | { |
| 38 | std::string err; |
| 39 | UseCustomMemoryAllocator(allocator, err); |
| 40 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 41 | ~ClBackend() = default; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 42 | |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 43 | static const BackendId& GetIdStatic(); |
| 44 | const BackendId& GetId() const override { return GetIdStatic(); } |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 45 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 46 | IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override; |
| 47 | |
| 48 | IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( |
| 49 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager = nullptr) const override; |
| 50 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 51 | IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( |
| 52 | TensorHandleFactoryRegistry& registry) const override; |
| 53 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 54 | IWorkloadFactoryPtr CreateWorkloadFactory(const IMemoryManagerSharedPtr& memoryManager, |
| 55 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 56 | |
| 57 | IWorkloadFactoryPtr CreateWorkloadFactory(class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 58 | const ModelOptions& modelOptions) const override; |
| 59 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 60 | IWorkloadFactoryPtr CreateWorkloadFactory(class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 61 | const ModelOptions& modelOptions, |
| 62 | MemorySourceFlags inputFlags, |
| 63 | MemorySourceFlags outputFlags) const override; |
| 64 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 65 | std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const override; |
| 66 | |
| 67 | void RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry) override; |
| 68 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 69 | void RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry, |
| 70 | MemorySourceFlags inputFlags, |
| 71 | MemorySourceFlags outputFlags) override; |
| 72 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 73 | IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const override; |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 74 | IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext( |
| 75 | const IRuntime::CreationOptions&, IBackendProfilingPtr& backendProfiling) override; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 76 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 77 | IBackendInternal::Optimizations GetOptimizations() const override; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 78 | IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 79 | IBackendInternal::ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const override; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 80 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 81 | OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph, |
| 82 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 83 | |
| 84 | IBackendInternal::IBackendSpecificModelContextPtr CreateBackendSpecificModelContext( |
| 85 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | aede8ca | 2021-03-31 16:12:13 +0100 | [diff] [blame] | 86 | |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 87 | BackendCapabilities GetCapabilities() const override |
| 88 | { |
| 89 | return gpuAccCapabilities; |
| 90 | }; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 91 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 92 | virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator, |
| 93 | armnn::Optional<std::string&> errMsg) override |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 94 | { |
| 95 | IgnoreUnused(errMsg); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 96 | ARMNN_LOG(info) << "Using Custom Allocator for ClBackend"; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 97 | |
| 98 | // Set flag to signal the backend to use a custom memory allocator |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 99 | m_CustomAllocator = std::make_shared<ClBackendCustomAllocatorWrapper>(std::move(allocator)); |
| 100 | m_UsingCustomAllocator = true; |
| 101 | return m_UsingCustomAllocator; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 104 | // Cl requires a arm_compute::IAllocator we wrap the Arm NN ICustomAllocator to achieve this |
| 105 | class ClBackendCustomAllocatorWrapper : public arm_compute::IAllocator |
| 106 | { |
| 107 | public: |
| 108 | ClBackendCustomAllocatorWrapper(std::shared_ptr<ICustomAllocator> alloc) : m_CustomAllocator(alloc) |
| 109 | {} |
| 110 | // Inherited methods overridden: |
| 111 | void* allocate(size_t size, size_t alignment) override |
| 112 | { |
| 113 | auto alloc = m_CustomAllocator->allocate(size, alignment); |
| 114 | return MapAllocatedMemory(alloc, size, m_CustomAllocator->GetMemorySourceType()); |
| 115 | } |
| 116 | void free(void* ptr) override |
| 117 | { |
| 118 | auto hostMemPtr = m_AllocatedBufferMappings[ptr]; |
| 119 | clReleaseMemObject(static_cast<cl_mem>(ptr)); |
| 120 | m_CustomAllocator->free(hostMemPtr); |
| 121 | } |
| 122 | std::unique_ptr<arm_compute::IMemoryRegion> make_region(size_t size, size_t alignment) override |
| 123 | { |
| 124 | auto hostMemPtr = m_CustomAllocator->allocate(size, alignment); |
| 125 | cl_mem buffer = MapAllocatedMemory(hostMemPtr, size, m_CustomAllocator->GetMemorySourceType()); |
| 126 | |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 127 | return std::make_unique<ClBackendCustomAllocatorMemoryRegion>(cl::Buffer(buffer), |
| 128 | hostMemPtr, |
| 129 | m_CustomAllocator->GetMemorySourceType()); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 130 | } |
| 131 | private: |
| 132 | cl_mem MapAllocatedMemory(void* memory, size_t size, MemorySource source) |
| 133 | { |
| 134 | // Round the size of the buffer to a multiple of the CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE |
| 135 | auto cachelineAlignment = |
| 136 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
| 137 | auto roundedSize = cachelineAlignment + size - (size % cachelineAlignment); |
| 138 | |
| 139 | if (source == MemorySource::Malloc) |
| 140 | { |
| 141 | const cl_import_properties_arm importProperties[] = |
| 142 | { |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 143 | CL_IMPORT_TYPE_ARM, |
| 144 | CL_IMPORT_TYPE_HOST_ARM, |
| 145 | 0 |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 146 | }; |
| 147 | cl_int error = CL_SUCCESS; |
| 148 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 149 | CL_MEM_READ_WRITE, |
| 150 | importProperties, |
| 151 | memory, |
| 152 | roundedSize, |
| 153 | &error); |
| 154 | if (error == CL_SUCCESS) |
| 155 | { |
| 156 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 157 | return buffer; |
| 158 | } |
| 159 | throw armnn::Exception( |
| 160 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " + std::to_string(error)); |
| 161 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 162 | else if (source == MemorySource::DmaBuf) |
| 163 | { |
| 164 | const cl_import_properties_arm importProperties[] = |
| 165 | { |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 166 | CL_IMPORT_TYPE_ARM, |
| 167 | CL_IMPORT_TYPE_DMA_BUF_ARM, |
| 168 | CL_IMPORT_DMA_BUF_DATA_CONSISTENCY_WITH_HOST_ARM, |
| 169 | CL_TRUE, |
| 170 | 0 |
| 171 | }; |
| 172 | cl_int error = CL_SUCCESS; |
| 173 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 174 | CL_MEM_READ_WRITE, |
| 175 | importProperties, |
| 176 | memory, |
| 177 | roundedSize, |
| 178 | &error); |
| 179 | if (error == CL_SUCCESS) |
| 180 | { |
| 181 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 182 | return buffer; |
| 183 | } |
| 184 | throw armnn::Exception( |
| 185 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " |
| 186 | + std::to_string(error)); |
| 187 | } |
| 188 | else if (source == MemorySource::DmaBufProtected) |
| 189 | { |
| 190 | const cl_import_properties_arm importProperties[] = |
| 191 | { |
| 192 | CL_IMPORT_TYPE_ARM, |
| 193 | CL_IMPORT_TYPE_DMA_BUF_ARM, |
| 194 | CL_IMPORT_TYPE_PROTECTED_ARM, |
| 195 | CL_TRUE, |
| 196 | 0 |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 197 | }; |
| 198 | cl_int error = CL_SUCCESS; |
| 199 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 200 | CL_MEM_READ_WRITE, |
| 201 | importProperties, |
| 202 | memory, |
| 203 | roundedSize, |
| 204 | &error); |
| 205 | if (error == CL_SUCCESS) |
| 206 | { |
| 207 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 208 | return buffer; |
| 209 | } |
| 210 | throw armnn::Exception( |
| 211 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " |
| 212 | + std::to_string(error)); |
| 213 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 214 | throw armnn::Exception( |
| 215 | "Attempting to allocate memory with unsupported MemorySource type in CustomAllocator"); |
| 216 | } |
| 217 | std::shared_ptr<ICustomAllocator> m_CustomAllocator; |
| 218 | std::map<void*, void*> m_AllocatedBufferMappings; |
| 219 | }; |
| 220 | |
| 221 | class ClBackendCustomAllocatorMemoryRegion : public arm_compute::ICLMemoryRegion |
| 222 | { |
| 223 | public: |
| 224 | // We need to have a new version of ICLMemoryRegion which holds a hostMemPtr to allow for cpu copy access |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 225 | ClBackendCustomAllocatorMemoryRegion(const cl::Buffer &buffer, void* hostMemPtr, armnn::MemorySource source) |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 226 | : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>()) |
| 227 | { |
| 228 | _mem = buffer; |
| 229 | m_HostMemPtr = hostMemPtr; |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 230 | m_MemorySource = source; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Inherited methods overridden : |
| 234 | void* ptr() override |
| 235 | { |
| 236 | return nullptr; |
| 237 | } |
| 238 | |
| 239 | void* map(cl::CommandQueue &q, bool blocking) override |
| 240 | { |
| 241 | armnn::IgnoreUnused(q, blocking); |
| 242 | if (m_HostMemPtr == nullptr) |
| 243 | { |
| 244 | throw armnn::Exception("ClBackend: Attempting to map memory with an invalid host ptr"); |
| 245 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 246 | if (_mapping != nullptr) |
| 247 | { |
| 248 | throw armnn::Exception("ClBackend: Attempting to map memory which has not yet been unmapped"); |
| 249 | } |
| 250 | switch (m_MemorySource) |
| 251 | { |
| 252 | case armnn::MemorySource::Malloc: |
| 253 | _mapping = m_HostMemPtr; |
| 254 | return _mapping; |
| 255 | break; |
| 256 | case armnn::MemorySource::DmaBuf: |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 257 | case armnn::MemorySource::DmaBufProtected: |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 258 | // If the source is a Dmabuf then the memory ptr should be pointing to an integer value for the fd |
| 259 | _mapping = mmap(NULL, _size, PROT_WRITE, MAP_SHARED, *(reinterpret_cast<int*>(m_HostMemPtr)), 0); |
| 260 | return _mapping; |
| 261 | break; |
| 262 | default: |
| 263 | throw armnn::Exception("ClBackend: Attempting to map imported memory without a valid source"); |
| 264 | break; |
| 265 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void unmap(cl::CommandQueue &q) override |
| 269 | { |
| 270 | armnn::IgnoreUnused(q); |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 271 | switch (m_MemorySource) |
| 272 | { |
| 273 | case armnn::MemorySource::Malloc: |
| 274 | _mapping = nullptr; |
| 275 | break; |
| 276 | case armnn::MemorySource::DmaBuf: |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 277 | case armnn::MemorySource::DmaBufProtected: |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 278 | munmap(_mapping, _size); |
| 279 | _mapping = nullptr; |
| 280 | break; |
| 281 | default: |
| 282 | throw armnn::Exception("ClBackend: Attempting to unmap imported memory without a valid source"); |
| 283 | break; |
| 284 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 285 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 286 | private: |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 287 | void* m_HostMemPtr = nullptr; |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 288 | armnn::MemorySource m_MemorySource; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | std::shared_ptr<ClBackendCustomAllocatorWrapper> m_CustomAllocator; |
| 292 | bool m_UsingCustomAllocator = false; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 293 | }; |
| 294 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 295 | } // namespace armnn |