blob: abf8506c336134c0db33135452a158d80e6a3315 [file] [log] [blame]
Kaizenbf8b01d2017-10-12 14:26:51 +01001/*
2 * Copyright (c) 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#include "arm_compute/graph/SubTensor.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/runtime/CL/CLSubTensor.h"
30#include "arm_compute/runtime/SubTensor.h"
31#include "utils/TypePrinter.h"
32
33using namespace arm_compute::graph;
34
35namespace
36{
37template <typename SubTensorType, typename ParentTensorType>
38std::unique_ptr<ITensor> initialise_subtensor(ITensor *parent, TensorShape shape, Coordinates coords)
39{
40 auto ptensor = dynamic_cast<ParentTensorType *>(parent);
41 auto subtensor = arm_compute::support::cpp14::make_unique<SubTensorType>(ptensor, shape, coords);
42 return std::move(subtensor);
43}
44} // namespace
45
46SubTensor::SubTensor()
47 : _target(TargetHint::DONT_CARE), _coords(), _info(), _parent(nullptr), _subtensor(nullptr)
48{
49}
50
51SubTensor::SubTensor(Tensor &parent, TensorShape tensor_shape, Coordinates coords)
52 : _target(TargetHint::DONT_CARE), _coords(coords), _info(), _parent(nullptr), _subtensor(nullptr)
53{
54 ARM_COMPUTE_ERROR_ON(parent.tensor() == nullptr);
55 _parent = parent.tensor();
56 _info = SubTensorInfo(parent.tensor()->info(), tensor_shape, coords);
57 _target = parent.target();
58
59 instantiate_subtensor();
60}
61
62SubTensor::SubTensor(ITensor *parent, TensorShape tensor_shape, Coordinates coords, TargetHint target)
63 : _target(target), _coords(coords), _info(), _parent(parent), _subtensor(nullptr)
64{
65 ARM_COMPUTE_ERROR_ON(parent == nullptr);
66 _info = SubTensorInfo(parent->info(), tensor_shape, coords);
67
68 instantiate_subtensor();
69}
70
71void SubTensor::set_info(SubTensorInfo &&info)
72{
73 _info = info;
74}
75
76const SubTensorInfo &SubTensor::info() const
77{
78 return _info;
79}
80
81ITensor *SubTensor::tensor()
82{
83 return _subtensor.get();
84}
85
86TargetHint SubTensor::target() const
87{
88 return _target;
89}
90
91void SubTensor::instantiate_subtensor()
92{
93 switch(_target)
94 {
95 case TargetHint::OPENCL:
96 _subtensor = initialise_subtensor<arm_compute::CLSubTensor, arm_compute::ICLTensor>(_parent, _info.tensor_shape(), _coords);
97 break;
98 case TargetHint::NEON:
99 _subtensor = initialise_subtensor<arm_compute::SubTensor, arm_compute::ITensor>(_parent, _info.tensor_shape(), _coords);
100 break;
101 default:
102 ARM_COMPUTE_ERROR("Invalid TargetHint");
103 }
104}