blob: 384a00855b3068526f7c926af6cd77eb18169eb4 [file] [log] [blame]
Georgios Pinitas84b51ad2017-11-07 13:24:57 +00001/*
Georgios Pinitas99d40952018-04-23 16:26:46 +01002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas84b51ad2017-11-07 13:24:57 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/TensorAllocator.h"
25
Georgios Pinitas17b12302018-06-18 18:13:51 +010026#include "arm_compute/core/utils/misc/Utility.h"
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000027#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010028#include "arm_compute/runtime/MemoryRegion.h"
Georgios Pinitas17b12302018-06-18 18:13:51 +010029
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000030#include "support/ToolchainSupport.h"
Georgios Pinitas17b12302018-06-18 18:13:51 +010031
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000032#include "tests/Utils.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Macros.h"
35
36namespace arm_compute
37{
38namespace test
39{
40namespace validation
41{
42TEST_SUITE(NEON)
43TEST_SUITE(UNIT)
44TEST_SUITE(TensorAllocator)
45
46TEST_CASE(ImportMemory, framework::DatasetMode::ALL)
47{
48 // Init tensor info
49 TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
50
51 // Allocate memory buffer
Georgios Pinitasdf310362018-11-14 13:16:56 +000052 const size_t total_size = info.total_size();
53 auto data = support::cpp14::make_unique<uint8_t[]>(total_size);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000054
Georgios Pinitasdf310362018-11-14 13:16:56 +000055 // Negative case : Import pointer with zero size
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000056 Tensor t1;
57 t1.allocator()->init(info);
Georgios Pinitasdf310362018-11-14 13:16:56 +000058 ARM_COMPUTE_EXPECT(!bool(t1.allocator()->import_memory(data.get(), 0)), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000059 ARM_COMPUTE_EXPECT(t1.info()->is_resizable(), framework::LogLevel::ERRORS);
60
Georgios Pinitasdf310362018-11-14 13:16:56 +000061 // Negative case : Import nullptr
62 Tensor t2;
63 t2.allocator()->init(info);
64 ARM_COMPUTE_EXPECT(!bool(t2.allocator()->import_memory(nullptr, total_size)), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000065 ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS);
66
Georgios Pinitasdf310362018-11-14 13:16:56 +000067 // Negative case : Import memory to a tensor that is memory managed
68 Tensor t3;
69 MemoryGroup mg;
70 t3.allocator()->set_associated_memory_group(&mg);
71 ARM_COMPUTE_EXPECT(!bool(t3.allocator()->import_memory(data.get(), total_size)), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000072 ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000073
Georgios Pinitasdf310362018-11-14 13:16:56 +000074 // Positive case : Set raw pointer
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000075 Tensor t4;
76 t4.allocator()->init(info);
Georgios Pinitasdf310362018-11-14 13:16:56 +000077 ARM_COMPUTE_EXPECT(bool(t4.allocator()->import_memory(data.get(), total_size)), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000078 ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS);
Georgios Pinitasdf310362018-11-14 13:16:56 +000079 ARM_COMPUTE_EXPECT(t4.buffer() == reinterpret_cast<uint8_t *>(data.get()), framework::LogLevel::ERRORS);
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000080 t4.allocator()->free();
81 ARM_COMPUTE_EXPECT(t4.info()->is_resizable(), framework::LogLevel::ERRORS);
82 ARM_COMPUTE_EXPECT(t4.buffer() == nullptr, framework::LogLevel::ERRORS);
83}
84
Georgios Pinitas17b12302018-06-18 18:13:51 +010085TEST_CASE(AlignedAlloc, framework::DatasetMode::ALL)
86{
87 // Init tensor info
88 TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32);
89 const size_t requested_alignment = 1024;
90
91 Tensor t;
92 t.allocator()->init(info, requested_alignment);
93 t.allocator()->allocate();
94
95 ARM_COMPUTE_EXPECT(t.buffer() != nullptr, framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(t.allocator()->alignment() == requested_alignment, framework::LogLevel::ERRORS);
97 ARM_COMPUTE_EXPECT(arm_compute::utility::check_aligned(reinterpret_cast<void *>(t.buffer()), requested_alignment),
98 framework::LogLevel::ERRORS);
99}
100
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000101TEST_SUITE_END()
102TEST_SUITE_END()
103TEST_SUITE_END()
104} // namespace validation
105} // namespace test
106} // namespace arm_compute