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>
28 ClBackend(std::shared_ptr<ICustomAllocator> allocator)
89 m_CustomAllocator = std::make_shared<ClBackendCustomAllocatorWrapper>(std::move(allocator));
103 void*
allocate(
size_t size,
size_t alignment)
override
105 auto alloc = m_CustomAllocator->allocate(size, alignment);
106 return MapAllocatedMemory(alloc, size, m_CustomAllocator->GetMemorySourceType());
110 auto hostMemPtr = m_AllocatedBufferMappings[ptr];
111 clReleaseMemObject(
static_cast<cl_mem
>(ptr));
112 m_CustomAllocator->free(hostMemPtr);
114 std::unique_ptr<arm_compute::IMemoryRegion>
make_region(
size_t size,
size_t alignment)
override
116 auto hostMemPtr = m_CustomAllocator->allocate(size, alignment);
117 cl_mem buffer = MapAllocatedMemory(hostMemPtr, size, m_CustomAllocator->GetMemorySourceType());
119 return std::make_unique<ClBackendCustomAllocatorMemoryRegion>(cl::Buffer(buffer),
121 m_CustomAllocator->GetMemorySourceType());
124 cl_mem MapAllocatedMemory(
void* memory,
size_t size,
MemorySource source)
127 auto cachelineAlignment =
128 arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
129 auto roundedSize = cachelineAlignment + size - (size % cachelineAlignment);
133 const cl_import_properties_arm importProperties[] =
136 CL_IMPORT_TYPE_HOST_ARM,
139 cl_int
error = CL_SUCCESS;
140 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(),
146 if (error == CL_SUCCESS)
148 m_AllocatedBufferMappings.insert(std::make_pair(
static_cast<void *
>(buffer), memory));
152 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " + std::to_string(error));
156 const cl_import_properties_arm importProperties[] =
159 CL_IMPORT_TYPE_DMA_BUF_ARM,
160 CL_IMPORT_DMA_BUF_DATA_CONSISTENCY_WITH_HOST_ARM,
164 cl_int
error = CL_SUCCESS;
165 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(),
171 if (error == CL_SUCCESS)
173 m_AllocatedBufferMappings.insert(std::make_pair(
static_cast<void *
>(buffer), memory));
177 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: "
178 + std::to_string(error));
182 const cl_import_properties_arm importProperties[] =
185 CL_IMPORT_TYPE_DMA_BUF_ARM,
186 CL_IMPORT_TYPE_PROTECTED_ARM,
190 cl_int
error = CL_SUCCESS;
191 cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(),
197 if (error == CL_SUCCESS)
199 m_AllocatedBufferMappings.insert(std::make_pair(
static_cast<void *
>(buffer), memory));
203 "Mapping allocated memory from CustomMemoryAllocator failed, errcode: "
204 + std::to_string(error));
207 "Attempting to allocate memory with unsupported MemorySource type in CustomAllocator");
209 std::shared_ptr<ICustomAllocator> m_CustomAllocator;
210 std::map<void*, void*> m_AllocatedBufferMappings;
218 : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>())
221 m_HostMemPtr = hostMemPtr;
222 m_MemorySource = source;
231 void*
map(cl::CommandQueue &q,
bool blocking)
override
234 if (m_HostMemPtr ==
nullptr)
236 throw armnn::Exception(
"ClBackend: Attempting to map memory with an invalid host ptr");
238 if (_mapping !=
nullptr)
240 throw armnn::Exception(
"ClBackend: Attempting to map memory which has not yet been unmapped");
242 switch (m_MemorySource)
245 _mapping = m_HostMemPtr;
251 _mapping = mmap(NULL, _size, PROT_WRITE, MAP_SHARED, *(
reinterpret_cast<int*
>(m_HostMemPtr)), 0);
255 throw armnn::Exception(
"ClBackend: Attempting to map imported memory without a valid source");
260 void unmap(cl::CommandQueue &q)
override
263 switch (m_MemorySource)
270 munmap(_mapping, _size);
274 throw armnn::Exception(
"ClBackend: Attempting to unmap imported memory without a valid source");
279 void* m_HostMemPtr =
nullptr;
#define ARMNN_LOG(severity)
void unmap(cl::CommandQueue &q) override
ClBackendCustomAllocatorMemoryRegion(const cl::Buffer &buffer, void *hostMemPtr, armnn::MemorySource source)
void * map(cl::CommandQueue &q, bool blocking) override
void * allocate(size_t size, size_t alignment) override
void free(void *ptr) override
std::unique_ptr< arm_compute::IMemoryRegion > make_region(size_t size, size_t alignment) override
ClBackendCustomAllocatorWrapper(std::shared_ptr< ICustomAllocator > alloc)
static const BackendId & GetIdStatic()
IBackendInternal::IBackendSpecificModelContextPtr CreateBackendSpecificModelContext(const ModelOptions &modelOptions) const override
OptimizationViews OptimizeSubgraphView(const SubgraphView &subgraph, const ModelOptions &modelOptions) const override
void RegisterTensorHandleFactories(TensorHandleFactoryRegistry ®istry) override
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
std::vector< ITensorHandleFactory::FactoryId > GetHandleFactoryPreferences() const override
(Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions &, IBackendProfilingPtr &backendProfiling) override
Create context specifically used for profiling interaction from backends.
const BackendId & GetId() const override
IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions &) const override
Create the runtime context of the backend.
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override
ClBackend(std::shared_ptr< ICustomAllocator > allocator)
BackendCapabilities GetCapabilities() const override
Returns a BackendCapability if the backend lists the capability The BackendCapability must then be in...
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.
bool m_UsingCustomAllocator
virtual unsigned int GetNumberOfCacheFiles() const override
Returns the number of files cached if backend supports caching.
std::shared_ptr< ClBackendCustomAllocatorWrapper > m_CustomAllocator
std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const override
Returns the default memory allocator for the backend.
Base class for all ArmNN exceptions so that users can filter to just those.
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
std::unique_ptr< arm::pipe::IBackendProfiling > IBackendProfilingPtr
std::shared_ptr< IBackendModelContext > IBackendSpecificModelContextPtr
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
std::unique_ptr< IBackendContext > IBackendContextPtr
std::shared_ptr< arm::pipe::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
The SubgraphView class represents a subgraph of a Graph.
Copyright (c) 2021 ARM Limited and Contributors.
MemorySource
Define the Memory Source to reduce copies.
void IgnoreUnused(Ts &&...)
unsigned int MemorySourceFlags
std::vector< BackendOptions > ModelOptions
Struct for the users to pass backend specific options.