blob: 31dd4e86accb1382d6b731e858d62de332a578a3 [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38:23 +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 */
Kaizen8938bd32017-09-28 14:38:23 +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>
38std::unique_ptr<ITensor> initialise_tensor(TensorInfo &info)
39{
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>
46void tensor_allocate(ITensor &tensor)
47{
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)
Kaizenbf8b01d2017-10-12 14:26:51 +010055 : _target(TargetHint::DONT_CARE), _info(info), _accessor(nullptr), _tensor(nullptr)
Kaizen8938bd32017-09-28 14:38:23 +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
88ITensor *Tensor::tensor()
89{
90 return _tensor.get();
91}
92
93const TensorInfo &Tensor::info() const
94{
95 return _info;
96}
97
Kaizenbf8b01d2017-10-12 14:26:51 +010098ITensor *Tensor::set_target(TargetHint target)
Kaizen8938bd32017-09-28 14:38:23 +010099{
100 if(_tensor != nullptr)
101 {
102 ARM_COMPUTE_ERROR_ON(target != _target);
103 }
104 else
105 {
106 switch(target)
107 {
Kaizenbf8b01d2017-10-12 14:26:51 +0100108 case TargetHint::OPENCL:
Kaizen8938bd32017-09-28 14:38:23 +0100109 _tensor = initialise_tensor<arm_compute::CLTensor>(_info);
110 break;
Kaizenbf8b01d2017-10-12 14:26:51 +0100111 case TargetHint::NEON:
Kaizen8938bd32017-09-28 14:38:23 +0100112 _tensor = initialise_tensor<arm_compute::Tensor>(_info);
113 break;
114 default:
Kaizenbf8b01d2017-10-12 14:26:51 +0100115 ARM_COMPUTE_ERROR("Invalid TargetHint");
Kaizen8938bd32017-09-28 14:38:23 +0100116 }
117 _target = target;
118 }
119 return _tensor.get();
120}
121
122void Tensor::allocate()
123{
124 ARM_COMPUTE_ERROR_ON_NULLPTR(_tensor.get());
125 switch(_target)
126 {
Kaizenbf8b01d2017-10-12 14:26:51 +0100127 case TargetHint::OPENCL:
Kaizen8938bd32017-09-28 14:38:23 +0100128 tensor_allocate<arm_compute::CLTensor>(*_tensor);
129 break;
Kaizenbf8b01d2017-10-12 14:26:51 +0100130 case TargetHint::NEON:
Kaizen8938bd32017-09-28 14:38:23 +0100131 tensor_allocate<arm_compute::Tensor>(*_tensor);
132 break;
133 default:
Kaizenbf8b01d2017-10-12 14:26:51 +0100134 ARM_COMPUTE_ERROR("Invalid TargetHint");
Kaizen8938bd32017-09-28 14:38:23 +0100135 }
136}
137
138void Tensor::allocate_and_fill_if_needed()
139{
140 allocate();
141 if(_accessor != nullptr)
142 {
143 call_accessor();
144 }
145}
146
Kaizenbf8b01d2017-10-12 14:26:51 +0100147TargetHint Tensor::target() const
Kaizen8938bd32017-09-28 14:38:23 +0100148{
149 return _target;
150}