blob: f85fe27dbf748d5304459cb8cd0c8c6310a8df47 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +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 */
Anthony Barbier2a07e182017-08-04 18:20:27 +010024#include "arm_compute/graph/Tensor.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/CLTensor.h"
30#include "arm_compute/runtime/Tensor.h"
31#include "utils/TypePrinter.h"
32
33using namespace arm_compute::graph;
34
35namespace
36{
37template <typename TensorType>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010038std::unique_ptr<arm_compute::ITensor> initialise_tensor(TensorInfo &info)
Anthony Barbier2a07e182017-08-04 18:20:27 +010039{
40 auto tensor = arm_compute::support::cpp14::make_unique<TensorType>();
41 tensor->allocator()->init(info);
42 return std::move(tensor);
43}
44
45template <typename TensorType>
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010046void tensor_allocate(arm_compute::ITensor &tensor)
Anthony Barbier2a07e182017-08-04 18:20:27 +010047{
48 auto itensor = dynamic_cast<TensorType *>(&tensor);
49 ARM_COMPUTE_ERROR_ON_NULLPTR(itensor);
50 itensor->allocator()->allocate();
51}
52} // namespace
53
54Tensor::Tensor(TensorInfo &&info)
Georgios Pinitasff421f22017-10-04 16:53:58 +010055 : _target(TargetHint::DONT_CARE), _info(info), _accessor(nullptr), _tensor(nullptr)
Anthony Barbier2a07e182017-08-04 18:20:27 +010056{
57}
58
59Tensor::Tensor(Tensor &&src) noexcept
60 : _target(src._target),
61 _info(std::move(src._info)),
62 _accessor(std::move(src._accessor)),
63 _tensor(std::move(src._tensor))
64{
65}
66
67void Tensor::set_info(TensorInfo &&info)
68{
69 _info = info;
70}
71
72bool Tensor::call_accessor()
73{
74 ARM_COMPUTE_ERROR_ON_NULLPTR(_accessor.get());
75 auto cl_tensor = dynamic_cast<arm_compute::CLTensor *>(_tensor.get());
76 if(cl_tensor != nullptr && cl_tensor->buffer() == nullptr)
77 {
78 cl_tensor->map();
79 }
80 bool retval = _accessor->access_tensor(*_tensor);
81 if(cl_tensor != nullptr)
82 {
83 cl_tensor->unmap();
84 }
85 return retval;
86}
87
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010088bool Tensor::has_accessor() const
89{
90 return (_accessor != nullptr);
91}
92
93arm_compute::ITensor *Tensor::tensor()
Anthony Barbier2a07e182017-08-04 18:20:27 +010094{
95 return _tensor.get();
96}
97
98const TensorInfo &Tensor::info() const
99{
100 return _info;
101}
102
Georgios Pinitase2c82fe2017-10-02 18:51:47 +0100103arm_compute::ITensor *Tensor::set_target(TargetHint target)
Anthony Barbier2a07e182017-08-04 18:20:27 +0100104{
105 if(_tensor != nullptr)
106 {
107 ARM_COMPUTE_ERROR_ON(target != _target);
108 }
109 else
110 {
111 switch(target)
112 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100113 case TargetHint::OPENCL:
Anthony Barbier2a07e182017-08-04 18:20:27 +0100114 _tensor = initialise_tensor<arm_compute::CLTensor>(_info);
115 break;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100116 case TargetHint::NEON:
Anthony Barbier2a07e182017-08-04 18:20:27 +0100117 _tensor = initialise_tensor<arm_compute::Tensor>(_info);
118 break;
119 default:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100120 ARM_COMPUTE_ERROR("Invalid TargetHint");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100121 }
122 _target = target;
123 }
124 return _tensor.get();
125}
126
127void Tensor::allocate()
128{
129 ARM_COMPUTE_ERROR_ON_NULLPTR(_tensor.get());
130 switch(_target)
131 {
Georgios Pinitasff421f22017-10-04 16:53:58 +0100132 case TargetHint::OPENCL:
Anthony Barbier2a07e182017-08-04 18:20:27 +0100133 tensor_allocate<arm_compute::CLTensor>(*_tensor);
134 break;
Georgios Pinitasff421f22017-10-04 16:53:58 +0100135 case TargetHint::NEON:
Anthony Barbier2a07e182017-08-04 18:20:27 +0100136 tensor_allocate<arm_compute::Tensor>(*_tensor);
137 break;
138 default:
Georgios Pinitasff421f22017-10-04 16:53:58 +0100139 ARM_COMPUTE_ERROR("Invalid TargetHint");
Anthony Barbier2a07e182017-08-04 18:20:27 +0100140 }
141}
142
143void Tensor::allocate_and_fill_if_needed()
144{
145 allocate();
146 if(_accessor != nullptr)
147 {
148 call_accessor();
149 }
150}
151
Georgios Pinitasff421f22017-10-04 16:53:58 +0100152TargetHint Tensor::target() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100153{
154 return _target;
155}