/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
61 : HiCR::Device(){};
62
71 Device(const nlohmann::json &input)
72 : HiCR::Device()
73 {
74 deserialize(input);
75 }
76
80 ~Device() override = default;
81
82 protected:
83
84 [[nodiscard]] __INLINE__ std::string getType() const override { return "NUMA Domain"; }
85
86 private:
87
91 NUMADomainID_t _NUMADomainId{};
92
93 private:
94
95 __INLINE__ void serializeImpl(nlohmann::json &output) const override
96 {
97 // Storing numa domain identifier
98 output["NUMA Domain Id"] = _NUMADomainId;
99 }
100
101 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
102 {
103 // Getting device id
104 std::string key = "NUMA Domain Id";
105 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
106 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
107 _NUMADomainId = input[key].get<NUMADomainID_t>();
108
109 // Iterating over the compute resource list
110 for (const auto &computeResource : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
111 {
112 // Getting device type
113 const auto type = computeResource["Type"].get<std::string>();
114
115 // Checking whether the type is correct
116 if (type != "Processing Unit") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
117
118 // Deserializing new device
119 auto computeResourceObj = std::make_shared<hwloc::ComputeResource>(computeResource);
120
121 // Inserting device into the list
122 this->addComputeResource(computeResourceObj);
123 }
124
125 // Iterating over the memory space list
126 for (const auto &memorySpace : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
127 {
128 // Getting device type
129 const auto type = memorySpace["Type"].get<std::string>();
130
131 // Checking whether the type is correct
132 if (type != "RAM") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
133
134 // Deserializing new device
135 auto memorySpaceObj = std::make_shared<hwloc::MemorySpace>(memorySpace);
136
137 // Inserting device into the list
138 this->addMemorySpace(memorySpaceObj);
139 }
140 }
141};
142
143} // namespace HiCR::backend::hwloc
Definition device.hpp:53
__INLINE__ void deserialize(const nlohmann::json &input)
Definition device.hpp:152
std::vector< std::shared_ptr< ComputeResource > > computeResourceList_t
Definition device.hpp:59
std::vector< std::shared_ptr< MemorySpace > > memorySpaceList_t
Definition device.hpp:64
__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
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:71
Device()
Definition device.hpp:60
__INLINE__ std::string getType() const override
Definition device.hpp:84
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: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