blob: 17d595221722e0ef5a65de6cf449325efdc3677a [file] [log] [blame]
David Monahan8a570462023-11-22 13:24:25 +00001//
2// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn/backends/ICustomAllocator.hpp>
7#include <armnn/BackendRegistry.hpp>
8#include <armnn/Descriptors.hpp>
9#include <armnn/Exceptions.hpp>
10#include <armnn/IRuntime.hpp>
11#include <armnn/backends/TensorHandle.hpp>
12// Requires the OpenCl backend to be included (GpuFsa)
13#include <gpuFsa/GpuFsaBackend.hpp>
14#include <doctest/doctest.h>
15#include <backendsCommon/DefaultAllocator.hpp>
16#include <armnnTestUtils/MockBackend.hpp>
17#include <gpuFsa/GpuFsaBackendDefaultAllocator.hpp>
18
19using namespace armnn;
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 = {{"GpuFsa", 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["GpuFsa"]->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(GpuFsaBackend::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 = {{"GpuFsa", 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["GpuFsa"]->allocate(totalBytes, 0);
70 void* alignedInputPtr2 = options.m_CustomAllocatorMap["GpuFsa"]->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(GpuFsaBackend::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 CHECK(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(GpuFsaBackend::GetIdStatic());
120}
121
122}
123
124
125TEST_SUITE("GpuFsaDefaultAllocatorTests")
126{
127
128TEST_CASE("GpuFsaDefaultAllocatorTest")
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<GpuFsaBackendDefaultAllocator>();
137 options.m_CustomAllocatorMap = {{"GpuFsa", 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["GpuFsa"]->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(GpuFsaBackend::GetIdStatic());
152}
153
154TEST_CASE("GpuFsaDefaultAllocatorTestMulti")
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<GpuFsaBackendDefaultAllocator>();
163 options.m_CustomAllocatorMap = {{"GpuFsa", 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["GpuFsa"]->allocate(totalBytes, 0);
171 void* alignedInputPtr2 = options.m_CustomAllocatorMap["GpuFsa"]->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(GpuFsaBackend::GetIdStatic());
189}
190
191}
192
193} // namespace armnn