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;