blob: 6756a90c2541971a64aefd8338a4d05d8e1bfa43 [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 SRC_COMMON_MEMORY_HELPERS_H
25#define SRC_COMMON_MEMORY_HELPERS_H
26
27#include "arm_compute/core/ITensorPack.h"
28#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/experimental/Types.h"
30#include "arm_compute/runtime/MemoryGroup.h"
31
32#include <memory>
33#include <utility>
34#include <vector>
35
36namespace arm_compute
37{
38inline int offset_int_vec(int offset)
39{
40 return ACL_INT_VEC + offset;
41}
42
43template <typename TensorType>
44using WorkspaceData = std::vector<std::pair<int, std::unique_ptr<TensorType>>>;
45
46template <typename TensorType>
47WorkspaceData<TensorType> manage_workspace(const experimental::MemoryRequirements &mem_reqs,
48 MemoryGroup &mgroup,
49 ITensorPack &run_pack, ITensorPack &prep_pack)
50{
51 WorkspaceData<TensorType> workspace_memory;
52 for(const auto &req : mem_reqs)
53 {
54 if(req.size == 0)
55 {
56 continue;
57 }
58
59 const auto aux_info = TensorInfo{ TensorShape(req.size), 1, DataType::U8 };
60 workspace_memory.emplace_back(req.slot, std::make_unique<TensorType>());
61
62 auto aux_tensor = workspace_memory.back().second.get();
63 ARM_COMPUTE_ERROR_ON_NULLPTR(aux_tensor);
64 aux_tensor->allocator()->init(aux_info);
65
66 if(req.lifetime == experimental::MemoryLifetime::Temporary)
67 {
68 mgroup.manage(aux_tensor);
69 }
70 else
71 {
72 prep_pack.add_tensor(req.slot, aux_tensor);
73 }
74 run_pack.add_tensor(req.slot, aux_tensor);
75 }
76
77 for(auto &mem : workspace_memory)
78 {
79 auto tensor = mem.second.get();
80 tensor->allocator()->allocate();
81 }
82
83 return workspace_memory;
84}
85} // namespace arm_compute
86#endif /* SRC_COMMON_MEMORY_HELPERS_H */