/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>
30#include <utility>
31
35constexpr std::string_view _HICR_DEVICE_COMPUTE_RESOURCES_KEY_ = "Compute Resources";
36
40constexpr std::string_view _HICR_DEVICE_MEMORY_SPACES_KEY_ = "Memory Spaces";
41
42namespace HiCR
43{
44
52class Device
53{
54 public:
55
59 using computeResourceList_t = std::vector<std::shared_ptr<ComputeResource>>;
60
64 using memorySpaceList_t = std::vector<std::shared_ptr<MemorySpace>>;
65
71 [[nodiscard]] virtual std::string getType() const = 0;
72
78 __INLINE__ const computeResourceList_t &getComputeResourceList() { return _computeResources; }
79
85 __INLINE__ const memorySpaceList_t &getMemorySpaceList() { return _memorySpaces; }
86
92 __INLINE__ void addComputeResource(const std::shared_ptr<HiCR::ComputeResource> &computeResource) { _computeResources.push_back(computeResource); }
93
99 __INLINE__ void addMemorySpace(const std::shared_ptr<HiCR::MemorySpace> &memorySpace) { _memorySpaces.push_back(memorySpace); }
100
104 virtual ~Device() = default;
105
112 Device(computeResourceList_t computeResources, memorySpaceList_t memorySpaces)
113 : _computeResources(std::move(computeResources)),
114 _memorySpaces(std::move(memorySpaces)){};
115
121 [[nodiscard]] __INLINE__ nlohmann::json serialize() const
122 {
123 // Storage for newly created serialized output
124 nlohmann::json output;
125
126 // Getting device type
127 output["Type"] = getType();
128
129 // Getting device-specific serialization information
130 serializeImpl(output);
131
132 // Serializing compute resource information
133 output[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_] = std::vector<nlohmann::json>();
134 for (const auto &computeResource : _computeResources) output[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_] += computeResource->serialize();
135
136 // Serializing memory space information
137 output[_HICR_DEVICE_MEMORY_SPACES_KEY_] = std::vector<nlohmann::json>();
138 for (const auto &memorySpace : _memorySpaces) output[_HICR_DEVICE_MEMORY_SPACES_KEY_] += memorySpace->serialize();
139
140 // Returning serialized information
141 return output;
142 }
143
152 __INLINE__ void deserialize(const nlohmann::json &input)
153 {
154 // First, discard all existing information
155 _computeResources.clear();
156 _memorySpaces.clear();
157
158 // Sanity checks
159 if (input.contains(_HICR_DEVICE_COMPUTE_RESOURCES_KEY_) == false)
160 HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the '%s' entry", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
161 if (input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_].is_array() == false)
162 HICR_THROW_LOGIC("Serialized device information is invalid, as '%s' entry is not an array.", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
163 for (const auto &c : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
164 {
165 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_);
166 if (c["Type"].is_string() == false)
167 HICR_THROW_LOGIC("In '%s', entry information information is invalid, as the 'Type' entry is not a string", _HICR_DEVICE_COMPUTE_RESOURCES_KEY_);
168 }
169
170 if (input.contains(_HICR_DEVICE_MEMORY_SPACES_KEY_) == false)
171 HICR_THROW_LOGIC("Serialized device information is invalid, as it lacks the '%s' entry", _HICR_DEVICE_MEMORY_SPACES_KEY_);
172 if (input[_HICR_DEVICE_MEMORY_SPACES_KEY_].is_array() == false)
173 HICR_THROW_LOGIC("Serialized device information is invalid, as '%s' entry is not an array.", _HICR_DEVICE_MEMORY_SPACES_KEY_);
174 for (const auto &c : input[_HICR_DEVICE_MEMORY_SPACES_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_MEMORY_SPACES_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_MEMORY_SPACES_KEY_);
179 }
180
181 // Then call the backend-specific deserialization function
182 deserializeImpl(input);
183
184 // Checking whether the deserialization was successful
185 if (_computeResources.size() != input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_].size())
186 HICR_THROW_LOGIC("Deserialization failed, as the number of compute resources created (%lu) differs from the ones provided in the serialized input (%lu)",
187 _memorySpaces.size(),
189 if (_memorySpaces.size() != input[_HICR_DEVICE_MEMORY_SPACES_KEY_].size())
190 HICR_THROW_LOGIC("Deserialization failed, as the number of memory spaces created (%lu) differs from the ones provided in the serialized input (%lu)",
191 _memorySpaces.size(),
192 input[_HICR_DEVICE_MEMORY_SPACES_KEY_].size());
193 };
194
195 protected:
196
200 Device() = default;
201
207 virtual void serializeImpl(nlohmann::json &output) const = 0;
208
214 virtual void deserializeImpl(const nlohmann::json &input) = 0;
215
216 private:
217
221 computeResourceList_t _computeResources;
222
226 memorySpaceList_t _memorySpaces;
227};
228
229} // namespace HiCR
Definition device.hpp:53
__INLINE__ nlohmann::json serialize() const
Definition device.hpp:121
virtual ~Device()=default
Device(computeResourceList_t computeResources, memorySpaceList_t memorySpaces)
Definition device.hpp:112
__INLINE__ const memorySpaceList_t & getMemorySpaceList()
Definition device.hpp:85
virtual void serializeImpl(nlohmann::json &output) const =0
__INLINE__ void deserialize(const nlohmann::json &input)
Definition device.hpp:152
std::vector< std::shared_ptr< ComputeResource > > computeResourceList_t
Definition device.hpp:59
virtual std::string getType() const =0
std::vector< std::shared_ptr< MemorySpace > > memorySpaceList_t
Definition device.hpp:64
virtual void deserializeImpl(const nlohmann::json &input)=0
Device()=default
__INLINE__ void addMemorySpace(const std::shared_ptr< HiCR::MemorySpace > &memorySpace)
Definition device.hpp:99
__INLINE__ void addComputeResource(const std::shared_ptr< HiCR::ComputeResource > &computeResource)
Definition device.hpp:92
__INLINE__ const computeResourceList_t & getComputeResourceList()
Definition device.hpp:78
Provides a base definition for a HiCR ComputeResource class.
constexpr std::string_view _HICR_DEVICE_MEMORY_SPACES_KEY_
Definition device.hpp:40
constexpr std::string_view _HICR_DEVICE_COMPUTE_RESOURCES_KEY_
Definition device.hpp:35
Provides a base definition for a HiCR MemorySpace class.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67