blob: 607f5cedbffef9e3b44895a73ce2a2c102e6e17a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
giuros01edc21e42018-11-16 14:45:31 +00002 * Copyright (c) 2016-2019 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#include "arm_compute/core/ITensor.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Window.h"
29
30#include <cstring>
31#include <limits>
32
33using namespace arm_compute;
34
35void ITensor::copy_from(const ITensor &src)
36{
37 if(&src == this)
38 {
39 return;
40 }
41
42 const ITensorInfo *src_info = src.info();
43 ITensorInfo *dst_info = this->info();
44
45 ARM_COMPUTE_ERROR_ON(src_info->num_dimensions() > dst_info->num_dimensions());
46 ARM_COMPUTE_ERROR_ON(src_info->num_channels() != dst_info->num_channels());
47 ARM_COMPUTE_ERROR_ON(src_info->element_size() != dst_info->element_size());
48
49 for(size_t d = 0; d < src_info->num_dimensions(); d++)
50 {
51 ARM_COMPUTE_ERROR_ON(src_info->dimension(d) > dst_info->dimension(d));
52 }
53
54 // Copy information about valid region
55 dst_info->set_valid_region(src_info->valid_region());
56
57 Window win_src;
SiCong Li86b53332017-08-23 11:02:43 +010058 win_src.use_tensor_dimensions(src_info->tensor_shape(), Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 Window win_dst;
SiCong Li86b53332017-08-23 11:02:43 +010060 win_dst.use_tensor_dimensions(dst_info->tensor_shape(), Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061
62 Iterator src_it(&src, win_src);
63 Iterator dst_it(this, win_dst);
64
Anthony Barbier5376c4c2018-07-13 09:48:58 +010065 const size_t line_size = src_info->element_size() * src_info->dimension(0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
Michalis Spyroua4f378d2019-04-26 14:54:54 +010067 execute_window_loop(win_src, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 {
69 memcpy(dst_it.ptr(), src_it.ptr(), line_size);
70 },
71 src_it, dst_it);
72}
73
giuros01edc21e42018-11-16 14:45:31 +000074#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075void ITensor::print(std::ostream &s, IOFormatInfo io_fmt) const
76{
77 ARM_COMPUTE_ERROR_ON(this->buffer() == nullptr);
78
Ioan-Cristian Szabo57120912017-12-12 09:56:21 +000079 const DataType dt = this->info()->data_type();
80 const size_t slices2D = this->info()->tensor_shape().total_size_upper(2);
81 const Strides strides = this->info()->strides_in_bytes();
82 const PaddingSize padding = this->info()->padding();
83 const size_t num_channels = this->info()->num_channels();
Michalis Spyrou53860dd2019-07-01 14:20:56 +010084 std::ios stream_status(nullptr);
85 stream_status.copyfmt(s);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 // Set precision
88 if(is_data_type_float(dt) && (io_fmt.precision_type != IOFormatInfo::PrecisionType::Default))
89 {
90 int precision = io_fmt.precision;
91 if(io_fmt.precision_type == IOFormatInfo::PrecisionType::Full)
92 {
93 precision = std::numeric_limits<float>().max_digits10;
94 }
95 s.precision(precision);
96 }
97
98 // Define region to print
99 size_t print_width = 0;
100 size_t print_height = 0;
101 int start_offset = 0;
102 switch(io_fmt.print_region)
103 {
104 case IOFormatInfo::PrintRegion::NoPadding:
105 print_width = this->info()->dimension(0);
106 print_height = this->info()->dimension(1);
107 start_offset = this->info()->offset_first_element_in_bytes();
108 break;
109 case IOFormatInfo::PrintRegion::ValidRegion:
110 print_width = this->info()->valid_region().shape.x();
111 print_height = this->info()->valid_region().shape.y();
112 start_offset = this->info()->offset_element_in_bytes(Coordinates(this->info()->valid_region().anchor.x(),
113 this->info()->valid_region().anchor.y()));
114 break;
115 case IOFormatInfo::PrintRegion::Full:
116 print_width = padding.left + this->info()->dimension(0) + padding.right;
117 print_height = padding.top + this->info()->dimension(1) + padding.bottom;
118 start_offset = static_cast<int>(this->info()->offset_first_element_in_bytes()) - padding.top * strides[1] - padding.left * strides[0];
119 break;
120 default:
121 break;
122 }
123
Ioan-Cristian Szabo57120912017-12-12 09:56:21 +0000124 print_width = print_width * num_channels;
125
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 // Set pointer to start
127 const uint8_t *ptr = this->buffer() + start_offset;
128
129 // Start printing
130 for(size_t i = 0; i < slices2D; ++i)
131 {
132 // Find max_width of elements in slice to align columns
133 int max_element_width = 0;
134 if(io_fmt.align_columns)
135 {
136 size_t offset = i * strides[2];
137 for(size_t h = 0; h < print_height; ++h)
138 {
139 max_element_width = std::max<int>(max_element_width, max_consecutive_elements_display_width(s, dt, ptr + offset, print_width));
140 offset += strides[1];
141 }
142 }
143
144 // Print slice
145 {
146 size_t offset = i * strides[2];
147 for(size_t h = 0; h < print_height; ++h)
148 {
149 print_consecutive_elements(s, dt, ptr + offset, print_width, max_element_width, io_fmt.element_delim);
150 offset += strides[1];
151 s << io_fmt.row_delim;
152 }
153 s << io_fmt.row_delim;
154 }
155 }
Michalis Spyrou53860dd2019-07-01 14:20:56 +0100156
157 // Restore output stream flags
158 s.copyfmt(stream_status);
SiCong Li86b53332017-08-23 11:02:43 +0100159}
giuros01edc21e42018-11-16 14:45:31 +0000160#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
Georgios Pinitas1562be32018-03-08 19:09:19 +0000161
162bool ITensor::is_used() const
163{
164 return _is_used;
165}
166
167void ITensor::mark_as_unused() const
168{
169 _is_used = false;
170}