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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/topology.hpp Source File
HiCR
topology.hpp
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
23#pragma once
24
25#include <memory>
26#include <unordered_set>
27#include <nlohmann_json/json.hpp>
28#include <nlohmann_json/parser.hpp>
29#include <hicr/core/device.hpp>
30
31namespace HiCR
32{
33
41{
42 public:
43
47 using deviceList_t = std::vector<std::shared_ptr<Device>>;
48
49 Topology() = default;
50 ~Topology() = default;
51
57 Topology(const nlohmann::json &input) { deserialize(input); }
58
63 [[nodiscard]] __INLINE__ const deviceList_t &getDevices() const { return _deviceList; }
64
70 __INLINE__ void addDevice(const std::shared_ptr<HiCR::Device> &device) { _deviceList.push_back(device); }
71
77 __INLINE__ void merge(const Topology &source)
78 {
79 // Adding each device separately
80 for (const auto &d : source.getDevices()) addDevice(d);
81 }
82
88 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
89 {
90 // Storage for newly created serialized output
91 nlohmann::json output;
92
93 // Adding serialized devices information into the array
94 std::string devicesKey = "Devices";
95 output[devicesKey] = std::vector<nlohmann::json>();
96 for (const auto &device : _deviceList) output[devicesKey] += device->serialize();
97
98 // Adding metadata, if defined
99 output["Metadata"] = _metadata;
100
101 // Returning topology
102 return output;
103 }
104
113 __INLINE__ void deserialize(const nlohmann::json &input)
114 {
115 if (input.contains("Devices"))
116 {
117 const auto &devicesJs = hicr::json::getArray<nlohmann::json>(input, "Devices");
118 for (const auto &deviceJs : devicesJs) addDevice(std::make_shared<Device>(deviceJs));
119 }
120
121 if (input.contains("Metadata")) _metadata = input["Metadata"];
122 };
123
129 __INLINE__ static void verify(const nlohmann::json &input)
130 {
131 // Sanity checks
132 if (input.contains("Devices") == false) HICR_THROW_LOGIC("Serialized topology manager information is invalid, as it lacks the 'Devices' entry");
133 if (input["Devices"].is_array() == false) HICR_THROW_LOGIC("Serialized topology manager 'Devices' entry is not an array.");
134
135 for (auto device : input["Devices"])
136 {
137 if (device.contains("Type") == false) HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the 'Type' entry");
138 if (device["Type"].is_string() == false) HICR_THROW_LOGIC("Serialized device information is invalid, as the 'Type' entry is not a string");
139 }
140 };
141
154 [[nodiscard]] static __INLINE__ bool isSubset(const HiCR::Topology &topology1, const HiCR::Topology &topology2)
155 {
156 // Making a copy of the current topology.
157 // Devices will be removed as we match them with the given device
158 auto devices1 = topology1.getDevices();
159
161 const auto devices2 = topology2.getDevices();
162
163 for (const auto &device2 : devices2)
164 {
165 const auto device2Type = device2->getType();
166 const auto device2MemorySpaces = device2->getMemorySpaceList();
167 const auto device2ComputeResources = device2->getComputeResourceList();
168
169 // Iterating over all the current devices to see if one of them satisfies this given device
170 bool foundCompatibleDevice = false;
171 for (auto device1Itr = devices1.begin(); device1Itr != devices1.end() && foundCompatibleDevice == false; device1Itr++)
172 {
173 // Getting current device object
174 const auto &device1 = device1Itr.operator*();
175
176 // Checking type
177 const auto &device1Type = device1->getType();
178
179 // If this device is the same type as given, proceed to check compute resources and memory spaces
180 if (device1Type == device2Type)
181 {
182 // Flag to indicate this device is compatible with the request
183 bool deviceIsCompatible = true;
184
186 auto device1ComputeResources = device1->getComputeResourceList();
187
188 // Getting compute resources in this current device
189 for (const auto &givenComputeResource : device2ComputeResources)
190 {
191 bool foundComputeResource = false;
192 for (auto device1ComputeResourceItr = device1ComputeResources.begin(); device1ComputeResourceItr != device1ComputeResources.end() && foundComputeResource == false;
193 device1ComputeResourceItr++)
194 {
195 // Getting current device object
196 const auto &device1ComputeResource = device1ComputeResourceItr.operator*();
197
198 // If it's the same type as given
199 if (device1ComputeResource->getType() == givenComputeResource->getType())
200 {
201 // Set compute resource as found
202 foundComputeResource = true;
203
204 // Erase this element from the list to not re-use it
205 device1ComputeResources.erase(device1ComputeResourceItr);
206 }
207 }
208
209 // If no compute resource was found, abandon search in this device
210 if (foundComputeResource == false)
211 {
212 deviceIsCompatible = false;
213 break;
214 }
215 }
216
217 // If no suitable device was found, advance with the next one
218 if (deviceIsCompatible == false) continue;
219
221 auto device1MemorySpaces = device1->getMemorySpaceList();
222
223 // Getting compute resources in this current device
224 for (const auto &device2MemorySpace : device2MemorySpaces)
225 {
226 bool foundMemorySpace = false;
227 for (auto device1MemorySpaceItr = device1MemorySpaces.begin(); device1MemorySpaceItr != device1MemorySpaces.end() && foundMemorySpace == false; device1MemorySpaceItr++)
228 {
229 // Getting current device object
230 const auto &device1MemorySpace = device1MemorySpaceItr.operator*();
231
232 // If it's the same type as given
233 if (device1MemorySpace->getType() == device2MemorySpace->getType())
234 {
235 // Check whether the size is at least as big as given
236 if (device1MemorySpace->getSize() >= device2MemorySpace->getSize())
237 {
238 // Set compute resource as found
239 foundMemorySpace = true;
240
241 // Erase this element from the list to not re-use it
242 device1MemorySpaces.erase(device1MemorySpaceItr);
243 }
244 }
245 }
246
247 // If no compute resource was found, abandon search in this device
248 if (foundMemorySpace == false)
249 {
250 deviceIsCompatible = false;
251 break;
252 }
253 }
254
255 // If no suitable device was found, advance with the next one
256 if (deviceIsCompatible == false) continue;
257
258 // If we reached this point, we've found the device
259 foundCompatibleDevice = true;
260
261 // Deleting device to prevent it from being counted again
262 devices1.erase(device1Itr);
263
264 // Stop checking
265 break;
266 }
267 }
268
269 // If no current devices could satisfy the given device, return false now
270 if (foundCompatibleDevice == false) return false;
271 }
272
273 // All requirements have been met, returning true
274 //printf("Requirements met\n");
275 return true;
276 }
277
283 nlohmann::json &getMetadata() { return _metadata; }
284
290 [[nodiscard]] const nlohmann::json &getMetadata() const { return _metadata; }
291
297 void setMetadata(const nlohmann::json &metadata) { _metadata = metadata; }
298
299 private:
300
306 nlohmann::json _metadata;
307
311 deviceList_t _deviceList;
312};
313
314} // namespace HiCR
Definition topology.hpp:41
__INLINE__ void deserialize(const nlohmann::json &input)
Definition topology.hpp:113
__INLINE__ void addDevice(const std::shared_ptr< HiCR::Device > &device)
Definition topology.hpp:70
__INLINE__ const deviceList_t & getDevices() const
Definition topology.hpp:63
void setMetadata(const nlohmann::json &metadata)
Definition topology.hpp:297
__INLINE__ nlohmann::json serialize() const
Definition topology.hpp:88
__INLINE__ void merge(const Topology &source)
Definition topology.hpp:77
static __INLINE__ void verify(const nlohmann::json &input)
Definition topology.hpp:129
Topology(const nlohmann::json &input)
Definition topology.hpp:57
const nlohmann::json & getMetadata() const
Definition topology.hpp:290
static __INLINE__ bool isSubset(const HiCR::Topology &topology1, const HiCR::Topology &topology2)
Definition topology.hpp:154
nlohmann::json & getMetadata()
Definition topology.hpp:283
std::vector< std::shared_ptr< Device > > deviceList_t
Definition topology.hpp:47
Provides a base definition for a HiCR Device class.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67