1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2016-2021 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
25*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEFunctions.h"
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/Types.h"
28*c217d954SCole Faust #include "utils/Utils.h"
29*c217d954SCole Faust
30*c217d954SCole Faust #include <cstring>
31*c217d954SCole Faust #include <iostream>
32*c217d954SCole Faust
33*c217d954SCole Faust using namespace arm_compute;
34*c217d954SCole Faust using namespace utils;
35*c217d954SCole Faust
36*c217d954SCole Faust class NEONCopyObjectsExample : public Example
37*c217d954SCole Faust {
38*c217d954SCole Faust public:
do_setup(int argc,char ** argv)39*c217d954SCole Faust bool do_setup(int argc, char **argv) override
40*c217d954SCole Faust {
41*c217d954SCole Faust ARM_COMPUTE_UNUSED(argc);
42*c217d954SCole Faust ARM_COMPUTE_UNUSED(argv);
43*c217d954SCole Faust
44*c217d954SCole Faust /** [Copy objects example] */
45*c217d954SCole Faust constexpr unsigned int width = 4;
46*c217d954SCole Faust constexpr unsigned int height = 3;
47*c217d954SCole Faust constexpr unsigned int batch = 2;
48*c217d954SCole Faust
49*c217d954SCole Faust src_data = new float[width * height * batch];
50*c217d954SCole Faust dst_data = new float[width * height * batch];
51*c217d954SCole Faust
52*c217d954SCole Faust // Fill src_data with pseudo(meaningless) values:
53*c217d954SCole Faust for(unsigned int b = 0; b < batch; b++)
54*c217d954SCole Faust {
55*c217d954SCole Faust for(unsigned int h = 0; h < height; h++)
56*c217d954SCole Faust {
57*c217d954SCole Faust for(unsigned int w = 0; w < width; w++)
58*c217d954SCole Faust {
59*c217d954SCole Faust src_data[b * (width * height) + h * width + w] = static_cast<float>(100 * b + 10 * h + w);
60*c217d954SCole Faust }
61*c217d954SCole Faust }
62*c217d954SCole Faust }
63*c217d954SCole Faust
64*c217d954SCole Faust // Initialize the tensors dimensions and type:
65*c217d954SCole Faust const TensorShape shape(width, height, batch);
66*c217d954SCole Faust input.allocator()->init(TensorInfo(shape, 1, DataType::F32));
67*c217d954SCole Faust output.allocator()->init(TensorInfo(shape, 1, DataType::F32));
68*c217d954SCole Faust
69*c217d954SCole Faust // Configure softmax:
70*c217d954SCole Faust softmax.configure(&input, &output);
71*c217d954SCole Faust
72*c217d954SCole Faust // Allocate the input / output tensors:
73*c217d954SCole Faust input.allocator()->allocate();
74*c217d954SCole Faust output.allocator()->allocate();
75*c217d954SCole Faust
76*c217d954SCole Faust // Fill the input tensor:
77*c217d954SCole Faust // Simplest way: create an iterator to iterate through each element of the input tensor:
78*c217d954SCole Faust Window input_window;
79*c217d954SCole Faust input_window.use_tensor_dimensions(input.info()->tensor_shape());
80*c217d954SCole Faust std::cout << " Dimensions of the input's iterator:\n";
81*c217d954SCole Faust std::cout << " X = [start=" << input_window.x().start() << ", end=" << input_window.x().end() << ", step=" << input_window.x().step() << "]\n";
82*c217d954SCole Faust std::cout << " Y = [start=" << input_window.y().start() << ", end=" << input_window.y().end() << ", step=" << input_window.y().step() << "]\n";
83*c217d954SCole Faust std::cout << " Z = [start=" << input_window.z().start() << ", end=" << input_window.z().end() << ", step=" << input_window.z().step() << "]\n";
84*c217d954SCole Faust
85*c217d954SCole Faust // Create an iterator:
86*c217d954SCole Faust Iterator input_it(&input, input_window);
87*c217d954SCole Faust
88*c217d954SCole Faust // Iterate through the elements of src_data and copy them one by one to the input tensor:
89*c217d954SCole Faust // This is equivalent to:
90*c217d954SCole Faust // for( unsigned int z = 0; z < batch; ++z)
91*c217d954SCole Faust // {
92*c217d954SCole Faust // for( unsigned int y = 0; y < height; ++y)
93*c217d954SCole Faust // {
94*c217d954SCole Faust // for( unsigned int x = 0; x < width; ++x)
95*c217d954SCole Faust // {
96*c217d954SCole Faust // *reinterpret_cast<float*>( input.buffer() + input.info()->offset_element_in_bytes(Coordinates(x,y,z))) = src_data[ z * (width*height) + y * width + x];
97*c217d954SCole Faust // }
98*c217d954SCole Faust // }
99*c217d954SCole Faust // }
100*c217d954SCole Faust // Except it works for an arbitrary number of dimensions
101*c217d954SCole Faust execute_window_loop(input_window, [&](const Coordinates & id)
102*c217d954SCole Faust {
103*c217d954SCole Faust std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
104*c217d954SCole Faust *reinterpret_cast<float *>(input_it.ptr()) = src_data[id.z() * (width * height) + id.y() * width + id.x()];
105*c217d954SCole Faust },
106*c217d954SCole Faust input_it);
107*c217d954SCole Faust
108*c217d954SCole Faust // More efficient way: create an iterator to iterate through each row (instead of each element) of the output tensor:
109*c217d954SCole Faust Window output_window;
110*c217d954SCole Faust output_window.use_tensor_dimensions(output.info()->tensor_shape(), /* first_dimension =*/Window::DimY); // Iterate through the rows (not each element)
111*c217d954SCole Faust std::cout << " Dimensions of the output's iterator:\n";
112*c217d954SCole Faust std::cout << " X = [start=" << output_window.x().start() << ", end=" << output_window.x().end() << ", step=" << output_window.x().step() << "]\n";
113*c217d954SCole Faust std::cout << " Y = [start=" << output_window.y().start() << ", end=" << output_window.y().end() << ", step=" << output_window.y().step() << "]\n";
114*c217d954SCole Faust std::cout << " Z = [start=" << output_window.z().start() << ", end=" << output_window.z().end() << ", step=" << output_window.z().step() << "]\n";
115*c217d954SCole Faust
116*c217d954SCole Faust // Create an iterator:
117*c217d954SCole Faust Iterator output_it(&output, output_window);
118*c217d954SCole Faust
119*c217d954SCole Faust // Iterate through the rows of the output tensor and copy them to dst_data:
120*c217d954SCole Faust // This is equivalent to:
121*c217d954SCole Faust // for( unsigned int z = 0; z < batch; ++z)
122*c217d954SCole Faust // {
123*c217d954SCole Faust // for( unsigned int y = 0; y < height; ++y)
124*c217d954SCole Faust // {
125*c217d954SCole Faust // memcpy( dst_data + z * (width*height) + y * width, input.buffer() + input.info()->offset_element_in_bytes(Coordinates(0,y,z)), width * sizeof(float));
126*c217d954SCole Faust // }
127*c217d954SCole Faust // }
128*c217d954SCole Faust // Except it works for an arbitrary number of dimensions
129*c217d954SCole Faust execute_window_loop(output_window, [&](const Coordinates & id)
130*c217d954SCole Faust {
131*c217d954SCole Faust std::cout << "Copying one row starting from [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
132*c217d954SCole Faust // Copy one whole row:
133*c217d954SCole Faust memcpy(dst_data + id.z() * (width * height) + id.y() * width, output_it.ptr(), width * sizeof(float));
134*c217d954SCole Faust },
135*c217d954SCole Faust output_it);
136*c217d954SCole Faust
137*c217d954SCole Faust /** [Copy objects example] */
138*c217d954SCole Faust
139*c217d954SCole Faust return true;
140*c217d954SCole Faust }
do_run()141*c217d954SCole Faust void do_run() override
142*c217d954SCole Faust {
143*c217d954SCole Faust // Run softmax:
144*c217d954SCole Faust softmax.run();
145*c217d954SCole Faust }
do_teardown()146*c217d954SCole Faust void do_teardown() override
147*c217d954SCole Faust {
148*c217d954SCole Faust delete[] src_data;
149*c217d954SCole Faust delete[] dst_data;
150*c217d954SCole Faust }
151*c217d954SCole Faust
152*c217d954SCole Faust private:
153*c217d954SCole Faust Tensor input{}, output{};
154*c217d954SCole Faust float *src_data{};
155*c217d954SCole Faust float *dst_data{};
156*c217d954SCole Faust NESoftmaxLayer softmax{};
157*c217d954SCole Faust };
158*c217d954SCole Faust /** Main program for the copy objects test
159*c217d954SCole Faust *
160*c217d954SCole Faust * @param[in] argc Number of arguments
161*c217d954SCole Faust * @param[in] argv Arguments
162*c217d954SCole Faust */
main(int argc,char ** argv)163*c217d954SCole Faust int main(int argc, char **argv)
164*c217d954SCole Faust {
165*c217d954SCole Faust return utils::run_example<NEONCopyObjectsExample>(argc, argv);
166*c217d954SCole Faust }
167