/home/runner/work/HiCR/HiCR/include/hicr/core/instanceManager.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/instanceManager.hpp Source File
HiCR
instanceManager.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Copyright Huawei Technologies Switzerland AG
19 * All rights reserved.
20 */
21
29#pragma once
30
31#include <map>
32#include <memory>
33#include <unordered_set>
34#include <utility>
35#include <functional>
42#include <hicr/core/topology.hpp>
46
47namespace HiCR
48{
49
57{
58 public:
59
63 using instanceList_t = std::vector<std::shared_ptr<HiCR::Instance>>;
64
68 InstanceManager() = default;
69
73 virtual ~InstanceManager() = default;
74
79 [[nodiscard]] __INLINE__ instanceList_t &getInstances() { return _instances; }
80
85 [[nodiscard]] __INLINE__ std::shared_ptr<HiCR::Instance> getCurrentInstance() const { return _currentInstance; }
86
92 __INLINE__ std::shared_ptr<HiCR::InstanceTemplate> createInstanceTemplate(const HiCR::Topology &requestedTopology = HiCR::Topology())
93 {
94 return std::make_shared<HiCR::InstanceTemplate>(requestedTopology);
95 }
96
102 __INLINE__ std::shared_ptr<HiCR::Instance> createInstance(const HiCR::InstanceTemplate instanceTemplate = HiCR::InstanceTemplate())
103 {
104 // Requesting the creating of the instance to the specific backend
105 auto newInstance = createInstanceImpl(instanceTemplate);
106
107 // If successul, adding the instance to the internal list
108 if (newInstance != nullptr) _instances.push_back(newInstance);
109
110 // Returning value for immediate use
111 return newInstance;
112 }
113
118 __INLINE__ void terminateInstance(const std::shared_ptr<HiCR::Instance> instance)
119 {
120 // Requesting the terminating of the instance to the specific backend
121 terminateInstanceImpl(instance);
122
123 // Remove from internal list
124 _instances.erase(std::remove_if(_instances.begin(), _instances.end(), [&](std::shared_ptr<Instance> &i) { return i->getId() == instance->getId(); }), _instances.end());
125 }
126
132 __INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
133 {
134 // Adding a new instance
135 auto instance = addInstanceImpl(instanceId);
136
137 // Adding the instance to the internal list
138 _instances.push_back(instance);
139 }
140
144 virtual void finalize() = 0;
145
151 virtual void abort(int errorCode) = 0;
152
157 [[nodiscard]] virtual HiCR::Instance::instanceId_t getRootInstanceId() const = 0;
158
159 protected:
160
166 virtual std::shared_ptr<HiCR::Instance> createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate)
167 {
168 HICR_THROW_LOGIC("This backend does not currently support the launching of new instances during runtime");
169 }
170
176 virtual std::shared_ptr<HiCR::Instance> addInstanceImpl(HiCR::Instance::instanceId_t instanceId)
177 {
178 HICR_THROW_LOGIC("This backend does not currently support the detection of new instances during runtime");
179 }
180
185 virtual void terminateInstanceImpl(const std::shared_ptr<HiCR::Instance> instance)
186 {
187 HICR_THROW_LOGIC("This backend does not currently support the termination of instances during runtime");
188 }
189
190 protected:
191
196 __INLINE__ void setCurrentInstance(const std::shared_ptr<HiCR::Instance> &instance) { _currentInstance = instance; }
197
202 __INLINE__ void addInstance(const std::shared_ptr<HiCR::Instance> &instance) { _instances.push_back(instance); }
203
204 private:
205
209 instanceList_t _instances;
210
214 std::shared_ptr<HiCR::Instance> _currentInstance;
215};
216
217} // namespace HiCR
Definition instanceManager.hpp:57
__INLINE__ std::shared_ptr< HiCR::Instance > createInstance(const HiCR::InstanceTemplate instanceTemplate=HiCR::InstanceTemplate())
Definition instanceManager.hpp:102
__INLINE__ std::shared_ptr< HiCR::Instance > getCurrentInstance() const
Definition instanceManager.hpp:85
virtual void abort(int errorCode)=0
__INLINE__ void addInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:202
__INLINE__ std::shared_ptr< HiCR::InstanceTemplate > createInstanceTemplate(const HiCR::Topology &requestedTopology=HiCR::Topology())
Definition instanceManager.hpp:92
__INLINE__ void terminateInstance(const std::shared_ptr< HiCR::Instance > instance)
Definition instanceManager.hpp:118
__INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:132
__INLINE__ void setCurrentInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:196
virtual void terminateInstanceImpl(const std::shared_ptr< HiCR::Instance > instance)
Definition instanceManager.hpp:185
__INLINE__ instanceList_t & getInstances()
Definition instanceManager.hpp:79
virtual ~InstanceManager()=default
virtual HiCR::Instance::instanceId_t getRootInstanceId() const =0
std::vector< std::shared_ptr< HiCR::Instance > > instanceList_t
Definition instanceManager.hpp:63
virtual std::shared_ptr< HiCR::Instance > addInstanceImpl(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:176
virtual std::shared_ptr< HiCR::Instance > createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate)
Definition instanceManager.hpp:166
virtual void finalize()=0
Definition instanceTemplate.hpp:44
uint64_t instanceId_t
Definition instance.hpp:44
Definition topology.hpp:41
Provides a definition for the base backend's communication manager class.
Provides a definition for the abstract compute manager class.
Provides a base definition for a HiCR Execution Unit class.
Provides a definition for the HiCR Instance class.
Provides a definition for a HiCR Local Memory Slot class.
Provides a definition for the base backend's memory manager class.
Provides a base definition for a HiCR MemorySpace class.
Provides a definition for a HiCR ProcessingUnit class.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
Provides a definition for the HiCR Instance Template class.