blob: 05670a94e84bec747cad7593c2d3d18cd45a251c [file] [log] [blame]
Georgios Pinitas12833d02019-07-25 13:31:10 +01001/*
2 * Copyright (c) 2019 ARM Limited.
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/RuntimeContext.h"
25
26#include "arm_compute/runtime/CPP/CPPScheduler.h"
27#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
28#include "arm_compute/runtime/Tensor.h"
29#include "support/ToolchainSupport.h"
30#include "tests/Globals.h"
31#include "tests/NEON/Accessor.h"
32#include "tests/Utils.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Macros.h"
35#include "tests/validation/Validation.h"
36#include "tests/validation/reference/ActivationLayer.h"
37
38#include <memory>
39#include <random>
40#if !defined(BARE_METAL)
41#include <thread>
42#endif // !defined(BARE_METAL)
43
44namespace arm_compute
45{
46namespace test
47{
48namespace validation
49{
50TEST_SUITE(NEON)
51TEST_SUITE(UNIT)
52TEST_SUITE(RuntimeContext)
53
54TEST_CASE(Scheduler, framework::DatasetMode::ALL)
55{
56 using namespace arm_compute;
57 // Create a runtime context object
58 RuntimeContext ctx;
59
60 // Check if it's been initialised properly
61 ARM_COMPUTE_EXPECT(ctx.scheduler() != nullptr, framework::LogLevel::ERRORS);
62 ARM_COMPUTE_EXPECT(ctx.asset_manager() == nullptr, framework::LogLevel::ERRORS);
63
64 // Create a CPPScheduler
65 CPPScheduler scheduler;
66 ctx.set_scheduler(&scheduler);
67 // Check if the scheduler has been properly setup
68 ARM_COMPUTE_EXPECT(ctx.scheduler() != nullptr, framework::LogLevel::ERRORS);
69
70 // Create a new activation function
71 NEActivationLayer act_layer(&ctx);
72
73 Tensor src = create_tensor<Tensor>(TensorShape(32, 32), DataType::F32, 1);
74 Tensor dst = create_tensor<Tensor>(TensorShape(32, 32), DataType::F32, 1);
75
76 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LINEAR));
77
78 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
80
81 // Allocate tensors
82 src.allocator()->allocate();
83 dst.allocator()->allocate();
84
85 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
86
87 float min_bound = 0;
88 float max_bound = 0;
89 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(ActivationLayerInfo::ActivationFunction::LINEAR, DataType::F32);
90 std::uniform_real_distribution<> distribution(min_bound, max_bound);
91 library->fill(Accessor(src), distribution, 0);
92
93 // Compute function
94 act_layer.run();
95}
96
97#if !defined(BARE_METAL)
98// This test tries scheduling work concurrently from two independent threads
99TEST_CASE(MultipleThreadedScheduller, framework::DatasetMode::ALL)
100{
101 // Create a runtime context object for thread 1
102 RuntimeContext ctx1;
103
104 // Create a runtime context object for thread 2
105 RuntimeContext ctx2;
106
107 // Create a new activation function
108 NEActivationLayer act_layer_thread0(&ctx1);
109 NEActivationLayer act_layer_thread1(&ctx2);
110
111 const TensorShape tensor_shape(128, 128);
112 Tensor src_t0 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
113 Tensor dst_t0 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
114 Tensor src_t1 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
115 Tensor dst_t1 = create_tensor<Tensor>(tensor_shape, DataType::F32, 1);
116 ActivationLayerInfo activation_info(ActivationLayerInfo::ActivationFunction::LINEAR);
117
118 act_layer_thread0.configure(&src_t0, &dst_t0, activation_info);
119 act_layer_thread1.configure(&src_t1, &dst_t1, activation_info);
120
121 ARM_COMPUTE_EXPECT(src_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
122 ARM_COMPUTE_EXPECT(dst_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
123 ARM_COMPUTE_EXPECT(src_t1.info()->is_resizable(), framework::LogLevel::ERRORS);
124 ARM_COMPUTE_EXPECT(dst_t1.info()->is_resizable(), framework::LogLevel::ERRORS);
125
126 // Allocate tensors
127 src_t0.allocator()->allocate();
128 dst_t0.allocator()->allocate();
129 src_t1.allocator()->allocate();
130 dst_t1.allocator()->allocate();
131
132 ARM_COMPUTE_EXPECT(!src_t0.info()->is_resizable(), framework::LogLevel::ERRORS);
133 ARM_COMPUTE_EXPECT(!src_t1.info()->is_resizable(), framework::LogLevel::ERRORS);
134
135 float min_bound = 0;
136 float max_bound = 0;
137 std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<float>(ActivationLayerInfo::ActivationFunction::LINEAR, DataType::F32);
138 std::uniform_real_distribution<> distribution(min_bound, max_bound);
139 library->fill(Accessor(src_t0), distribution, 0);
140 library->fill(Accessor(src_t1), distribution, 0);
141
142 std::thread neon_thread1([&] { act_layer_thread0.run(); });
143 std::thread neon_thread2([&] { act_layer_thread1.run(); });
144
145 neon_thread1.join();
146 neon_thread2.join();
147
148 Window window;
149 window.use_tensor_dimensions(dst_t0.info()->tensor_shape());
150 Iterator t0_it(&dst_t0, window);
151 Iterator t1_it(&dst_t1, window);
152 execute_window_loop(window, [&](const Coordinates &)
153 {
154 const bool match = (*reinterpret_cast<float *>(t0_it.ptr()) == *reinterpret_cast<float *>(t1_it.ptr()));
155 ARM_COMPUTE_EXPECT(match, framework::LogLevel::ERRORS);
156 },
157 t0_it, t1_it);
158}
159#endif // !defined(BARE_METAL)
160
161TEST_SUITE_END() // RuntimeContext
162TEST_SUITE_END() // UNIT
163TEST_SUITE_END() // NEON
164} // namespace validation
165} // namespace test
166} // namespace arm_compute