blob: 6e9ebcaad5f65644ba41f466808dc1e5a66e03de [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/runtime/NEON/NEFunctions.h"
27
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "utils/Utils.h"
29
30#include <cstring>
31#include <iostream>
32
33using namespace arm_compute;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000034using namespace utils;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000036class NEONCopyObjectsExample : public Example
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000038public:
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010039 bool do_setup(int argc, char **argv) override
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041 ARM_COMPUTE_UNUSED(argc);
42 ARM_COMPUTE_UNUSED(argv);
43
44 /** [Copy objects example] */
45 constexpr unsigned int width = 4;
46 constexpr unsigned int height = 3;
47 constexpr unsigned int batch = 2;
48
49 src_data = new float[width * height * batch];
50 dst_data = new float[width * height * batch];
51
ramelg01b2eba7f2021-12-23 08:32:08 +000052 // Fill src_data with pseudo(meaningless) values:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 for (unsigned int b = 0; b < batch; b++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 for (unsigned int h = 0; h < height; h++)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 for (unsigned int w = 0; w < width; w++)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000058 {
59 src_data[b * (width * height) + h * width + w] = static_cast<float>(100 * b + 10 * h + w);
60 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 }
62 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000063
64 // Initialize the tensors dimensions and type:
65 const TensorShape shape(width, height, batch);
66 input.allocator()->init(TensorInfo(shape, 1, DataType::F32));
67 output.allocator()->init(TensorInfo(shape, 1, DataType::F32));
68
69 // Configure softmax:
70 softmax.configure(&input, &output);
71
72 // Allocate the input / output tensors:
73 input.allocator()->allocate();
74 output.allocator()->allocate();
75
76 // Fill the input tensor:
77 // Simplest way: create an iterator to iterate through each element of the input tensor:
78 Window input_window;
79 input_window.use_tensor_dimensions(input.info()->tensor_shape());
80 std::cout << " Dimensions of the input's iterator:\n";
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 std::cout << " X = [start=" << input_window.x().start() << ", end=" << input_window.x().end()
82 << ", step=" << input_window.x().step() << "]\n";
83 std::cout << " Y = [start=" << input_window.y().start() << ", end=" << input_window.y().end()
84 << ", step=" << input_window.y().step() << "]\n";
85 std::cout << " Z = [start=" << input_window.z().start() << ", end=" << input_window.z().end()
86 << ", step=" << input_window.z().step() << "]\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000087
88 // Create an iterator:
89 Iterator input_it(&input, input_window);
90
91 // Iterate through the elements of src_data and copy them one by one to the input tensor:
92 // This is equivalent to:
93 // for( unsigned int z = 0; z < batch; ++z)
94 // {
95 // for( unsigned int y = 0; y < height; ++y)
96 // {
97 // for( unsigned int x = 0; x < width; ++x)
98 // {
99 // *reinterpret_cast<float*>( input.buffer() + input.info()->offset_element_in_bytes(Coordinates(x,y,z))) = src_data[ z * (width*height) + y * width + x];
100 // }
101 // }
102 // }
103 // Except it works for an arbitrary number of dimensions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 execute_window_loop(
105 input_window,
106 [&](const Coordinates &id)
107 {
108 std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
109 *reinterpret_cast<float *>(input_it.ptr()) =
110 src_data[id.z() * (width * height) + id.y() * width + id.x()];
111 },
112 input_it);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000113
114 // More efficient way: create an iterator to iterate through each row (instead of each element) of the output tensor:
115 Window output_window;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 output_window.use_tensor_dimensions(
117 output.info()->tensor_shape(),
118 /* first_dimension =*/Window::DimY); // Iterate through the rows (not each element)
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000119 std::cout << " Dimensions of the output's iterator:\n";
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 std::cout << " X = [start=" << output_window.x().start() << ", end=" << output_window.x().end()
121 << ", step=" << output_window.x().step() << "]\n";
122 std::cout << " Y = [start=" << output_window.y().start() << ", end=" << output_window.y().end()
123 << ", step=" << output_window.y().step() << "]\n";
124 std::cout << " Z = [start=" << output_window.z().start() << ", end=" << output_window.z().end()
125 << ", step=" << output_window.z().step() << "]\n";
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000126
127 // Create an iterator:
128 Iterator output_it(&output, output_window);
129
130 // Iterate through the rows of the output tensor and copy them to dst_data:
131 // This is equivalent to:
132 // for( unsigned int z = 0; z < batch; ++z)
133 // {
134 // for( unsigned int y = 0; y < height; ++y)
135 // {
136 // memcpy( dst_data + z * (width*height) + y * width, input.buffer() + input.info()->offset_element_in_bytes(Coordinates(0,y,z)), width * sizeof(float));
137 // }
138 // }
139 // Except it works for an arbitrary number of dimensions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 execute_window_loop(
141 output_window,
142 [&](const Coordinates &id)
143 {
144 std::cout << "Copying one row starting from [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
145 // Copy one whole row:
146 memcpy(dst_data + id.z() * (width * height) + id.y() * width, output_it.ptr(), width * sizeof(float));
147 },
148 output_it);
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000149
150 /** [Copy objects example] */
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100151
152 return true;
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000153 }
154 void do_run() override
155 {
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000156 // Run softmax:
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000157 softmax.run();
158 }
159 void do_teardown() override
160 {
161 delete[] src_data;
162 delete[] dst_data;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
164
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000165private:
166 Tensor input{}, output{};
167 float *src_data{};
168 float *dst_data{};
169 NESoftmaxLayer softmax{};
170};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171/** Main program for the copy objects test
172 *
173 * @param[in] argc Number of arguments
174 * @param[in] argv Arguments
175 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000176int main(int argc, char **argv)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000178 return utils::run_example<NEONCopyObjectsExample>(argc, argv);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179}