9 #include <arm_compute/core/Types.h> 10 #include <arm_compute/runtime/CL/CLBufferAllocator.h> 13 #include <arm_compute/runtime/CL/CLMemoryRegion.h> 15 #include <arm_compute/core/CL/CLKernelLibrary.h> 16 #include <CL/cl_ext.h> 27 {
"NonConstWeights",
false},
28 {
"AsyncExecution",
false},
29 {
"ProtectedContentAllocation",
true},
30 {
"ConstantTensorsAsInputs",
false},
31 {
"PreImportIOTensors",
false},
32 {
"ExternallyManagedMemory",
true},
33 {
"MultiAxisPacking",
false},
34 {
"SingleAxisPacking",
true}
41 ClBackend(std::shared_ptr<ICustomAllocator> allocator)
105 m_CustomAllocator = std::make_shared<ClBackendCustomAllocatorWrapper>(std::move(allocator));
119 void*
allocate(
size_t size,
size_t alignment)
override 121 auto alloc = m_CustomAllocator->allocate(size, alignment);
122 return MapAllocatedMemory(alloc, size, m_CustomAllocator->GetMemorySourceType());
126 auto hostMemPtr = m_AllocatedBufferMappings[ptr];
127 clReleaseMemObject(static_cast<cl_mem>(ptr));
128 m_CustomAllocator->free(hostMemPtr);
130 std::unique_ptr<arm_compute::IMemoryRegion>
make_region(
size_t size,
size_t alignment)
override 132 auto hostMemPtr = m_CustomAllocator->allocate(size, alignment);
133 cl_mem buffer = MapAllocatedMemory(hostMemPtr, size, m_CustomAllocator->GetMemorySourceType());
135 return std::make_unique<ClBackendCustomAllocatorMemoryRegion>(cl::Buffer(buffer),
137 m_CustomAllocator->GetMemorySourceType());
140 cl_mem MapAllocatedMemory(
void* memory,
size_t size,
MemorySource source)
143 auto cachelineAlignment =
144 arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
145 auto roundedSize = cachelineAlignment + size - (size % cachelineAlignment);
149 const cl_import_properties_arm importProperties[] =
152 CL_IMPORT_TYPE_HOST_ARM,
155 cl_int
error = CL_SUCCESS;
156 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().
get(),
162 if (error == CL_SUCCESS)
164 m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory));
168 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " + std::to_string(error));
172 const cl_import_properties_arm importProperties[] =
175 CL_IMPORT_TYPE_DMA_BUF_ARM,
176 CL_IMPORT_DMA_BUF_DATA_CONSISTENCY_WITH_HOST_ARM,
180 cl_int
error = CL_SUCCESS;
181 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().
get(),
187 if (error == CL_SUCCESS)
189 m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory));
193 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " 194 + std::to_string(error));
198 const cl_import_properties_arm importProperties[] =
201 CL_IMPORT_TYPE_DMA_BUF_ARM,
202 CL_IMPORT_TYPE_PROTECTED_ARM,
206 cl_int
error = CL_SUCCESS;
207 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().
get(),
213 if (error == CL_SUCCESS)
215 m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory));
219 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " 220 + std::to_string(error));
223 "Attempting to allocate memory with unsupported MemorySource type in CustomAllocator");
225 std::shared_ptr<ICustomAllocator> m_CustomAllocator;
226 std::map<void*, void*> m_AllocatedBufferMappings;
234 : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>())
237 m_HostMemPtr = hostMemPtr;
238 m_MemorySource = source;
247 void*
map(cl::CommandQueue &q,
bool blocking)
override 250 if (m_HostMemPtr ==
nullptr)
252 throw armnn::Exception(
"ClBackend: Attempting to map memory with an invalid host ptr");
254 if (_mapping !=
nullptr)
256 throw armnn::Exception(
"ClBackend: Attempting to map memory which has not yet been unmapped");
258 switch (m_MemorySource)
261 _mapping = m_HostMemPtr;
267 _mapping = mmap(NULL, _size, PROT_WRITE, MAP_SHARED, *(reinterpret_cast<int*>(m_HostMemPtr)), 0);
271 throw armnn::Exception(
"ClBackend: Attempting to map imported memory without a valid source");
276 void unmap(cl::CommandQueue &q)
override 279 switch (m_MemorySource)
286 munmap(_mapping, _size);
290 throw armnn::Exception(
"ClBackend: Attempting to unmap imported memory without a valid source");
295 void* m_HostMemPtr =
nullptr;
void * map(cl::CommandQueue &q, bool blocking) override
ClBackend(std::shared_ptr< ICustomAllocator > allocator)
void free(void *ptr) override
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
std::vector< BackendOptions > ModelOptions
std::unique_ptr< arm_compute::IMemoryRegion > make_region(size_t size, size_t alignment) override
#define ARMNN_LOG(severity)
void unmap(cl::CommandQueue &q) override
std::shared_ptr< ClBackendCustomAllocatorWrapper > m_CustomAllocator
unsigned int MemorySourceFlags
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
void IgnoreUnused(Ts &&...)
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override
void RegisterTensorHandleFactories(TensorHandleFactoryRegistry ®istry) override
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
The SubgraphView class represents a subgraph of a Graph.
IBackendInternal::IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions &modelOptions) const override
std::unique_ptr< armnn::profiling::IBackendProfiling > IBackendProfilingPtr
BackendCapabilities GetCapabilities() const override
Returns a BackendCapability if the backend lists the capability The BackendCapability must then be in...
OptimizationViews OptimizeSubgraphView(const SubgraphView &subgraph, const ModelOptions &modelOptions) const override
std::shared_ptr< IBackendModelContext > IBackendSpecificModelContextPtr
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions &) const override
Create the runtime context of the backend.
std::vector< ITensorHandleFactory::FactoryId > GetHandleFactoryPreferences() const override
(Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
virtual bool UseCustomMemoryAllocator(std::shared_ptr< ICustomAllocator > allocator, armnn::Optional< std::string &> errMsg) override
Signals the backend to use a custom memory allocator provided by the user.
BackendOptions BackendCapabilities
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
Struct for the users to pass backend specific options.
const BackendId & GetId() const override
virtual unsigned int GetNumberOfCacheFiles() const override
Returns the number of files cached if backend supports caching.
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override
const BackendCapabilities gpuAccCapabilities("GpuAcc", { {"NonConstWeights", false}, {"AsyncExecution", false}, {"ProtectedContentAllocation", true}, {"ConstantTensorsAsInputs", false}, {"PreImportIOTensors", false}, {"ExternallyManagedMemory", true}, {"MultiAxisPacking", false}, {"SingleAxisPacking", true} })
Base class for all ArmNN exceptions so that users can filter to just those.
MemorySource
Define the Memory Source to reduce copies.
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions &, IBackendProfilingPtr &backendProfiling) override
Create context specifically used for profiling interaction from backends.
bool m_UsingCustomAllocator
ClBackendCustomAllocatorMemoryRegion(const cl::Buffer &buffer, void *hostMemPtr, armnn::MemorySource source)
void * allocate(size_t size, size_t alignment) override
std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const override
Returns the default memory allocator for the backend.
static const BackendId & GetIdStatic()
ClBackendCustomAllocatorWrapper(std::shared_ptr< ICustomAllocator > alloc)
std::shared_ptr< armnn::profiling::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we'll keep it in the backend namespace...
std::unique_ptr< IBackendContext > IBackendContextPtr