Compute Library
 21.02
neon_copy_objects.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 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 
26 
27 #include "arm_compute/core/Types.h"
28 #include "utils/Utils.h"
29 
30 #include <cstring>
31 #include <iostream>
32 
33 using namespace arm_compute;
34 using namespace utils;
35 
36 class NEONCopyObjectsExample : public Example
37 {
38 public:
39  bool do_setup(int argc, char **argv) override
40  {
41  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 
52  // Fill src_data with dummy values:
53  for(unsigned int b = 0; b < batch; b++)
54  {
55  for(unsigned int h = 0; h < height; h++)
56  {
57  for(unsigned int w = 0; w < width; w++)
58  {
59  src_data[b * (width * height) + h * width + w] = static_cast<float>(100 * b + 10 * h + w);
60  }
61  }
62  }
63 
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";
81  std::cout << " X = [start=" << input_window.x().start() << ", end=" << input_window.x().end() << ", step=" << input_window.x().step() << "]\n";
82  std::cout << " Y = [start=" << input_window.y().start() << ", end=" << input_window.y().end() << ", step=" << input_window.y().step() << "]\n";
83  std::cout << " Z = [start=" << input_window.z().start() << ", end=" << input_window.z().end() << ", step=" << input_window.z().step() << "]\n";
84 
85  // Create an iterator:
86  Iterator input_it(&input, input_window);
87 
88  // Iterate through the elements of src_data and copy them one by one to the input tensor:
89  // This is equivalent to:
90  // for( unsigned int z = 0; z < batch; ++z)
91  // {
92  // for( unsigned int y = 0; y < height; ++y)
93  // {
94  // for( unsigned int x = 0; x < width; ++x)
95  // {
96  // *reinterpret_cast<float*>( input.buffer() + input.info()->offset_element_in_bytes(Coordinates(x,y,z))) = src_data[ z * (width*height) + y * width + x];
97  // }
98  // }
99  // }
100  // Except it works for an arbitrary number of dimensions
101  execute_window_loop(input_window, [&](const Coordinates & id)
102  {
103  std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
104  *reinterpret_cast<float *>(input_it.ptr()) = src_data[id.z() * (width * height) + id.y() * width + id.x()];
105  },
106  input_it);
107 
108  // More efficient way: create an iterator to iterate through each row (instead of each element) of the output tensor:
109  Window output_window;
110  output_window.use_tensor_dimensions(output.info()->tensor_shape(), /* first_dimension =*/Window::DimY); // Iterate through the rows (not each element)
111  std::cout << " Dimensions of the output's iterator:\n";
112  std::cout << " X = [start=" << output_window.x().start() << ", end=" << output_window.x().end() << ", step=" << output_window.x().step() << "]\n";
113  std::cout << " Y = [start=" << output_window.y().start() << ", end=" << output_window.y().end() << ", step=" << output_window.y().step() << "]\n";
114  std::cout << " Z = [start=" << output_window.z().start() << ", end=" << output_window.z().end() << ", step=" << output_window.z().step() << "]\n";
115 
116  // Create an iterator:
117  Iterator output_it(&output, output_window);
118 
119  // Iterate through the rows of the output tensor and copy them to dst_data:
120  // This is equivalent to:
121  // for( unsigned int z = 0; z < batch; ++z)
122  // {
123  // for( unsigned int y = 0; y < height; ++y)
124  // {
125  // memcpy( dst_data + z * (width*height) + y * width, input.buffer() + input.info()->offset_element_in_bytes(Coordinates(0,y,z)), width * sizeof(float));
126  // }
127  // }
128  // Except it works for an arbitrary number of dimensions
129  execute_window_loop(output_window, [&](const Coordinates & id)
130  {
131  std::cout << "Copying one row starting from [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
132  // Copy one whole row:
133  memcpy(dst_data + id.z() * (width * height) + id.y() * width, output_it.ptr(), width * sizeof(float));
134  },
135  output_it);
136 
137  /** [Copy objects example] */
138 
139  return true;
140  }
141  void do_run() override
142  {
143  // Run Neon softmax:
144  softmax.run();
145  }
146  void do_teardown() override
147  {
148  delete[] src_data;
149  delete[] dst_data;
150  }
151 
152 private:
153  Tensor input{}, output{};
154  float *src_data{};
155  float *dst_data{};
156  NESoftmaxLayer softmax{};
157 };
158 /** Main program for the copy objects test
159  *
160  * @param[in] argc Number of arguments
161  * @param[in] argv Arguments
162  */
163 int main(int argc, char **argv)
164 {
165  return utils::run_example<NEONCopyObjectsExample>(argc, argv);
166 }
SimpleTensor< float > w
Definition: DFT.cpp:156
Shape of a tensor.
Definition: TensorShape.h:39
SimpleTensor< float > b
Definition: DFT.cpp:157
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
1 channel, 1 F32 per channel
constexpr const Dimension & z() const
Alias to access the third dimension of the window.
Definition: Window.h:163
Includes all the Neon functions at once.
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
Copyright (c) 2017-2021 Arm Limited.
Basic function to compute a SoftmaxLayer and a Log SoftmaxLayer.
int main(int argc, char **argv)
Main program for the copy objects test.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Coordinates of an item.
Definition: Coordinates.h:37
Abstract Example class.
Definition: Utils.h:78
Basic implementation of the tensor interface.
Definition: Tensor.h:37
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145