c534ae0296827447479ac462df753d70c9bd4850
[platform/upstream/armcl.git] / src / graph / Tensor.cpp
1 /*
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
25 #include "arm_compute/graph/Tensor.h"
26
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/runtime/CL/CLTensor.h"
31 #include "arm_compute/runtime/Tensor.h"
32 #include "utils/TypePrinter.h"
33
34 using namespace arm_compute::graph;
35
36 namespace
37 {
38 template <typename TensorType>
39 std::unique_ptr<ITensor> initialise_tensor(TensorInfo &info)
40 {
41     auto tensor = arm_compute::support::cpp14::make_unique<TensorType>();
42     tensor->allocator()->init(info);
43     return std::move(tensor);
44 }
45
46 template <typename TensorType>
47 void tensor_allocate(ITensor &tensor)
48 {
49     auto itensor = dynamic_cast<TensorType *>(&tensor);
50     ARM_COMPUTE_ERROR_ON_NULLPTR(itensor);
51     itensor->allocator()->allocate();
52 }
53 } // namespace
54
55 Tensor::Tensor(TensorInfo &&info)
56     : _target(Hint::DONT_CARE), _info(info), _accessor(nullptr), _tensor(nullptr)
57 {
58 }
59
60 Tensor::Tensor(Tensor &&src) noexcept
61     : _target(src._target),
62       _info(std::move(src._info)),
63       _accessor(std::move(src._accessor)),
64       _tensor(std::move(src._tensor))
65 {
66 }
67
68 void Tensor::set_info(TensorInfo &&info)
69 {
70     _info = info;
71 }
72
73 bool Tensor::call_accessor()
74 {
75     ARM_COMPUTE_ERROR_ON_NULLPTR(_accessor.get());
76     auto cl_tensor = dynamic_cast<arm_compute::CLTensor *>(_tensor.get());
77     if(cl_tensor != nullptr && cl_tensor->buffer() == nullptr)
78     {
79         cl_tensor->map();
80     }
81     bool retval = _accessor->access_tensor(*_tensor);
82     if(cl_tensor != nullptr)
83     {
84         cl_tensor->unmap();
85     }
86     return retval;
87 }
88
89 ITensor *Tensor::tensor()
90 {
91     return _tensor.get();
92 }
93
94 const TensorInfo &Tensor::info() const
95 {
96     return _info;
97 }
98
99 ITensor *Tensor::set_target(Hint target)
100 {
101     if(_tensor != nullptr)
102     {
103         ARM_COMPUTE_ERROR_ON(target != _target);
104     }
105     else
106     {
107         switch(target)
108         {
109             case Hint::OPENCL:
110                 _tensor = initialise_tensor<arm_compute::CLTensor>(_info);
111                 break;
112             case Hint::NEON:
113                 _tensor = initialise_tensor<arm_compute::Tensor>(_info);
114                 break;
115             default:
116                 ARM_COMPUTE_ERROR("Invalid Hint");
117         }
118         _target = target;
119     }
120     return _tensor.get();
121 }
122
123 void Tensor::allocate()
124 {
125     ARM_COMPUTE_ERROR_ON_NULLPTR(_tensor.get());
126     switch(_target)
127     {
128         case Hint::OPENCL:
129             tensor_allocate<arm_compute::CLTensor>(*_tensor);
130             break;
131         case Hint::NEON:
132             tensor_allocate<arm_compute::Tensor>(*_tensor);
133             break;
134         default:
135             ARM_COMPUTE_ERROR("Invalid Hint");
136     }
137 }
138
139 void Tensor::allocate_and_fill_if_needed()
140 {
141     allocate();
142     if(_accessor != nullptr)
143     {
144         call_accessor();
145     }
146 }
147
148 Hint Tensor::target() const
149 {
150     return _target;
151 }