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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/device.hpp Source File
HiCR
device.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
23#pragma once
24
25#include <memory>
26#include <unordered_set>
27#include <nlohmann_json/json.hpp>
28#include <nlohmann_json/parser.hpp>
31#include <utility>
32
36constexpr std::string_view _HICR_DEVICE_COMPUTE_RESOURCES_KEY_ = "Compute Resources";
37
41constexpr std::string_view _HICR_DEVICE_MEMORY_SPACES_KEY_ = "Memory Spaces";
42
43namespace HiCR
44{
45
53class Device
54{
55 public:
56
60 using computeResourceList_t = std::vector<std::shared_ptr<ComputeResource>>;
61
65 using memorySpaceList_t = std::vector<std::shared_ptr<MemorySpace>>;
66
72 Device(const nlohmann::json &input) { deserialize(input); }
73
77 virtual ~Device() = default;
78
85 Device(computeResourceList_t computeResources, memorySpaceList_t memorySpaces)
86 : _computeResources(std::move(computeResources)),
87 _memorySpaces(std::move(memorySpaces)){};
88
94 [[nodiscard]] __INLINE__ std::string getType() const { return _type; };
95
101 __INLINE__ const computeResourceList_t &getComputeResourceList() { return _computeResources; }
102
108 __INLINE__ const memorySpaceList_t &getMemorySpaceList() { return _memorySpaces; }
109
115 __INLINE__ void addComputeResource(const std::shared_ptr<HiCR::ComputeResource> &computeResource) { _computeResources.push_back(computeResource); }
116
122 __INLINE__ void addMemorySpace(const std::shared_ptr<HiCR::MemorySpace> &memorySpace) { _memorySpaces.push_back(memorySpace); }
123
129 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
130 {
131 // Storage for newly created serialized output
132 nlohmann::json output;
133
134 // Getting device type
135 output["Type"] = _type;
136
137 // Getting device-specific serialization information
138 serializeImpl(output);
139
140 // Serializing compute resource information
141 output[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_] = std::vector<nlohmann::json>();
142 for (const auto &computeResource : _computeResources) output[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_] += computeResource->serialize();
143
144 // Serializing memory space information
145 output[_HICR_DEVICE_MEMORY_SPACES_KEY_] = std::vector<nlohmann::json>();
146 for (const auto &memorySpace : _memorySpaces) output[_HICR_DEVICE_MEMORY_SPACES_KEY_] += memorySpace->serialize();
147
148 // Returning serialized information
149 return output;
150 }
151
160 __INLINE__ void deserialize(const nlohmann::json &input)
161 {
162 // Setting device type
163 _type = hicr::json::getString(input, "Type");
164
165 // First, discard all existing information
166 _computeResources.clear();
167 _memorySpaces.clear();
168
169 // Sanity checks
170 if (input.contains(_HICR_DEVICE_COMPUTE_RESOURCES_KEY_) == false)
171 HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the '%s' entry", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
172 if (input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_].is_array() == false)
173 HICR_THROW_LOGIC("Serialized device information is invalid, as '%s' entry is not an array.", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
174 for (const auto &c : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
175 {
176 if (c.contains("Type") == false) HICR_THROW_LOGIC("In '%s', entry information is invalid, as it lacks the 'Type' entry", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
177 if (c["Type"].is_string() == false)
178 HICR_THROW_LOGIC("In '%s', entry information information is invalid, as the 'Type' entry is not a string", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
179 }
180
181 if (input.contains(_HICR_DEVICE_MEMORY_SPACES_KEY_) == false)
182 HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the '%s' entry", _HICR_DEVICE_MEMORY_SPACES_KEY_);
183 if (input[_HICR_DEVICE_MEMORY_SPACES_KEY_].is_array() == false)
184 HICR_THROW_LOGIC("Serialized device information is invalid, as '%s' entry is not an array.", _HICR_DEVICE_MEMORY_SPACES_KEY_);
185 for (const auto &c : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
186 {
187 if (c.contains("Type") == false) HICR_THROW_LOGIC("In '%s', entry information is invalid, as it lacks the 'Type' entry", _HICR_DEVICE_MEMORY_SPACES_KEY_);
188 if (c["Type"].is_string() == false)
189 HICR_THROW_LOGIC("In '%s', entry information information is invalid, as the 'Type' entry is not a string", _HICR_DEVICE_MEMORY_SPACES_KEY_);
190 }
191
192 // Then call the backend-specific deserialization function
193 deserializeImpl(input);
194
195 // Checking whether the deserialization was successful
196 if (_computeResources.size() != input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_].size())
197 HICR_THROW_LOGIC("Deserialization failed, as the number of compute resources created (%lu) differs from the ones provided in the serialized input (%lu)",
198 _memorySpaces.size(),
200 if (_memorySpaces.size() != input[_HICR_DEVICE_MEMORY_SPACES_KEY_].size())
201 HICR_THROW_LOGIC("Deserialization failed, as the number of memory spaces created (%lu) differs from the ones provided in the serialized input (%lu)",
202 _memorySpaces.size(),
203 input[_HICR_DEVICE_MEMORY_SPACES_KEY_].size());
204 };
205
206 protected:
207
211 Device() = default;
212
218 virtual void serializeImpl(nlohmann::json &output) const {}
219
225 virtual void deserializeImpl(const nlohmann::json &input)
226 {
227 // Iterating over the compute resource list
228 for (const auto &computeResource : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
229 {
230 // Deserializing new compute resource
231 auto computeResourceObj = std::make_shared<ComputeResource>(computeResource);
232
233 // Inserting device into the list
234 this->addComputeResource(computeResourceObj);
235 }
236
237 // Iterating over the memory space list
238 for (const auto &memorySpace : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
239 {
240 // Deserializing new memory space
241 auto memorySpaceObj = std::make_shared<MemorySpace>(memorySpace);
242
243 // Inserting device into the list
244 this->addMemorySpace(memorySpaceObj);
245 }
246 }
247
248 protected:
249
253 std::string _type;
254
255 private:
256
260 computeResourceList_t _computeResources;
261
265 memorySpaceList_t _memorySpaces;
266};
267
268} // namespace HiCR
Definition device.hpp:54
__INLINE__ nlohmann::json serialize() const
Definition device.hpp:129
virtual void serializeImpl(nlohmann::json &output) const
Definition device.hpp:218
virtual ~Device()=default
Device(computeResourceList_t computeResources, memorySpaceList_t memorySpaces)
Definition device.hpp:85
__INLINE__ const memorySpaceList_t & getMemorySpaceList()
Definition device.hpp:108
__INLINE__ void deserialize(const nlohmann::json &input)
Definition device.hpp:160
std::vector< std::shared_ptr< ComputeResource > > computeResourceList_t
Definition device.hpp:60
std::vector< std::shared_ptr< MemorySpace > > memorySpaceList_t
Definition device.hpp:65
Device(const nlohmann::json &input)
Definition device.hpp:72
Device()=default
__INLINE__ void addMemorySpace(const std::shared_ptr< HiCR::MemorySpace > &memorySpace)
Definition device.hpp:122
__INLINE__ std::string getType() const
Definition device.hpp:94
__INLINE__ void addComputeResource(const std::shared_ptr< HiCR::ComputeResource > &computeResource)
Definition device.hpp:115
std::string _type
Definition device.hpp:253
virtual void deserializeImpl(const nlohmann::json &input)
Definition device.hpp:225
__INLINE__ const computeResourceList_t & getComputeResourceList()
Definition device.hpp:101
Provides a base definition for a HiCR ComputeResource class.
constexpr std::string_view _HICR_DEVICE_MEMORY_SPACES_KEY_
Definition device.hpp:41
constexpr std::string_view _HICR_DEVICE_COMPUTE_RESOURCES_KEY_
Definition device.hpp:36
Provides a base definition for a HiCR MemorySpace class.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67