Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H |
| 25 | #define ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 26 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/GraphContext.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/IMemoryManager.h" |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 33 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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::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 | */ |
| 60 | template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType> |
| 61 | std::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 | */ |
| 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 | { |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 91 | bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 92 | return enabled ? ctx.memory_management_ctx(target)->intra_mm : nullptr; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 93 | } |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 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 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 107 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 108 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 109 | } // namespace arm_compute |
| 110 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 111 | #endif /* ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H */ |