Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [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" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | { |
| 34 | namespace 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 | */ |
| 43 | template <typename FunctionType, typename FunctionNameType, typename... ParameterType> |
| 44 | std::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 | */ |
| 59 | template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType> |
| 60 | std::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 | */ |
| 76 | inline 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 | */ |
| 88 | inline std::shared_ptr<IMemoryManager> get_memory_manager(GraphContext &ctx, Target target) |
| 89 | { |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 90 | bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 91 | return enabled ? ctx.memory_management_ctx(target)->mm : nullptr; |
| 92 | } |
| 93 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 94 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 95 | } // namespace arm_compute |
| 96 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 97 | #endif /* __ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H__ */ |