From d9e55f05847792edbbf6a8c2c4d3901d37f63d1f Mon Sep 17 00:00:00 2001 From: Cathal Corbett Date: Wed, 11 Jan 2023 13:03:21 +0000 Subject: Move tuning and IClTensorHandle code from cl to aclCommon backend. * Required to enable easier future merging and rebase into experimental/GpuFsa as part of IVGCVSW-7380. Signed-off-by: Cathal Corbett Change-Id: I066dcf00523ff430a0908666e452548ab848bd86 --- src/backends/aclCommon/ArmComputeTuningUtils.cpp | 60 +++++++ src/backends/aclCommon/ArmComputeTuningUtils.hpp | 84 ++++++++++ src/backends/aclCommon/CMakeLists.txt | 5 +- src/backends/aclCommon/IClTensorHandle.hpp | 22 +++ src/backends/aclCommon/common.mk | 1 + src/backends/cl/CMakeLists.txt | 3 +- src/backends/cl/ClBackendContext.cpp | 86 +--------- src/backends/cl/ClContextControl.cpp | 53 +------ src/backends/cl/ClContextControl.hpp | 22 +-- src/backends/cl/ClImportTensorHandle.hpp | 4 +- src/backends/cl/ClTensorHandle.hpp | 4 +- src/backends/cl/IClTensorHandle.hpp | 22 --- src/backends/cl/test/CMakeLists.txt | 4 +- src/backends/cl/test/ClDefaultAllocatorTests.cpp | 194 +++++++++++++++++++++++ src/backends/cl/test/DefaultAllocatorTests.cpp | 194 ----------------------- 15 files changed, 379 insertions(+), 379 deletions(-) create mode 100644 src/backends/aclCommon/ArmComputeTuningUtils.cpp create mode 100644 src/backends/aclCommon/ArmComputeTuningUtils.hpp create mode 100644 src/backends/aclCommon/IClTensorHandle.hpp delete mode 100644 src/backends/cl/IClTensorHandle.hpp create mode 100644 src/backends/cl/test/ClDefaultAllocatorTests.cpp delete mode 100644 src/backends/cl/test/DefaultAllocatorTests.cpp diff --git a/src/backends/aclCommon/ArmComputeTuningUtils.cpp b/src/backends/aclCommon/ArmComputeTuningUtils.cpp new file mode 100644 index 0000000000..4680541ae5 --- /dev/null +++ b/src/backends/aclCommon/ArmComputeTuningUtils.cpp @@ -0,0 +1,60 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ArmComputeTuningUtils.hpp" + +namespace armnn +{ + +IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(IGpuAccTunedParameters::Mode mode, + IGpuAccTunedParameters::TuningLevel tuningLevel) +{ + return new ClTunedParameters(mode, tuningLevel); +} + +IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(IGpuAccTunedParameters::Mode mode, + IGpuAccTunedParameters::TuningLevel tuningLevel) +{ + return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); +} + +void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) +{ + delete params; +} + +ClTunedParameters::ClTunedParameters(IGpuAccTunedParameters::Mode mode, + IGpuAccTunedParameters::TuningLevel tuningLevel) + : m_Mode(mode) + , m_TuningLevel(tuningLevel) + , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) +{ +} + +void ClTunedParameters::Load(const char* filename) +{ + try + { + m_Tuner.load_from_file(filename); + } + catch (const std::exception& e) + { + throw Exception(std::string("Failed to load tuned parameters file '") + filename + "': " + e.what()); + } +} + +void ClTunedParameters::Save(const char* filename) const +{ + try + { + m_Tuner.save_to_file(filename); + } + catch (const std::exception& e) + { + throw Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " + e.what()); + } +} + +} \ No newline at end of file diff --git a/src/backends/aclCommon/ArmComputeTuningUtils.hpp b/src/backends/aclCommon/ArmComputeTuningUtils.hpp new file mode 100644 index 0000000000..6d99d3f08e --- /dev/null +++ b/src/backends/aclCommon/ArmComputeTuningUtils.hpp @@ -0,0 +1,84 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace armnn +{ + +enum class TuningLevel +{ + None, + Rapid, + Normal, + Exhaustive +}; + +inline TuningLevel ParseTuningLevel(const BackendOptions::Var& value, TuningLevel defaultValue) +{ + if (value.IsInt()) + { + int v = value.AsInt(); + if (v > static_cast(TuningLevel::Exhaustive) || + v < static_cast(TuningLevel::None)) + { + ARMNN_LOG(warning) << "Invalid GpuAcc tuning level ("<< v << ") selected. " + "Using default(" << static_cast(defaultValue) << ")"; + } else + { + return static_cast(v); + } + } + return defaultValue; +} + +inline void ConfigureTuner(arm_compute::CLTuner &tuner, TuningLevel level) +{ + tuner.set_tune_new_kernels(true); // Turn on tuning initially. + + switch (level) + { + case TuningLevel::Rapid: + ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Rapid (1)"; + tuner.set_tuner_mode(arm_compute::CLTunerMode::RAPID); + break; + case TuningLevel::Normal: + ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Normal (2)"; + tuner.set_tuner_mode(arm_compute::CLTunerMode::NORMAL); + break; + case TuningLevel::Exhaustive: + ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Exhaustive (3)"; + tuner.set_tuner_mode(arm_compute::CLTunerMode::EXHAUSTIVE); + break; + case TuningLevel::None: + default: + tuner.set_tune_new_kernels(false); // Turn off tuning. Set to "use" only mode. + break; + } +} + +class ClTunedParameters : public IGpuAccTunedParameters +{ +public: + ClTunedParameters(IGpuAccTunedParameters::Mode mode, IGpuAccTunedParameters::TuningLevel tuningLevel); + + virtual void Load(const char* filename); + virtual void Save(const char* filename) const; + + Mode m_Mode; + TuningLevel m_TuningLevel; + + arm_compute::CLTuner m_Tuner; + arm_compute::CLGEMMHeuristicsHandle m_HeuristicsHandle; +}; + +} \ No newline at end of file diff --git a/src/backends/aclCommon/CMakeLists.txt b/src/backends/aclCommon/CMakeLists.txt index 05fbe6cca9..b3bf89e750 100644 --- a/src/backends/aclCommon/CMakeLists.txt +++ b/src/backends/aclCommon/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright © 2017 Arm Ltd. All rights reserved. +# Copyright © 2017, 2023 Arm Ltd and Contributors. All rights reserved. # SPDX-License-Identifier: MIT # @@ -8,9 +8,12 @@ list(APPEND armnnAclCommon_sources ArmComputeTensorHandle.hpp ArmComputeTensorUtils.hpp ArmComputeTensorUtils.cpp + ArmComputeTuningUtils.hpp + ArmComputeTuningUtils.cpp ArmComputeUtils.hpp BaseMemoryManager.cpp BaseMemoryManager.hpp + IClTensorHandle.hpp ) if(BUILD_UNIT_TESTS) diff --git a/src/backends/aclCommon/IClTensorHandle.hpp b/src/backends/aclCommon/IClTensorHandle.hpp new file mode 100644 index 0000000000..48cf5f57d6 --- /dev/null +++ b/src/backends/aclCommon/IClTensorHandle.hpp @@ -0,0 +1,22 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + +#include +#include + +namespace armnn +{ + +class IClTensorHandle : public IAclTensorHandle +{ +public: + virtual arm_compute::ICLTensor& GetTensor() = 0; + virtual arm_compute::ICLTensor const& GetTensor() const = 0; + virtual arm_compute::DataType GetDataType() const = 0; + virtual void SetMemoryGroup(const std::shared_ptr& memoryGroup) = 0; +}; + +} //namespace armnn \ No newline at end of file diff --git a/src/backends/aclCommon/common.mk b/src/backends/aclCommon/common.mk index 0ba966af14..b113269df9 100644 --- a/src/backends/aclCommon/common.mk +++ b/src/backends/aclCommon/common.mk @@ -9,6 +9,7 @@ COMMON_SOURCES := \ ArmComputeTensorUtils.cpp \ + ArmComputeTuningUtils.cpp \ BaseMemoryManager.cpp # COMMON_TEST_SOURCES contains the list of files to be included diff --git a/src/backends/cl/CMakeLists.txt b/src/backends/cl/CMakeLists.txt index aeb90b069c..20c42061fc 100644 --- a/src/backends/cl/CMakeLists.txt +++ b/src/backends/cl/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright © 2017 Arm Ltd. All rights reserved. +# Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. # SPDX-License-Identifier: MIT # @@ -44,7 +44,6 @@ if(ARMCOMPUTECL) ClTensorHandleFactory.hpp ClWorkloadFactory.cpp ClWorkloadFactory.hpp - IClTensorHandle.hpp ICLTensorProxy.hpp OpenClTimer.cpp OpenClTimer.hpp diff --git a/src/backends/cl/ClBackendContext.cpp b/src/backends/cl/ClBackendContext.cpp index c63fb0c893..adee2763ba 100644 --- a/src/backends/cl/ClBackendContext.cpp +++ b/src/backends/cl/ClBackendContext.cpp @@ -1,5 +1,5 @@ // -// Copyright © 2017 Arm Ltd. All rights reserved. +// Copyright © 2017, 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // @@ -59,84 +59,6 @@ struct ClBackendContext::ClContextControlWrapper ClContextControl m_ClContextControl; }; -std::string LowerString(std::string value) -{ - std::transform(value.begin(), value.end(), value.begin(), - [](unsigned char c){ return std::tolower(c); }); - - return value; -} - -enum class TuningLevel -{ - None, - Rapid, - Normal, - Exhaustive -}; - - -TuningLevel ParseTuningLevel(const BackendOptions::Var& value, TuningLevel defaultValue) -{ - if (value.IsInt()) - { - int v = value.AsInt(); - if (v > static_cast(TuningLevel::Exhaustive) || - v < static_cast(TuningLevel::None)) - { - ARMNN_LOG(warning) << "Invalid GpuAcc tuning level ("<< v << ") selected. " - "Using default(" << static_cast(defaultValue) << ")"; - } else - { - return static_cast(v); - } - } - return defaultValue; -} - -bool ParseBoolean(const BackendOptions::Var& value, bool defaultValue) -{ - if (value.IsBool()) - { - return value.AsBool(); - } - return defaultValue; -} - -std::string ParseFile(const BackendOptions::Var& value, std::string defaultValue) -{ - if (value.IsString()) - { - return value.AsString(); - } - return defaultValue; -} - -void ConfigureTuner(arm_compute::CLTuner &tuner, TuningLevel level) -{ - tuner.set_tune_new_kernels(true); // Turn on tuning initially. - - switch (level) - { - case TuningLevel::Rapid: - ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Rapid (1)"; - tuner.set_tuner_mode(arm_compute::CLTunerMode::RAPID); - break; - case TuningLevel::Normal: - ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Normal (2)"; - tuner.set_tuner_mode(arm_compute::CLTunerMode::NORMAL); - break; - case TuningLevel::Exhaustive: - ARMNN_LOG(info) << "Gpu tuning is activated. TuningLevel: Exhaustive (3)"; - tuner.set_tuner_mode(arm_compute::CLTunerMode::EXHAUSTIVE); - break; - case TuningLevel::None: - default: - tuner.set_tune_new_kernels(false); // Turn off tuning. Set to "use" only mode. - break; - } -} - ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options) : IBackendContext(options) , m_TuningFile() @@ -191,17 +113,17 @@ ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options) { if (name == "KernelProfilingEnabled") { - kernelProfiling |= ParseBoolean(value, false); + kernelProfiling |= ParseBooleanBackendOption(value, false); } else if (name == "TuningFile") { - m_TuningFile = ParseFile(value, ""); + m_TuningFile = ParseStringBackendOption(value, ""); } else if (name == "TuningLevel") { tuningLevel = ParseTuningLevel(value, defaultTuningLevel); } else if (name == "MLGOTuningFilePath") { - m_MLGOTuningFile = ParseFile(value, ""); + m_MLGOTuningFile = ParseStringBackendOption(value, ""); } }); diff --git a/src/backends/cl/ClContextControl.cpp b/src/backends/cl/ClContextControl.cpp index fd2d0f53eb..34eca961b4 100644 --- a/src/backends/cl/ClContextControl.cpp +++ b/src/backends/cl/ClContextControl.cpp @@ -1,5 +1,5 @@ // -// Copyright © 2017 Arm Ltd. All rights reserved. +// Copyright © 2017, 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // @@ -166,55 +166,4 @@ void ClContextControl::ClearClCache() DoLoadOpenClRuntime(true); } -armnn::IGpuAccTunedParameters* IGpuAccTunedParameters::CreateRaw(armnn::IGpuAccTunedParameters::Mode mode, - armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) -{ - return new ClTunedParameters(mode, tuningLevel); -} - -armnn::IGpuAccTunedParametersPtr IGpuAccTunedParameters::Create(armnn::IGpuAccTunedParameters::Mode mode, - armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) -{ - return IGpuAccTunedParametersPtr(CreateRaw(mode, tuningLevel), &IGpuAccTunedParameters::Destroy); -} - -void IGpuAccTunedParameters::Destroy(IGpuAccTunedParameters* params) -{ - delete params; -} - -ClTunedParameters::ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, - armnn::IGpuAccTunedParameters::TuningLevel tuningLevel) - : m_Mode(mode) - , m_TuningLevel(tuningLevel) - , m_Tuner(mode == ClTunedParameters::Mode::UpdateTunedParameters) -{ -} - -void ClTunedParameters::Load(const char* filename) -{ - try - { - m_Tuner.load_from_file(filename); - } - catch (const std::exception& e) - { - throw armnn::Exception(std::string("Failed to load tuned parameters file '") + filename + "': " + - e.what()); - } -} - -void ClTunedParameters::Save(const char* filename) const -{ - try - { - m_Tuner.save_to_file(filename); - } - catch (const std::exception& e) - { - throw armnn::Exception(std::string("Failed to save tuned parameters file to '") + filename + "': " + - e.what()); - } -} - } // namespace armnn diff --git a/src/backends/cl/ClContextControl.hpp b/src/backends/cl/ClContextControl.hpp index 4a640cdf22..7520d102a5 100644 --- a/src/backends/cl/ClContextControl.hpp +++ b/src/backends/cl/ClContextControl.hpp @@ -1,13 +1,10 @@ // -// Copyright © 2017 Arm Ltd. All rights reserved. +// Copyright © 2017, 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once -#include "armnn/IRuntime.hpp" - -#include -#include +#include namespace armnn { @@ -42,19 +39,4 @@ private: bool m_ProfilingEnabled; }; -class ClTunedParameters : public IGpuAccTunedParameters -{ -public: - ClTunedParameters(armnn::IGpuAccTunedParameters::Mode mode, armnn::IGpuAccTunedParameters::TuningLevel tuningLevel); - - virtual void Load(const char* filename); - virtual void Save(const char* filename) const; - - Mode m_Mode; - TuningLevel m_TuningLevel; - - arm_compute::CLTuner m_Tuner; - arm_compute::CLGEMMHeuristicsHandle m_HeuristicsHandle; -}; - } // namespace armnn diff --git a/src/backends/cl/ClImportTensorHandle.hpp b/src/backends/cl/ClImportTensorHandle.hpp index 889a2ad5f3..a03a4e9ea6 100644 --- a/src/backends/cl/ClImportTensorHandle.hpp +++ b/src/backends/cl/ClImportTensorHandle.hpp @@ -1,5 +1,5 @@ // -// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include diff --git a/src/backends/cl/ClTensorHandle.hpp b/src/backends/cl/ClTensorHandle.hpp index f63f1faa07..3d750f9059 100644 --- a/src/backends/cl/ClTensorHandle.hpp +++ b/src/backends/cl/ClTensorHandle.hpp @@ -1,5 +1,5 @@ // -// Copyright © 2017 Arm Ltd. All rights reserved. +// Copyright © 2017, 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once @@ -18,7 +18,7 @@ #include #include -#include +#include namespace armnn { diff --git a/src/backends/cl/IClTensorHandle.hpp b/src/backends/cl/IClTensorHandle.hpp deleted file mode 100644 index 48cf5f57d6..0000000000 --- a/src/backends/cl/IClTensorHandle.hpp +++ /dev/null @@ -1,22 +0,0 @@ -// -// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. -// SPDX-License-Identifier: MIT -// -#pragma once - -#include -#include - -namespace armnn -{ - -class IClTensorHandle : public IAclTensorHandle -{ -public: - virtual arm_compute::ICLTensor& GetTensor() = 0; - virtual arm_compute::ICLTensor const& GetTensor() const = 0; - virtual arm_compute::DataType GetDataType() const = 0; - virtual void SetMemoryGroup(const std::shared_ptr& memoryGroup) = 0; -}; - -} //namespace armnn \ No newline at end of file diff --git a/src/backends/cl/test/CMakeLists.txt b/src/backends/cl/test/CMakeLists.txt index ec1d0a6c2f..6568d48ce5 100644 --- a/src/backends/cl/test/CMakeLists.txt +++ b/src/backends/cl/test/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright © 2017 Arm Ltd. All rights reserved. +# Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. # SPDX-License-Identifier: MIT # @@ -8,6 +8,7 @@ list(APPEND armnnClBackendUnitTests_sources ClContextControlFixture.hpp ClContextSerializerTests.cpp ClCustomAllocatorTests.cpp + ClDefaultAllocatorTests.cpp ClCreateWorkloadTests.cpp ClEndToEndTests.cpp ClImportTensorHandleFactoryTests.cpp @@ -18,7 +19,6 @@ list(APPEND armnnClBackendUnitTests_sources ClOptimizedNetworkTests.cpp ClRuntimeTests.cpp ClWorkloadFactoryHelper.hpp - DefaultAllocatorTests.cpp Fp16SupportTest.cpp ICLTensorProxyTests.cpp OpenClTimerTest.cpp diff --git a/src/backends/cl/test/ClDefaultAllocatorTests.cpp b/src/backends/cl/test/ClDefaultAllocatorTests.cpp new file mode 100644 index 0000000000..411a480815 --- /dev/null +++ b/src/backends/cl/test/ClDefaultAllocatorTests.cpp @@ -0,0 +1,194 @@ +// +// Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include +#include +#include +#include +#include +// Requires the OpenCl backend to be included (GpuAcc) +#include +#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()); +} + +} + + +TEST_SUITE("ClDefaultAllocatorTests") +{ + +TEST_CASE("ClDefaultAllocatorTest") +{ + 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("ClDefaultAllocatorTestMulti") +{ + 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()); +} + +} + +} // namespace armnn \ No newline at end of file diff --git a/src/backends/cl/test/DefaultAllocatorTests.cpp b/src/backends/cl/test/DefaultAllocatorTests.cpp deleted file mode 100644 index eaa30c8800..0000000000 --- a/src/backends/cl/test/DefaultAllocatorTests.cpp +++ /dev/null @@ -1,194 +0,0 @@ -// -// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include -#include -#include -#include -#include -#include -// Requires the OpenCl backend to be included (GpuAcc) -#include -#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()); -} - -} - - -TEST_SUITE("ClDefaultAllocatorTests") -{ - -TEST_CASE("ClDefaultAllocatorTest") -{ - 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("ClDefaultAllocatorTestMulti") -{ - 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()); -} - -} - -} // namespace armnn \ No newline at end of file -- cgit v1.2.1