/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 <hicr/core/device.hpp>
29
30namespace HiCR
31{
32
40{
41 public:
42
46 using deviceList_t = std::vector<std::shared_ptr<Device>>;
47
48 Topology() = default;
49 ~Topology() = default;
50
55 [[nodiscard]] __INLINE__ const deviceList_t &getDevices() const { return _deviceList; }
56
62 __INLINE__ void addDevice(const std::shared_ptr<HiCR::Device> &device) { _deviceList.push_back(device); }
63
69 __INLINE__ void merge(const Topology &source)
70 {
71 // Adding each device separately
72 for (const auto &d : source.getDevices()) addDevice(d);
73 }
74
80 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
81 {
82 // Storage for newly created serialized output
83 nlohmann::json output;
84
85 // Adding serialized devices information into the array
86 std::string devicesKey = "Devices";
87 output[devicesKey] = std::vector<nlohmann::json>();
88 for (const auto &device : _deviceList) output[devicesKey] += device->serialize();
89
90 // Adding metadata, if defined
91 output["Metadata"] = _metadata;
92
93 // Returning topology
94 return output;
95 }
96
102 __INLINE__ static void verify(const nlohmann::json &input)
103 {
104 // Sanity checks
105 if (input.contains("Devices") == false) HICR_THROW_LOGIC("Serialized topology manager information is invalid, as it lacks the 'Devices' entry");
106 if (input["Devices"].is_array() == false) HICR_THROW_LOGIC("Serialized topology manager 'Devices' entry is not an array.");
107
108 for (auto device : input["Devices"])
109 {
110 if (device.contains("Type") == false) HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the 'Type' entry");
111 if (device["Type"].is_string() == false) HICR_THROW_LOGIC("Serialized device information is invalid, as the 'Type' entry is not a string");
112 }
113 };
114
120 nlohmann::json &getMetadata() { return _metadata; }
121
127 [[nodiscard]] const nlohmann::json &getMetadata() const { return _metadata; }
128
134 void setMetadata(const nlohmann::json &metadata) { _metadata = metadata; }
135
136 private:
137
143 nlohmann::json _metadata;
144
148 deviceList_t _deviceList;
149};
150
151} // namespace HiCR
Definition topology.hpp:40
__INLINE__ void addDevice(const std::shared_ptr< HiCR::Device > &device)
Definition topology.hpp:62
__INLINE__ const deviceList_t & getDevices() const
Definition topology.hpp:55
void setMetadata(const nlohmann::json &metadata)
Definition topology.hpp:134
__INLINE__ nlohmann::json serialize() const
Definition topology.hpp:80
__INLINE__ void merge(const Topology &source)
Definition topology.hpp:69
static __INLINE__ void verify(const nlohmann::json &input)
Definition topology.hpp:102
const nlohmann::json & getMetadata() const
Definition topology.hpp:127
nlohmann::json & getMetadata()
Definition topology.hpp:120
std::vector< std::shared_ptr< Device > > deviceList_t
Definition topology.hpp:46
Provides a base definition for a HiCR Device class.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67