blob: c7a50d93c6b32aa64b618f435747c6d1db3d7e4e [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#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"
29
30namespace arm_compute
31{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010032namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033{
34namespace backends
35{
36/** Creates and configures a named function
37 *
38 * @param[in] name Name of the function
39 * @param[in] args Function arguments
40 *
41 * @return A configured backend function
42 */
43template <typename FunctionType, typename FunctionNameType, typename... ParameterType>
44std::pair<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_function(FunctionNameType name, ParameterType... args)
45{
46 auto f = arm_compute::support::cpp14::make_unique<FunctionType>();
47 f->configure(std::forward<ParameterType>(args)...);
48 return std::make_pair(std::move(f), name);
49}
50
51/** Creates and configures a named function
52 *
53 * @param[in] name Name of the function
54 * @param[in] mm Memory manager to use
55 * @param[in] args Function arguments
56 *
57 * @return A configured backend function
58 */
59template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType>
60std::pair<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_memory_managed_function(FunctionNameType name,
61 MemoryManagerType mm,
62 ParameterType... args)
63{
64 auto f = arm_compute::support::cpp14::make_unique<FunctionType>(mm);
65 f->configure(std::forward<ParameterType>(args)...);
66 return std::make_pair(std::move(f), name);
67}
68
69/** Checks if an operation is in place
70 *
71 * @param[in] input Pointer to input
72 * @param[in] output Pointer to output
73 *
74 * @return True if output is nullptr or input is equal to the output, else false
75 */
76inline bool is_in_place_operation(void *input, void *output)
77{
78 return (output == nullptr) || (input == output);
79}
80
81/** Returns the memory manager for a given target
82 *
83 * @param[in] ctx Graph context containing memory management metadata
84 * @param[in] target Target to retrieve the memory manager from
85 *
86 * @return The memory manager for the given target else false
87 */
88inline std::shared_ptr<IMemoryManager> get_memory_manager(GraphContext &ctx, Target target)
89{
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000090 bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010091 return enabled ? ctx.memory_management_ctx(target)->intra_mm : nullptr;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000092}
93} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010094} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000095} // namespace arm_compute
96
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010097#endif /* __ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H__ */