ArmNN
 21.11
DefaultAllocatorTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include <armnn/Descriptors.hpp>
8 #include <armnn/Exceptions.hpp>
9 #include <armnn/IRuntime.hpp>
11 // Requires the OpenCl backend to be included (GpuAcc)
12 #include <cl/ClBackend.hpp>
13 #include <doctest/doctest.h>
17 
18 using namespace armnn;
19 
20 
21 namespace
22 {
23 
24 TEST_SUITE("DefaultAllocatorTests")
25 {
26 
27 TEST_CASE("DefaultAllocatorTest")
28 {
29  float number = 3;
30 
31  TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
32 
33  // Create ArmNN runtime
34  IRuntime::CreationOptions options; // default options
35  auto customAllocator = std::make_shared<DefaultAllocator>();
36  options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}};
37  IRuntimePtr run = IRuntime::Create(options);
38 
39  // Creates structures for input & output
40  unsigned int numElements = inputTensorInfo.GetNumElements();
41  size_t totalBytes = numElements * sizeof(float);
42 
43  void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
44 
45  auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr);
46  std::fill_n(inputPtr, numElements, number);
47  CHECK(inputPtr[0] == 3);
48 
49  auto& backendRegistry = armnn::BackendRegistryInstance();
50  backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic());
51 }
52 
53 TEST_CASE("DefaultAllocatorTestMulti")
54 {
55  float number = 3;
56 
57  TensorInfo inputTensorInfo(TensorShape({2, 1}), DataType::Float32);
58 
59  // Create ArmNN runtime
60  IRuntime::CreationOptions options; // default options
61  auto customAllocator = std::make_shared<DefaultAllocator>();
62  options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}};
63  IRuntimePtr run = IRuntime::Create(options);
64 
65  // Creates structures for input & output
66  unsigned int numElements = inputTensorInfo.GetNumElements();
67  size_t totalBytes = numElements * sizeof(float);
68 
69  void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
70  void* alignedInputPtr2 = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
71 
72  auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr);
73  std::fill_n(inputPtr, numElements, number);
74  CHECK(inputPtr[0] == 3);
75  CHECK(inputPtr[1] == 3);
76 
77  auto* inputPtr2 = reinterpret_cast<float*>(alignedInputPtr2);
78  std::fill_n(inputPtr2, numElements, number);
79  CHECK(inputPtr2[0] == 3);
80  CHECK(inputPtr2[1] == 3);
81 
82  // No overlap
83  CHECK(inputPtr[0] == 3);
84  CHECK(inputPtr[1] == 3);
85 
86  auto& backendRegistry = armnn::BackendRegistryInstance();
87  backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic());
88 }
89 
90 TEST_CASE("DefaultAllocatorTestMock")
91 {
92  // Create ArmNN runtime
93  IRuntime::CreationOptions options; // default options
94  IRuntimePtr run = IRuntime::Create(options);
95 
96  // Initialize Mock Backend
97  MockBackendInitialiser initialiser;
98  auto factoryFun = BackendRegistryInstance().GetFactory(MockBackend().GetIdStatic());
99  ARMNN_ASSERT(factoryFun != nullptr);
100  auto backend = factoryFun();
101  auto defaultAllocator = backend->GetDefaultAllocator();
102 
103  // GetMemorySourceType
104  CHECK(defaultAllocator->GetMemorySourceType() == MemorySource::Malloc);
105 
106  size_t totalBytes = 1 * sizeof(float);
107  // Allocate
108  void* ptr = defaultAllocator->allocate(totalBytes, 0);
109 
110  // GetMemoryRegionAtOffset
111  CHECK(defaultAllocator->GetMemoryRegionAtOffset(ptr, 0, 0));
112 
113  // Free
114  defaultAllocator->free(ptr);
115 
116  // Clean up
117  auto& backendRegistry = armnn::BackendRegistryInstance();
118  backendRegistry.Deregister(MockBackend().GetIdStatic());
119  backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic());
120 }
121 
122 }
123 
124 
125 TEST_SUITE("ClDefaultAllocatorTests")
126 {
127 
128 TEST_CASE("ClDefaultAllocatorTest")
129 {
130  float number = 3;
131 
132  TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
133 
134  // Create ArmNN runtime
135  IRuntime::CreationOptions options; // default options
136  auto customAllocator = std::make_shared<ClBackendDefaultAllocator>();
137  options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}};
138  IRuntimePtr run = IRuntime::Create(options);
139 
140  // Creates structures for input & output
141  unsigned int numElements = inputTensorInfo.GetNumElements();
142  size_t totalBytes = numElements * sizeof(float);
143 
144  void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
145 
146  auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr);
147  std::fill_n(inputPtr, numElements, number);
148  CHECK(inputPtr[0] == 3);
149 
150  auto& backendRegistry = armnn::BackendRegistryInstance();
151  backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic());
152 }
153 
154 TEST_CASE("ClDefaultAllocatorTestMulti")
155 {
156  float number = 3;
157 
158  TensorInfo inputTensorInfo(TensorShape({2, 1}), DataType::Float32);
159 
160  // Create ArmNN runtime
161  IRuntime::CreationOptions options; // default options
162  auto customAllocator = std::make_shared<ClBackendDefaultAllocator>();
163  options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}};
164  IRuntimePtr run = IRuntime::Create(options);
165 
166  // Creates structures for input & output
167  unsigned int numElements = inputTensorInfo.GetNumElements();
168  size_t totalBytes = numElements * sizeof(float);
169 
170  void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
171  void* alignedInputPtr2 = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, 0);
172 
173  auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr);
174  std::fill_n(inputPtr, numElements, number);
175  CHECK(inputPtr[0] == 3);
176  CHECK(inputPtr[1] == 3);
177 
178  auto* inputPtr2 = reinterpret_cast<float*>(alignedInputPtr2);
179  std::fill_n(inputPtr2, numElements, number);
180  CHECK(inputPtr2[0] == 3);
181  CHECK(inputPtr2[1] == 3);
182 
183  // No overlap
184  CHECK(inputPtr[0] == 3);
185  CHECK(inputPtr[1] == 3);
186 
187  auto& backendRegistry = armnn::BackendRegistryInstance();
188  backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic());
189 }
190 
191 }
192 
193 } // namespace armnn
TEST_SUITE("TestConstTensorLayerVisitor")
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:40
FactoryFunction GetFactory(const BackendId &id) const
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:31
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
std::map< BackendId, std::shared_ptr< ICustomAllocator > > m_CustomAllocatorMap
A map to define a custom memory allocator for specific backend Ids.
Definition: IRuntime.hpp:145
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
static const BackendId & GetIdStatic()
Definition: ClBackend.cpp:45