blob: 6bb11a1aa19b7bef0335b3d5a108e2872c1d0e57 [file] [log] [blame]
Francis Murtaghe8d7ccb2021-10-14 17:30:24 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn/backends/ICustomAllocator.hpp>
7#include <armnn/Descriptors.hpp>
8#include <armnn/Exceptions.hpp>
9#include <armnn/IRuntime.hpp>
Colm Donelan0c479742021-12-10 12:43:54 +000010#include <armnn/backends/TensorHandle.hpp>
Francis Murtaghe8d7ccb2021-10-14 17:30:24 +010011// Requires the OpenCl backend to be included (GpuAcc)
12#include <cl/ClBackend.hpp>
13#include <doctest/doctest.h>
14#include <backendsCommon/DefaultAllocator.hpp>
15#include <backendsCommon/test/MockBackend.hpp>
David Monahan6642b8a2021-11-04 16:31:46 +000016#include <cl/ClBackendDefaultAllocator.hpp>
Francis Murtaghe8d7ccb2021-10-14 17:30:24 +010017
18using namespace armnn;
19
20
21namespace
22{
23
24TEST_SUITE("DefaultAllocatorTests")
25{
26
27TEST_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
53TEST_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
90TEST_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
David Monahan6642b8a2021-11-04 16:31:46 +0000122}
123
124
125TEST_SUITE("ClDefaultAllocatorTests")
126{
127
128TEST_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
154TEST_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}
Francis Murtaghe8d7ccb2021-10-14 17:30:24 +0100190
191}
192
193} // namespace armnn