1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/runtime/RuntimeContext.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "tests/CL/CLAccessor.h"
27*c217d954SCole Faust #include "tests/framework/Macros.h"
28*c217d954SCole Faust #include "tests/framework/ParametersLibrary.h"
29*c217d954SCole Faust #include "tests/validation/Validation.h"
30*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
31*c217d954SCole Faust #include "arm_compute/runtime/CL/functions/CLPixelWiseMultiplication.h"
32*c217d954SCole Faust #include "tests/validation/reference/ActivationLayer.h"
33*c217d954SCole Faust #include "tests/validation/reference/PixelWiseMultiplication.h"
34*c217d954SCole Faust #include <thread>
35*c217d954SCole Faust
36*c217d954SCole Faust namespace arm_compute
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace test
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace validation
41*c217d954SCole Faust {
42*c217d954SCole Faust TEST_SUITE(CL)
TEST_SUITE(UNIT)43*c217d954SCole Faust TEST_SUITE(UNIT)
44*c217d954SCole Faust TEST_SUITE(RuntimeContext)
45*c217d954SCole Faust // This test tries scheduling work concurrently from two independent threads
46*c217d954SCole Faust TEST_CASE(MultipleThreadedScheduller, framework::DatasetMode::ALL)
47*c217d954SCole Faust {
48*c217d954SCole Faust constexpr auto num_threads(16u);
49*c217d954SCole Faust std::array<CLActivationLayer, num_threads> func{};
50*c217d954SCole Faust std::array<CLPixelWiseMultiplication, num_threads> pmul{};
51*c217d954SCole Faust std::array<CLTensor, num_threads> s0{};
52*c217d954SCole Faust std::array<CLTensor, num_threads> s1{};
53*c217d954SCole Faust
54*c217d954SCole Faust std::array<CLTensor, num_threads> st{};
55*c217d954SCole Faust std::array<CLTensor, num_threads> dt{};
56*c217d954SCole Faust
57*c217d954SCole Faust const TensorShape tensor_shape(128u, 4u, 5u);
58*c217d954SCole Faust const ActivationLayerInfo ainfo(ActivationLayerInfo::ActivationFunction::LOGISTIC, 0.5f, 1.f);
59*c217d954SCole Faust std::array<std::thread, num_threads> threads;
60*c217d954SCole Faust auto ctx = parameters->get_ctx<CLTensor>();
61*c217d954SCole Faust
62*c217d954SCole Faust for(auto i = 0u; i < num_threads; ++i)
63*c217d954SCole Faust {
64*c217d954SCole Faust s0[i] = create_tensor<CLTensor>(tensor_shape, DataType::F32, 1);
65*c217d954SCole Faust s1[i] = create_tensor<CLTensor>(tensor_shape, DataType::F32, 1);
66*c217d954SCole Faust st[i] = create_tensor<CLTensor>(tensor_shape, DataType::F32, 1);
67*c217d954SCole Faust dt[i] = create_tensor<CLTensor>(tensor_shape, DataType::F32, 1);
68*c217d954SCole Faust func[i] = CLActivationLayer(ctx);
69*c217d954SCole Faust pmul[i] = CLPixelWiseMultiplication();
70*c217d954SCole Faust threads[i] =
71*c217d954SCole Faust std::thread([&,i]
72*c217d954SCole Faust {
73*c217d954SCole Faust auto &s = st[i];
74*c217d954SCole Faust auto &t = dt[i];
75*c217d954SCole Faust auto &p0 = s0[i];
76*c217d954SCole Faust auto &p1 = s1[i];
77*c217d954SCole Faust pmul[i].configure(&p0, &p1, &s, 1.f, ConvertPolicy::WRAP, RoundingPolicy::TO_NEAREST_UP);
78*c217d954SCole Faust func[i].configure(&s, &t, ainfo);
79*c217d954SCole Faust s.allocator()->allocate();
80*c217d954SCole Faust t.allocator()->allocate();
81*c217d954SCole Faust p0.allocator()->allocate();
82*c217d954SCole Faust p1.allocator()->allocate();
83*c217d954SCole Faust library->fill_tensor_uniform(CLAccessor(p0), 0, -1.f, 1.f);
84*c217d954SCole Faust library->fill_tensor_uniform(CLAccessor(p1), 0, -1.f, 1.f);
85*c217d954SCole Faust pmul[i].run();
86*c217d954SCole Faust func[i].run();
87*c217d954SCole Faust });
88*c217d954SCole Faust }
89*c217d954SCole Faust
90*c217d954SCole Faust for(auto &t : threads)
91*c217d954SCole Faust {
92*c217d954SCole Faust t.join();
93*c217d954SCole Faust }
94*c217d954SCole Faust
95*c217d954SCole Faust SimpleTensor<float> rs{ tensor_shape, DataType::F32, 1 };
96*c217d954SCole Faust SimpleTensor<float> ra{ tensor_shape, DataType::F32, 1 };
97*c217d954SCole Faust SimpleTensor<float> rb{ tensor_shape, DataType::F32, 1 };
98*c217d954SCole Faust library->fill_tensor_uniform(ra, 0, -1.f, 1.f);
99*c217d954SCole Faust library->fill_tensor_uniform(rb, 0, -1.f, 1.f);
100*c217d954SCole Faust const auto mul = reference::pixel_wise_multiplication<float, float, float>(ra, rb, 1.f, ConvertPolicy::WRAP, RoundingPolicy::TO_NEAREST_UP, DataType::F32);
101*c217d954SCole Faust const auto golden = reference::activation_layer<float>(mul, ainfo);
102*c217d954SCole Faust for(auto &d : dt)
103*c217d954SCole Faust {
104*c217d954SCole Faust validate(CLAccessor(d), golden);
105*c217d954SCole Faust }
106*c217d954SCole Faust }
107*c217d954SCole Faust
108*c217d954SCole Faust TEST_SUITE_END() // MultipleThreadedScheduller
109*c217d954SCole Faust TEST_SUITE_END() // UNIT
110*c217d954SCole Faust TEST_SUITE_END() // CL
111*c217d954SCole Faust } // namespace validation
112*c217d954SCole Faust } // namespace test
113*c217d954SCole Faust } // namespace arm_compute
114