blob: ad893acea52ca2ec15ae87d42b14d3a28ec95004 [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +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_CL_UTILS_CL_AUX_TENSOR_HANDLER_H
25#define ARM_COMPUTE_CL_UTILS_CL_AUX_TENSOR_HANDLER_H
26
27#include "arm_compute/core/ITensorPack.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/runtime/CL/CLTensor.h"
30
31#include "support/Cast.h"
32
33namespace arm_compute
34{
35namespace opencl
36{
37/* Tensor handler to wrap and handle tensor allocations on workspace buffers */
38class CLAuxTensorHandler
39{
40public:
41 CLAuxTensorHandler(int slot_id, TensorInfo &info, ITensorPack &pack, bool pack_inject = false)
42 : _tensor()
43 {
44 _tensor.allocator()->soft_init(info);
45
46 ICLTensor *packed_tensor = utils::cast::polymorphic_downcast<ICLTensor *>(pack.get_tensor(slot_id));
47 if((packed_tensor == nullptr) || (info.total_size() > packed_tensor->info()->total_size()))
48 {
49 _tensor.allocator()->allocate();
50 if(pack_inject)
51 {
52 pack.add_tensor(slot_id, &_tensor);
53 }
54 }
55 else
56 {
57 _tensor.allocator()->import_memory(packed_tensor->cl_buffer());
58 }
59 }
60
61 CLAuxTensorHandler(TensorInfo &info, ICLTensor &tensor)
62 : _tensor()
63 {
64 _tensor.allocator()->soft_init(info);
65 if(info.total_size() <= tensor.info()->total_size())
66 {
67 _tensor.allocator()->import_memory(tensor.cl_buffer());
68 }
69 }
70
71 ICLTensor *get()
72 {
73 return &_tensor;
74 }
75
76 ICLTensor *operator()()
77 {
78 return &_tensor;
79 }
80
81private:
82 CLTensor _tensor{};
83};
84} // namespace opencl
85} // namespace arm_compute
86#endif /* ARM_COMPUTE_CL_UTILS_CL_AUX_TENSOR_HANDLER_H */