blob: 0d1c927b5a439315762eab2312fb18389460067f [file] [log] [blame]
Manuel Bottini94f799e2021-06-09 16:37:32 +01001/*
2 * Copyright (c) 2021 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#ifndef ARM_COMPUTE_CPU_UTILS_CPU_AUX_TENSOR_HANDLER_H
25#define ARM_COMPUTE_CPU_UTILS_CPU_AUX_TENSOR_HANDLER_H
26
27#include "arm_compute/core/ITensorPack.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/runtime/Tensor.h"
30
31#include "support/Cast.h"
32
33namespace arm_compute
34{
35namespace cpu
36{
37/* Tensor handler to wrap and handle tensor allocations on workspace buffers */
38class CpuAuxTensorHandler
39{
40public:
41 CpuAuxTensorHandler(int slot_id, TensorInfo &info, ITensorPack &pack, bool pack_inject = false)
42 : _tensor()
43 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010044 if(info.total_size() == 0)
45 {
46 return;
47 }
Manuel Bottini94f799e2021-06-09 16:37:32 +010048 _tensor.allocator()->soft_init(info);
49
50 ITensor *packed_tensor = utils::cast::polymorphic_downcast<ITensor *>(pack.get_tensor(slot_id));
51 if((packed_tensor == nullptr) || (info.total_size() > packed_tensor->info()->total_size()))
52 {
53 _tensor.allocator()->allocate();
54 if(pack_inject)
55 {
56 pack.add_tensor(slot_id, &_tensor);
57 _injected_tensor_pack = &pack;
58 _injected_slot_id = slot_id;
59 }
60 }
61 else
62 {
63 _tensor.allocator()->import_memory(packed_tensor->buffer());
64 }
65 }
66
67 CpuAuxTensorHandler(TensorInfo &info, ITensor &tensor)
68 : _tensor()
69 {
70 _tensor.allocator()->soft_init(info);
71 if(info.total_size() <= tensor.info()->total_size())
72 {
73 _tensor.allocator()->import_memory(tensor.buffer());
74 }
75 }
76
77 CpuAuxTensorHandler(const CpuAuxTensorHandler &) = delete;
78 CpuAuxTensorHandler &operator=(const CpuAuxTensorHandler) = delete;
79
80 ~CpuAuxTensorHandler()
81 {
82 if(_injected_tensor_pack)
83 {
84 _injected_tensor_pack->remove_tensor(_injected_slot_id);
85 }
86 }
87
88 ITensor *get()
89 {
90 return &_tensor;
91 }
92
93 ITensor *operator()()
94 {
95 return &_tensor;
96 }
97
98private:
99 Tensor _tensor{};
100 ITensorPack *_injected_tensor_pack{ nullptr };
101 int _injected_slot_id{ TensorType::ACL_UNKNOWN };
102};
103} // namespace cpu
104} // namespace arm_compute
105#endif /* ARM_COMPUTE_CPU_UTILS_CPU_AUX_TENSOR_HANDLER_H */