Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 3 | * |
| 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 Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/utils/misc/Utility.h" |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/MemoryRegion.h" |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 29 | |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 30 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 31 | |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 32 | #include "tests/Utils.h" |
| 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Macros.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | TEST_SUITE(NEON) |
| 43 | TEST_SUITE(UNIT) |
| 44 | TEST_SUITE(TensorAllocator) |
| 45 | |
| 46 | TEST_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 Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 52 | const size_t total_size = info.total_size(); |
| 53 | auto data = support::cpp14::make_unique<uint8_t[]>(total_size); |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 54 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 55 | // Negative case : Import pointer with zero size |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 56 | Tensor t1; |
| 57 | t1.allocator()->init(info); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_EXPECT(!bool(t1.allocator()->import_memory(data.get(), 0)), framework::LogLevel::ERRORS); |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_EXPECT(t1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 60 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 61 | // 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 Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 66 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 67 | // 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 Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 72 | ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 73 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 74 | // Positive case : Set raw pointer |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 75 | Tensor t4; |
| 76 | t4.allocator()->init(info); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_EXPECT(bool(t4.allocator()->import_memory(data.get(), total_size)), framework::LogLevel::ERRORS); |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 79 | ARM_COMPUTE_EXPECT(t4.buffer() == reinterpret_cast<uint8_t *>(data.get()), framework::LogLevel::ERRORS); |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 80 | 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 Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 85 | TEST_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 Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 101 | TEST_SUITE_END() |
| 102 | TEST_SUITE_END() |
| 103 | TEST_SUITE_END() |
| 104 | } // namespace validation |
| 105 | } // namespace test |
| 106 | } // namespace arm_compute |