Compute Library
 21.02
CpuCopyKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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  */
25 
26 #include "arm_compute/core/Error.h"
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
40 namespace kernels
41 {
42 namespace
43 {
44 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PaddingList &padding = PaddingList())
45 {
48  ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 4);
49 
50  // Validate destination if initialized
51  if(dst->total_size() != 0)
52  {
55  }
56 
57  return Status{};
58 }
59 
60 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
61 {
62  // Destination auto inizialitation if not yet initialized
63  auto_init_if_empty(*dst, *src);
64  return std::make_pair(Status{}, calculate_max_window(*dst));
65 }
66 
67 std::pair<Status, Window> validate_and_configure_window_with_padding(const ITensorInfo *src, ITensorInfo *dst, const PaddingList &padding)
68 {
69  const TensorShape src_shape = src->tensor_shape();
70  const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(src_shape, padding);
71  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(padded_shape));
72  // Configure window
73  const Window win = calculate_max_window(*dst, dst->dimension(0));
74  return std::make_pair(Status{}, win);
75 }
76 
77 } // namespace
78 
79 void CpuCopyKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const PaddingList &padding)
80 {
83 
84  _padding = padding;
85 
86  std::pair<Status, Window> win_config;
87  if(padding.empty())
88  {
89  win_config = validate_and_configure_window(src, dst);
90  }
91  else
92  {
93  win_config = validate_and_configure_window_with_padding(src, dst, padding);
94  }
95 
96  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
97  ICpuKernel::configure(win_config.second);
98 }
99 
101 {
103 
104  if(padding.empty())
105  {
106  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get()).first);
107  }
108  else
109  {
110  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_with_padding(src->clone().get(), dst->clone().get(), padding).first);
111  }
112 
113  return Status{};
114 }
115 
117 {
118  ARM_COMPUTE_UNUSED(info);
121 
122  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
123  auto dst = tensors.get_tensor(TensorType::ACL_DST);
124 
125  if(_padding.empty())
126  {
127  Window dst_window{ window };
128  dst_window.set(Window::DimX, Window::Dimension(dst_window.x().start(), dst_window.x().end(), src->info()->dimension(0)));
129  Window out_slice = dst_window.first_slice_window_1D();
130  do
131  {
132  Iterator src_it(src, out_slice);
133  Iterator dst_it(dst, out_slice);
134 
135  execute_window_loop(out_slice, [&](const Coordinates &)
136  {
137  memcpy(dst_it.ptr(), src_it.ptr(), dst->info()->dimension(0) * dst->info()->element_size());
138  },
139  src_it, dst_it);
140  }
141  while(dst_window.slide_window_slice_1D(out_slice));
142  }
143  else
144  {
145  Window src_window{ window };
146  src_window.set(Window::DimX, Window::Dimension(0, window.x().end() - _padding[0].first, src->info()->dimension(0)));
147 
148  Iterator src_it(src, src_window);
149  Iterator dst_it(dst, window);
150  const size_t row_size_in_bytes = src->info()->dimension(0) * src->info()->element_size();
151  execute_window_loop(window, [&](const Coordinates &)
152  {
153  auto dst_ptr = dst_it.ptr() + _padding[0].first * dst->info()->element_size();
154  std::memcpy(dst_ptr, src_it.ptr(), row_size_in_bytes);
155  },
156  src_it, dst_it);
157  }
158 }
159 
160 const char *CpuCopyKernel::name() const
161 {
162  return "CpuCopyKernel";
163 }
164 } // namespace kernels
165 } // namespace cpu
166 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:481
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PaddingList &padding=PaddingList())
Static function to check if given info will lead to a valid configuration of CpuCopyKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:40
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:50
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
Tensor packing service.
Definition: ITensorPack.h:37
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
void configure(const ITensorInfo *src, ITensorInfo *dst, const PaddingList &padding=PaddingList())
Configure kernel for a given list of arguments.
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
Describe a multidimensional execution window.
Definition: Window.h:39
const char * name() const override
Name of the kernel.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:275
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145