/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 RPCTargetIndex_t = uint64_t;
64
68 using RPCFunction_t = std::function<void()>;
69
73 using instanceList_t = std::vector<std::shared_ptr<HiCR::Instance>>;
74
78 InstanceManager() = default;
79
83 virtual ~InstanceManager() = default;
84
89 [[nodiscard]] __INLINE__ instanceList_t &getInstances() { return _instances; }
90
95 [[nodiscard]] __INLINE__ std::shared_ptr<HiCR::Instance> getCurrentInstance() const { return _currentInstance; }
96
102 __INLINE__ std::shared_ptr<HiCR::InstanceTemplate> createInstanceTemplate(const HiCR::Topology &requestedTopology = HiCR::Topology())
103 {
104 return std::make_shared<HiCR::InstanceTemplate>(requestedTopology);
105 }
106
112 __INLINE__ std::shared_ptr<HiCR::Instance> createInstance(const HiCR::InstanceTemplate instanceTemplate)
113 {
114 // Requesting the creating of the instance to the specific backend
115 auto newInstance = createInstanceImpl(instanceTemplate);
116
117 // If successul, adding the instance to the internal list
118 if (newInstance != nullptr) _instances.push_back(newInstance);
119
120 // Returning value for immediate use
121 return newInstance;
122 }
123
128 __INLINE__ void terminateInstance(const std::shared_ptr<HiCR::Instance> instance)
129 {
130 // Requesting the terminating of the instance to the specific backend
131 terminateInstanceImpl(instance);
132 }
133
139 __INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
140 {
141 // Adding a new instance
142 auto instance = addInstanceImpl(instanceId);
143
144 // Adding the instance to the internal list
145 _instances.push_back(instance);
146 }
147
151 virtual void finalize() = 0;
152
158 virtual void abort(int errorCode) = 0;
159
164 [[nodiscard]] virtual HiCR::Instance::instanceId_t getRootInstanceId() const = 0;
165
166 protected:
167
173 virtual std::shared_ptr<HiCR::Instance> createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate)
174 {
175 HICR_THROW_LOGIC("This backend does not currently support the launching of new instances during runtime");
176 }
177
183 virtual std::shared_ptr<HiCR::Instance> addInstanceImpl(HiCR::Instance::instanceId_t instanceId)
184 {
185 HICR_THROW_LOGIC("The Host backend does not currently support the detection of new instances during runtime");
186 }
187
192 virtual void terminateInstanceImpl(const std::shared_ptr<HiCR::Instance> instance)
193 {
194 HICR_THROW_LOGIC("The Host backend does not currently support the termination of instances during runtime");
195 }
196
197 protected:
198
203 __INLINE__ void setCurrentInstance(const std::shared_ptr<HiCR::Instance> &instance) { _currentInstance = instance; }
204
209 __INLINE__ void addInstance(const std::shared_ptr<HiCR::Instance> &instance) { _instances.push_back(instance); }
210
211 private:
212
216 instanceList_t _instances;
217
221 std::shared_ptr<HiCR::Instance> _currentInstance;
222
226 std::map<RPCTargetIndex_t, RPCFunction_t> _RPCTargetMap;
227};
228
229} // namespace HiCR
Definition instanceManager.hpp:57
__INLINE__ std::shared_ptr< HiCR::Instance > getCurrentInstance() const
Definition instanceManager.hpp:95
virtual void abort(int errorCode)=0
__INLINE__ void addInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:209
std::function< void()> RPCFunction_t
Definition instanceManager.hpp:68
__INLINE__ std::shared_ptr< HiCR::InstanceTemplate > createInstanceTemplate(const HiCR::Topology &requestedTopology=HiCR::Topology())
Definition instanceManager.hpp:102
__INLINE__ void terminateInstance(const std::shared_ptr< HiCR::Instance > instance)
Definition instanceManager.hpp:128
__INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:139
__INLINE__ void setCurrentInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:203
virtual void terminateInstanceImpl(const std::shared_ptr< HiCR::Instance > instance)
Definition instanceManager.hpp:192
__INLINE__ instanceList_t & getInstances()
Definition instanceManager.hpp:89
virtual ~InstanceManager()=default
__INLINE__ std::shared_ptr< HiCR::Instance > createInstance(const HiCR::InstanceTemplate instanceTemplate)
Definition instanceManager.hpp:112
virtual HiCR::Instance::instanceId_t getRootInstanceId() const =0
uint64_t RPCTargetIndex_t
Definition instanceManager.hpp:63
std::vector< std::shared_ptr< HiCR::Instance > > instanceList_t
Definition instanceManager.hpp:73
virtual std::shared_ptr< HiCR::Instance > addInstanceImpl(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:183
virtual std::shared_ptr< HiCR::Instance > createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate)
Definition instanceManager.hpp:173
virtual void finalize()=0
Definition instanceTemplate.hpp:44
uint64_t instanceId_t
Definition instance.hpp:44
Definition topology.hpp:40
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.