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