• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 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 #include "src/core/CL/kernels/CLCopyKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/Utils.h"
30 #include "src/core/helpers/AutoConfiguration.h"
31 #include "src/core/helpers/WindowHelpers.h"
32 #include "support/StringSupport.h"
33 
34 namespace arm_compute
35 {
36 namespace
37 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,Window * output_window=nullptr)38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, Window *output_window = nullptr)
39 {
40     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
41 
42     // Validate output if initialized
43     if(output->total_size() != 0)
44     {
45         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
46         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
47         if(output_window == nullptr)
48         {
49             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
50         }
51         else
52         {
53             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output_window->shape());
54         }
55     }
56 
57     return Status{};
58 }
59 
60 } // namespace
61 
CLCopyKernel()62 CLCopyKernel::CLCopyKernel()
63     : _input(nullptr), _output(nullptr), _output_window(), _has_output_window(false)
64 {
65 }
66 
configure(const ICLTensor * input,ICLTensor * output,Window * output_window)67 void CLCopyKernel::configure(const ICLTensor *input, ICLTensor *output, Window *output_window)
68 {
69     configure(CLKernelLibrary::get().get_compile_context(), input, output, output_window);
70 }
71 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,Window * output_window)72 void CLCopyKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, Window *output_window)
73 {
74     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
75     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), output_window));
76 
77     auto padding_info = get_padding_info({ input, output });
78 
79     _input  = input;
80     _output = output;
81 
82     // Create kernel
83     CLBuildOptions build_opts;
84     build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
85 
86     // Output auto inizialitation if not yet initialized
87     auto_init_if_empty(*(output->info()), *(input->info()));
88 
89     // Configure window
90     const unsigned int vec_size_x = adjust_vec_size(16 / input->info()->element_size(), input->info()->dimension(0));
91 
92     const Window win_config = calculate_max_window(*(input->info()), Steps(vec_size_x));
93 
94     if(output_window != nullptr)
95     {
96         _has_output_window             = true;
97         _output_window                 = Window(*output_window);
98         const int  width_x             = output_window->num_iterations(0);
99         const int  vec_size_x_leftover = width_x % vec_size_x;
100         const bool multi_access_x      = width_x >= static_cast<int32_t>(vec_size_x);
101 
102         if(multi_access_x)
103         {
104             _output_window.set(Window::DimX, Window::Dimension(output_window->x().start(), ceil_to_multiple(output_window->x().end(), vec_size_x), vec_size_x));
105         }
106 
107         build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover));
108     }
109     else
110     {
111         const int width_x             = input->info()->tensor_shape().x();
112         const int vec_size_x_leftover = width_x % vec_size_x;
113 
114         build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftover));
115     }
116 
117     build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
118 
119     // Build kernel
120     _kernel = create_kernel(compile_context, "copy_tensor", build_opts.options());
121 
122     // Validate and set the window
123     ICLKernel::configure_internal(win_config);
124 
125     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
126 }
127 
validate(const arm_compute::ITensorInfo * input,const arm_compute::ITensorInfo * output,Window * output_window)128 Status CLCopyKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output, Window *output_window)
129 {
130     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, output_window));
131 
132     return Status{};
133 }
134 
run(const Window & window,cl::CommandQueue & queue)135 void CLCopyKernel::run(const Window &window, cl::CommandQueue &queue)
136 {
137     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
139 
140     Window slice;
141 
142     if(_has_output_window)
143     {
144         slice            = window.first_slice_window_3D();
145         Window out_slice = _output_window.first_slice_window_3D();
146         do
147         {
148             unsigned int idx = 0;
149             add_3D_tensor_argument(idx, _input, slice);
150             add_3D_tensor_argument(idx, _output, out_slice);
151             enqueue(queue, *this, slice, lws_hint());
152         }
153         while(window.slide_window_slice_3D(slice) && _output_window.slide_window_slice_3D(out_slice));
154     }
155     else
156     {
157         Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
158         slice            = collapsed.first_slice_window_3D();
159         do
160         {
161             unsigned int idx = 0;
162             add_3D_tensor_argument(idx, _input, slice);
163             add_3D_tensor_argument(idx, _output, slice);
164             enqueue(queue, *this, slice, lws_hint());
165         }
166         while(collapsed.slide_window_slice_3D(slice));
167     }
168 }
169 } // namespace arm_compute
170