Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Arm Limited. |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [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_GRAPH_CONTEXT_H |
| 25 | #define ARM_COMPUTE_GRAPH_GRAPH_CONTEXT_H |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/graph/Types.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [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 | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 30 | |
| 31 | #include <map> |
| 32 | #include <memory> |
| 33 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace graph |
| 37 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 38 | /** Contains structs required for memory management */ |
| 39 | struct MemoryManagerContext |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 40 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | Target target = {Target::UNSPECIFIED}; /**< Target */ |
| 42 | std::shared_ptr<arm_compute::IMemoryManager> intra_mm = {nullptr}; /**< Intra-function memory manager */ |
| 43 | std::shared_ptr<arm_compute::IMemoryManager> cross_mm = {nullptr}; /**< Cross-function memory manager */ |
| 44 | std::shared_ptr<arm_compute::IMemoryGroup> cross_group = {nullptr}; /**< Cross-function memory group */ |
| 45 | IAllocator *allocator = {nullptr}; /**< Backend allocator to use */ |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 48 | /** Contains structs required for weights management */ |
| 49 | struct WeightsManagerContext |
| 50 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 51 | Target target = {Target::UNSPECIFIED}; /**< Target */ |
| 52 | std::shared_ptr<arm_compute::IWeightsManager> wm = {nullptr}; /**< Weights manager */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 55 | /** Graph context **/ |
| 56 | class GraphContext final |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 57 | { |
| 58 | public: |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 59 | /** Constructor */ |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 60 | GraphContext(); |
Anthony Barbier | 5a65cfd | 2018-08-10 14:10:08 +0100 | [diff] [blame] | 61 | /** Destructor */ |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 62 | ~GraphContext(); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 63 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 64 | GraphContext(const GraphContext &) = delete; |
| 65 | /** Default move constructor */ |
| 66 | GraphContext(GraphContext &&) = default; |
| 67 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 68 | GraphContext &operator=(const GraphContext &) = delete; |
| 69 | /** Default move assignment operator */ |
| 70 | GraphContext &operator=(GraphContext &&) = default; |
| 71 | /** Graph configuration accessor |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 72 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 73 | * @note Every alteration has to be done before graph finalization |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 74 | * |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 75 | * @return The graph configuration |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 76 | */ |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 77 | const GraphConfig &config() const; |
| 78 | /** Sets graph configuration |
| 79 | * |
| 80 | * @param[in] config Configuration to use |
| 81 | */ |
| 82 | void set_config(const GraphConfig &config); |
| 83 | /** Inserts a memory manager context |
| 84 | * |
| 85 | * @param[in] memory_ctx Memory manage context |
| 86 | * |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 87 | * @return True if the insertion succeeded else false |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 88 | */ |
| 89 | bool insert_memory_management_ctx(MemoryManagerContext &&memory_ctx); |
| 90 | /** Gets a memory manager context for a given target |
| 91 | * |
| 92 | * @param[in] target To retrieve the management context |
| 93 | * |
| 94 | * @return Management context for the target if exists else nullptr |
| 95 | */ |
| 96 | MemoryManagerContext *memory_management_ctx(Target target); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 97 | /** Gets the memory managers map |
| 98 | * |
| 99 | * @return Memory manager contexts |
| 100 | */ |
| 101 | std::map<Target, MemoryManagerContext> &memory_managers(); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 102 | /** Inserts a weights manager context |
| 103 | * |
| 104 | * @param[in] weights_ctx Weights manager context |
| 105 | * |
| 106 | * @return True if the insertion succeeded else false |
| 107 | */ |
| 108 | bool insert_weights_management_ctx(WeightsManagerContext &&weights_ctx); |
| 109 | |
| 110 | /** Gets a weights manager context for a given target |
| 111 | * |
| 112 | * @param[in] target To retrieve the weights management context |
| 113 | * |
| 114 | * @return Management context for the target if exists else nullptr |
| 115 | */ |
| 116 | WeightsManagerContext *weights_management_ctx(Target target); |
| 117 | |
| 118 | /** Gets the weights managers map |
| 119 | * |
| 120 | * @return Weights manager contexts |
| 121 | */ |
| 122 | std::map<Target, WeightsManagerContext> &weights_managers(); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 123 | /** Finalizes memory managers in graph context */ |
| 124 | void finalize(); |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 125 | |
| 126 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | GraphConfig _config; /**< Graph configuration */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 128 | std::map<Target, MemoryManagerContext> _memory_managers; /**< Memory managers for each target */ |
| 129 | std::map<Target, WeightsManagerContext> _weights_managers; /**< Weights managers for each target */ |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 130 | }; |
| 131 | } // namespace graph |
| 132 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 133 | #endif /* ARM_COMPUTE_GRAPH_GRAPH_CONTEXT_H */ |