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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/hwloc/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
24#pragma once
25
26#include <hicr/core/definitions.hpp>
27#include <hicr/core/device.hpp>
28#include "computeResource.hpp"
29#include "memorySpace.hpp"
30
31namespace HiCR::backend::hwloc
32{
33
37class Device final : public HiCR::Device
38{
39 public:
40
44 using NUMADomainID_t = unsigned int;
45
53 Device(const NUMADomainID_t NUMADomainId, const computeResourceList_t &computeResources, const memorySpaceList_t &memorySpaces)
54 : HiCR::Device(computeResources, memorySpaces),
55 _NUMADomainId(NUMADomainId)
56 {
57 _type = "NUMA Domain";
58 };
59
64 : HiCR::Device()
65 {
66 _type = "NUMA Domain";
67 };
68
77 Device(const nlohmann::json &input)
78 : HiCR::Device()
79 {
80 deserialize(input);
81 }
82
86 ~Device() override = default;
87
88 private:
89
93 NUMADomainID_t _NUMADomainId{};
94
95 private:
96
97 __INLINE__ void serializeImpl(nlohmann::json &output) const override
98 {
99 // Storing numa domain identifier
100 output["NUMA Domain Id"] = _NUMADomainId;
101 }
102
103 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
104 {
105 // Getting device id
106 std::string key = "NUMA Domain Id";
107 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
108 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
109 _NUMADomainId = input[key].get<NUMADomainID_t>();
110
111 // Iterating over the compute resource list
112 for (const auto &computeResource : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
113 {
114 // Getting device type
115 const auto type = computeResource["Type"].get<std::string>();
116
117 // Deserializing new device
118 auto computeResourceObj = std::make_shared<HiCR::ComputeResource>(computeResource);
119
120 // Inserting device into the list
121 this->addComputeResource(computeResourceObj);
122 }
123
124 // Iterating over the memory space list
125 for (const auto &memorySpace : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
126 {
127 // Getting device type
128 const auto type = memorySpace["Type"].get<std::string>();
129
130 // Deserializing new device
131 auto memorySpaceObj = std::make_shared<HiCR::MemorySpace>(memorySpace);
132
133 // Inserting device into the list
134 this->addMemorySpace(memorySpaceObj);
135 }
136 }
137};
138
139} // namespace HiCR::backend::hwloc
Definition device.hpp:54
__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
__INLINE__ void addMemorySpace(const std::shared_ptr< HiCR::MemorySpace > &memorySpace)
Definition device.hpp:122
__INLINE__ void addComputeResource(const std::shared_ptr< HiCR::ComputeResource > &computeResource)
Definition device.hpp:115
std::string _type
Definition device.hpp:253
Definition device.hpp:38
Device(const NUMADomainID_t NUMADomainId, const computeResourceList_t &computeResources, const memorySpaceList_t &memorySpaces)
Definition device.hpp:53
unsigned int NUMADomainID_t
Definition device.hpp:44
~Device() override=default
Device(const nlohmann::json &input)
Definition device.hpp:77
Device()
Definition device.hpp:63
Provides a base definition for a HiCR ComputeResource class.
Provides a base definition for a HiCR Device 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