Compute Library
 21.05
Utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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_GRAPH_BACKENDS_UTILS_H
25 #define ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H
26 
30 
31 namespace arm_compute
32 {
33 namespace graph
34 {
35 namespace backends
36 {
37 /** Creates and configures a named function
38  *
39  * @param[in] name Name of the function
40  * @param[in] args Function arguments
41  *
42  * @return A configured backend function
43  */
44 template <typename FunctionType, typename FunctionNameType, typename... ParameterType>
45 std::tuple<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_function(FunctionNameType name, ParameterType... args)
46 {
47  auto f = std::make_unique<FunctionType>();
48  f->configure(std::forward<ParameterType>(args)...);
49  return std::make_pair(std::move(f), name);
50 }
51 
52 /** Creates and configures a named function
53  *
54  * @param[in] name Name of the function
55  * @param[in] mm Memory manager to use
56  * @param[in] args Function arguments
57  *
58  * @return A configured backend function
59  */
60 template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType>
61 std::tuple<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_memory_managed_function(FunctionNameType name,
62  MemoryManagerType mm,
63  ParameterType... args)
64 {
65  auto f = std::make_unique<FunctionType>(mm);
66  f->configure(std::forward<ParameterType>(args)...);
67  return std::make_pair(std::move(f), name);
68 }
69 
70 /** Checks if an operation is in place
71  *
72  * @param[in] input Pointer to input
73  * @param[in] output Pointer to output
74  *
75  * @return True if output is nullptr or input is equal to the output, else false
76  */
77 inline bool is_in_place_operation(void *input, void *output)
78 {
79  return (output == nullptr) || (input == output);
80 }
81 
82 /** Returns the memory manager for a given target
83  *
84  * @param[in] ctx Graph context containing memory management metadata
85  * @param[in] target Target to retrieve the memory manager from
86  *
87  * @return The memory manager for the given target else false
88  */
89 inline std::shared_ptr<IMemoryManager> get_memory_manager(GraphContext &ctx, Target target)
90 {
91  bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr);
92  return enabled ? ctx.memory_management_ctx(target)->intra_mm : nullptr;
93 }
94 
95 /** Returns the weights manager for a given target
96  *
97  * @param[in] ctx Graph context containing weight management metadata
98  * @param[in] target Target to retrieve the weights manager from
99  *
100  * @return The weights manager for the given target else false
101  */
102 inline std::shared_ptr<IWeightsManager> get_weights_manager(GraphContext &ctx, Target target)
103 {
104  bool enabled = ctx.config().use_function_weights_manager && (ctx.weights_management_ctx(target) != nullptr);
105  return enabled ? ctx.weights_management_ctx(target)->wm : nullptr;
106 }
107 } // namespace backends
108 } // namespace graph
109 } // namespace arm_compute
110 
111 #endif /* ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H */
std::tuple< std::unique_ptr< arm_compute::IFunction >, FunctionNameType > create_named_function(FunctionNameType name, ParameterType... args)
Creates and configures a named function.
Definition: Utils.h:45
bool use_function_weights_manager
Use a weights manager to manage transformed weights.
Definition: Types.h:86
WeightsManagerContext * weights_management_ctx(Target target)
Gets a weights manager context for a given target.
Copyright (c) 2017-2021 Arm Limited.
bool use_function_memory_manager
Use a memory manager to manage per-function auxilary memory.
Definition: Types.h:85
std::tuple< std::unique_ptr< arm_compute::IFunction >, FunctionNameType > create_named_memory_managed_function(FunctionNameType name, MemoryManagerType mm, ParameterType... args)
Creates and configures a named function.
Definition: Utils.h:61
const char * name
bool is_in_place_operation(void *input, void *output)
Checks if an operation is in place.
Definition: Utils.h:77
std::shared_ptr< arm_compute::IMemoryManager > intra_mm
Intra-function memory manager.
Definition: GraphContext.h:43
const GraphConfig & config() const
Graph configuration accessor.
std::shared_ptr< IMemoryManager > get_memory_manager(GraphContext &ctx, Target target)
Returns the memory manager for a given target.
Definition: Utils.h:89
MemoryManagerContext * memory_management_ctx(Target target)
Gets a memory manager context for a given target.
std::shared_ptr< IWeightsManager > get_weights_manager(GraphContext &ctx, Target target)
Returns the weights manager for a given target.
Definition: Utils.h:102
std::shared_ptr< arm_compute::IWeightsManager > wm
Weights manager.
Definition: GraphContext.h:53