From e8d7ccb1a18b5c39ecf17fb063fcc50baacc74b3 Mon Sep 17 00:00:00 2001 From: Francis Murtagh Date: Thu, 14 Oct 2021 17:30:24 +0100 Subject: IVGCVSW-6310 Update ICustomAllocator and add GetDefaultAllocator function to backends Signed-off-by: Francis Murtagh Change-Id: If2879e8a82692285e7fa3f4d09abd608ca28de12 --- src/backends/cl/test/CMakeLists.txt | 1 + src/backends/cl/test/ClCustomAllocatorTests.cpp | 10 +- src/backends/cl/test/DefaultAllocatorTests.cpp | 124 ++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/backends/cl/test/DefaultAllocatorTests.cpp (limited to 'src/backends/cl/test') diff --git a/src/backends/cl/test/CMakeLists.txt b/src/backends/cl/test/CMakeLists.txt index 41cbe24c15..8ee532a323 100644 --- a/src/backends/cl/test/CMakeLists.txt +++ b/src/backends/cl/test/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND armnnClBackendUnitTests_sources ClOptimizedNetworkTests.cpp ClRuntimeTests.cpp ClWorkloadFactoryHelper.hpp + DefaultAllocatorTests.cpp Fp16SupportTest.cpp OpenClTimerTest.cpp ) diff --git a/src/backends/cl/test/ClCustomAllocatorTests.cpp b/src/backends/cl/test/ClCustomAllocatorTests.cpp index e614f4c624..60145139ff 100644 --- a/src/backends/cl/test/ClCustomAllocatorTests.cpp +++ b/src/backends/cl/test/ClCustomAllocatorTests.cpp @@ -10,13 +10,13 @@ #include #include #include + #include #if defined(ARMCOMPUTENEON_ENABLED) #include #endif - #include - +#include // Contains the OpenCl interfaces for mapping memory in the Gpu Page Tables // Requires the OpenCl backend to be included (GpuAcc) #include @@ -31,7 +31,7 @@ class SampleClBackendCustomAllocator : public armnn::ICustomAllocator public: SampleClBackendCustomAllocator() = default; - void* allocate(size_t size, size_t alignment) + void* allocate(size_t size, size_t alignment) override { // If alignment is 0 just use the CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE for alignment if (alignment == 0) @@ -49,12 +49,12 @@ public: } /** Interface to be implemented by the child class to free the allocated tensor */ - void free(void* ptr) + void free(void* ptr) override { std::free(ptr); } - armnn::MemorySource GetMemorySourceType() + armnn::MemorySource GetMemorySourceType() override { return armnn::MemorySource::Malloc; } diff --git a/src/backends/cl/test/DefaultAllocatorTests.cpp b/src/backends/cl/test/DefaultAllocatorTests.cpp new file mode 100644 index 0000000000..196c0fb412 --- /dev/null +++ b/src/backends/cl/test/DefaultAllocatorTests.cpp @@ -0,0 +1,124 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include +#include +#include +#include +// Requires the OpenCl backend to be included (GpuAcc) +#include +#include +#include +#include + +using namespace armnn; + + +namespace +{ + +TEST_SUITE("DefaultAllocatorTests") +{ + +TEST_CASE("DefaultAllocatorTest") +{ + float number = 3; + + TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); + + // Create ArmNN runtime + IRuntime::CreationOptions options; // default options + auto customAllocator = std::make_shared(); + options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}}; + IRuntimePtr run = IRuntime::Create(options); + + // Creates structures for input & output + unsigned int numElements = inputTensorInfo.GetNumElements(); + size_t totalBytes = numElements * sizeof(float); + + void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0); + + auto* inputPtr = reinterpret_cast(alignedInputPtr); + std::fill_n(inputPtr, numElements, number); + CHECK(inputPtr[0] == 3); + + auto& backendRegistry = armnn::BackendRegistryInstance(); + backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); +} + +TEST_CASE("DefaultAllocatorTestMulti") +{ + float number = 3; + + TensorInfo inputTensorInfo(TensorShape({2, 1}), DataType::Float32); + + // Create ArmNN runtime + IRuntime::CreationOptions options; // default options + auto customAllocator = std::make_shared(); + options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}}; + IRuntimePtr run = IRuntime::Create(options); + + // Creates structures for input & output + unsigned int numElements = inputTensorInfo.GetNumElements(); + size_t totalBytes = numElements * sizeof(float); + + void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0); + void* alignedInputPtr2 = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0); + + auto* inputPtr = reinterpret_cast(alignedInputPtr); + std::fill_n(inputPtr, numElements, number); + CHECK(inputPtr[0] == 3); + CHECK(inputPtr[1] == 3); + + auto* inputPtr2 = reinterpret_cast(alignedInputPtr2); + std::fill_n(inputPtr2, numElements, number); + CHECK(inputPtr2[0] == 3); + CHECK(inputPtr2[1] == 3); + + // No overlap + CHECK(inputPtr[0] == 3); + CHECK(inputPtr[1] == 3); + + auto& backendRegistry = armnn::BackendRegistryInstance(); + backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); +} + +TEST_CASE("DefaultAllocatorTestMock") +{ + // Create ArmNN runtime + IRuntime::CreationOptions options; // default options + IRuntimePtr run = IRuntime::Create(options); + + // Initialize Mock Backend + MockBackendInitialiser initialiser; + auto factoryFun = BackendRegistryInstance().GetFactory(MockBackend().GetIdStatic()); + ARMNN_ASSERT(factoryFun != nullptr); + auto backend = factoryFun(); + auto defaultAllocator = backend->GetDefaultAllocator(); + + // GetMemorySourceType + CHECK(defaultAllocator->GetMemorySourceType() == MemorySource::Malloc); + + size_t totalBytes = 1 * sizeof(float); + // Allocate + void* ptr = defaultAllocator->allocate(totalBytes, 0); + + // GetMemoryRegionAtOffset + CHECK(defaultAllocator->GetMemoryRegionAtOffset(ptr, 0, 0)); + + // Free + defaultAllocator->free(ptr); + + // Clean up + auto& backendRegistry = armnn::BackendRegistryInstance(); + backendRegistry.Deregister(MockBackend().GetIdStatic()); + backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); +} + + +} + +} // namespace armnn \ No newline at end of file -- cgit v1.2.1