blob: 4893340b4c926ee22ea379db8c8d59dfd60c3c33 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michalis Spyrou1a569a32019-09-10 17:20:34 +01002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H
25#define ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H
Georgios Pinitasd8734b52017-12-22 15:27:52 +000026
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/GraphContext.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000028#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010029#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000030
31namespace arm_compute
32{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034{
35namespace 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 */
44template <typename FunctionType, typename FunctionNameType, typename... ParameterType>
45std::pair<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_function(FunctionNameType name, ParameterType... args)
46{
47 auto f = arm_compute::support::cpp14::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 */
60template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType>
61std::pair<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_memory_managed_function(FunctionNameType name,
62 MemoryManagerType mm,
63 ParameterType... args)
64{
65 auto f = arm_compute::support::cpp14::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 */
77inline 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 */
89inline std::shared_ptr<IMemoryManager> get_memory_manager(GraphContext &ctx, Target target)
90{
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000091 bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010092 return enabled ? ctx.memory_management_ctx(target)->intra_mm : nullptr;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000093}
Michalis Spyrou1a569a32019-09-10 17:20:34 +010094
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 */
102inline 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}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000107} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100108} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000109} // namespace arm_compute
110
Michalis Spyrouf4643372019-11-29 16:17:13 +0000111#endif /* ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H */