arm_compute v18.01
[platform/upstream/armcl.git] / src / graph / SubTensor.cpp
1 /*
2  * Copyright (c) 2017-2018 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/CL/CLTensor.h"
31 #include "arm_compute/runtime/SubTensor.h"
32 #include "arm_compute/runtime/Tensor.h"
33 #include "utils/TypePrinter.h"
34
35 using namespace arm_compute::graph;
36
37 namespace
38 {
39 template <typename SubTensorType, typename ParentTensorType>
40 std::unique_ptr<arm_compute::ITensor> initialise_subtensor(arm_compute::ITensor *parent, TensorShape shape, Coordinates coords, bool extend_parent)
41 {
42     auto ptensor   = dynamic_cast<ParentTensorType *>(parent);
43     auto subtensor = arm_compute::support::cpp14::make_unique<SubTensorType>(ptensor, shape, coords, extend_parent);
44     return std::move(subtensor);
45 }
46 } // namespace
47
48 SubTensor::SubTensor()
49     : _target(TargetHint::DONT_CARE), _tensor_shape(), _coords(), _parent(nullptr), _subtensor(nullptr), _extend_parent(false)
50 {
51 }
52
53 SubTensor::SubTensor(Tensor &parent, TensorShape tensor_shape, Coordinates coords, bool extend_parent)
54     : _target(TargetHint::DONT_CARE), _tensor_shape(tensor_shape), _coords(coords), _parent(nullptr), _subtensor(nullptr), _extend_parent(extend_parent)
55 {
56     ARM_COMPUTE_ERROR_ON(parent.tensor() == nullptr);
57     _parent = parent.tensor();
58     _target = parent.target();
59
60     instantiate_subtensor();
61 }
62
63 SubTensor::SubTensor(arm_compute::ITensor *parent, TensorShape tensor_shape, Coordinates coords, TargetHint target, bool extend_parent)
64     : _target(target), _tensor_shape(tensor_shape), _coords(coords), _parent(parent), _subtensor(nullptr), _extend_parent(extend_parent)
65 {
66     ARM_COMPUTE_ERROR_ON(parent == nullptr);
67     instantiate_subtensor();
68 }
69
70 bool SubTensor::call_accessor()
71 {
72     return true;
73 }
74
75 bool SubTensor::has_accessor() const
76 {
77     return false;
78 }
79
80 arm_compute::ITensor *SubTensor::set_target(TargetHint target)
81 {
82     ARM_COMPUTE_ERROR_ON(target != _target);
83     return (target == _target) ? _subtensor.get() : nullptr;
84 }
85
86 arm_compute::ITensor *SubTensor::tensor()
87 {
88     return _subtensor.get();
89 }
90
91 const arm_compute::ITensor *SubTensor::tensor() const
92 {
93     return _subtensor.get();
94 }
95
96 TargetHint SubTensor::target() const
97 {
98     return _target;
99 }
100
101 void SubTensor::allocate()
102 {
103     // NOP for sub-tensors
104 }
105
106 void SubTensor::instantiate_subtensor()
107 {
108     switch(_target)
109     {
110         case TargetHint::OPENCL:
111             _subtensor = initialise_subtensor<arm_compute::CLSubTensor, arm_compute::ICLTensor>(_parent, _tensor_shape, _coords, _extend_parent);
112             break;
113         case TargetHint::NEON:
114             _subtensor = initialise_subtensor<arm_compute::SubTensor, arm_compute::ITensor>(_parent, _tensor_shape, _coords, _extend_parent);
115             break;
116         default:
117             ARM_COMPUTE_ERROR("Invalid TargetHint");
118     }
119 }