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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/hwloc/computeResource.hpp Source File
HiCR
computeResource.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 <unordered_set>
27#include <hwloc.h>
29#include <hicr/core/definitions.hpp>
32
33namespace HiCR::backend::hwloc
34{
35
41{
42 public:
43
47 using logicalProcessorId_t = unsigned int;
48
52 using physicalProcessorId_t = unsigned int;
53
57 using numaAffinity_t = unsigned int;
58
64 ComputeResource(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
65 : HiCR::ComputeResource(),
66 _logicalProcessorId(logicalProcessorId),
67 _physicalProcessorId(detectPhysicalProcessorId(topology, logicalProcessorId)),
68 _numaAffinity(detectCoreNUMAffinity(topology, logicalProcessorId)),
69 _caches(detectCpuCaches(topology, logicalProcessorId))
70 {
71 _type = "Processing Unit";
72 };
73
81 ComputeResource(const logicalProcessorId_t logicalProcessorId,
82 const physicalProcessorId_t physicalProcessorId,
83 const numaAffinity_t numaAffinity,
84 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> caches)
85 : HiCR::ComputeResource(),
86 _logicalProcessorId(logicalProcessorId),
87 _physicalProcessorId(physicalProcessorId),
88 _numaAffinity(numaAffinity),
89 _caches(std::move(caches))
90 {
91 _type = "Processing Unit";
92 };
93
94 ~ComputeResource() override = default;
95
99 ComputeResource() { _type = "Processing Unit"; };
100
106 __INLINE__ logicalProcessorId_t getProcessorId() const { return _logicalProcessorId; }
107
114 __INLINE__ physicalProcessorId_t getPhysicalProcessorId() const { return _physicalProcessorId; }
115
124 __INLINE__ static void detectThreadPUs(hwloc_topology_t topology, hwloc_obj_t obj, int depth, std::vector<logicalProcessorId_t> &threadPUs)
125 {
126 if (obj->arity == 0) threadPUs.push_back(obj->logical_index);
127 for (unsigned int i = 0; i < obj->arity; i++) detectThreadPUs(topology, obj->children[i], depth + 1, threadPUs);
128 }
129
137 __INLINE__ static physicalProcessorId_t detectPhysicalProcessorId(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
138 {
139 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
140 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
141
142 // Acquire the parent core object
143 // There is an asumption here that a HWLOC_OBJ_PU type always has a parent of type HWLOC_OBJ_CORE,
144 // which is consistent with current HWloc, but maybe reconsider it.
145 obj = obj->parent;
146 if (obj->type != HWLOC_OBJ_CORE) HICR_THROW_RUNTIME("Unexpected hwloc object type while trying to access Core/CPU (%lu)", logicalProcessorId);
147
148 return obj->logical_index;
149 }
150
158 __INLINE__ static numaAffinity_t detectCoreNUMAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
159 {
160 // Sanitize input? So far we only call it internally so assume ID given is safe?
161 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
162
163 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
164
165 size_t ret = 0;
166
167 // obj is a leaf/PU; get to its parents to discover the hwloc memory space it belongs to
168 hwloc_obj_t ancestor = obj->parent;
169 hwloc_obj_t nodeNUMA = nullptr;
170 bool found = false;
171
172 // iterate over parents until we find a memory node
173 while (ancestor && !ancestor->memory_arity) ancestor = ancestor->parent;
174
175 // iterate over potential sibling nodes (the likely behavior though is to run only once)
176 for (size_t memChild = 0; memChild < ancestor->memory_arity; memChild++)
177 {
178 if (memChild == 0)
179 nodeNUMA = ancestor->memory_first_child;
180 else if (nodeNUMA)
181 nodeNUMA = nodeNUMA->next_sibling;
182
183 if (hwloc_obj_type_is_memory(nodeNUMA->type) && hwloc_bitmap_isset(obj->nodeset, nodeNUMA->os_index))
184 {
185 found = true;
186 ret = nodeNUMA->logical_index;
187 break;
188 }
189 }
190
191 if (!found) HICR_THROW_RUNTIME("NUMA Domain not detected for compute resource (%lu)", logicalProcessorId);
192
193 return ret;
194 }
195
210 __INLINE__ static std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> detectCpuCaches(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
211 {
212 // Sanitize input? So far we only call it internally so assume ID given is safe?
213 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
214
215 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
216
217 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> ret;
218
219 // Start from 1 level above our leaf/PU
220 hwloc_obj_t cache = obj->parent;
221 while (cache)
222 {
224 std::string type;
225
226 // Check if the current object is a cache-type object
227 if (cache->type == HWLOC_OBJ_L1CACHE || cache->type == HWLOC_OBJ_L2CACHE || cache->type == HWLOC_OBJ_L3CACHE || cache->type == HWLOC_OBJ_L4CACHE ||
228 cache->type == HWLOC_OBJ_L5CACHE || cache->type == HWLOC_OBJ_L1ICACHE || cache->type == HWLOC_OBJ_L2ICACHE || cache->type == HWLOC_OBJ_L3ICACHE)
229 {
230 // In case it is a cache, deduce the level from the types HWloc supports
231 switch (cache->type)
232 {
233 case HWLOC_OBJ_L1CACHE:
234 case HWLOC_OBJ_L1ICACHE: level = Cache::cacheLevel_t::L1; break;
235 case HWLOC_OBJ_L2CACHE:
236 case HWLOC_OBJ_L2ICACHE: level = Cache::cacheLevel_t::L2; break;
237 case HWLOC_OBJ_L3CACHE:
238 case HWLOC_OBJ_L3ICACHE: level = Cache::cacheLevel_t::L3; break;
239 case HWLOC_OBJ_L4CACHE: level = Cache::cacheLevel_t::L4; break;
240 case HWLOC_OBJ_L5CACHE: level = Cache::cacheLevel_t::L5; break;
241 // We never expect to get here; this is for compiler warning suppresion
242 default: HICR_THROW_RUNTIME("Unsupported Cache level detected (%lu)", cache->type);
243 }
244
245 // Storage for cache type
246 std::string type = "Unknown";
247
248 // Discover the type: Instruction, Data or Unified
249 switch (cache->attr->cache.type)
250 {
251 case HWLOC_OBJ_CACHE_UNIFIED: type = "Unified"; break;
252 case HWLOC_OBJ_CACHE_INSTRUCTION: type = "Instruction"; break;
253 case HWLOC_OBJ_CACHE_DATA: type = "Data"; break;
254 }
255
256 // Storage for more cache information
257 const bool shared = cache->arity > 1;
258 const auto size = cache->attr->cache.size;
259 const auto lineSize = cache->attr->cache.linesize;
260
261 // Insert element to our return container
262 ret.insert(std::make_shared<backend::hwloc::Cache>(level, type, size, lineSize, shared));
263 }
264
265 // Repeat the search 1 level above
266 cache = cache->parent;
267 }
268
269 return ret;
270 }
271
279 __INLINE__ static numaAffinity_t getCpuNumaAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
280 {
281 // Sanitize input? So far we only call it internally so assume ID given is safe?
282 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
283
284 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
285
286 numaAffinity_t ret = 0;
287
288 // obj is a leaf/PU; get to its parents to discover the hwloc memory space it belongs to
289 hwloc_obj_t ancestor = obj->parent;
290 hwloc_obj_t nodeNUMA = nullptr;
291 bool found = false;
292
293 // iterate over parents until we find a memory node
294 while (ancestor && !ancestor->memory_arity) ancestor = ancestor->parent;
295
296 // iterate over potential sibling nodes (the likely behavior though is to run only once)
297 for (size_t memChild = 0; memChild < ancestor->memory_arity; memChild++)
298 {
299 if (memChild == 0)
300 nodeNUMA = ancestor->memory_first_child;
301 else if (nodeNUMA)
302 nodeNUMA = nodeNUMA->next_sibling;
303
304 if (hwloc_obj_type_is_memory(nodeNUMA->type) && hwloc_bitmap_isset(obj->nodeset, nodeNUMA->os_index))
305 {
306 found = true;
307 ret = (numaAffinity_t)nodeNUMA->logical_index;
308 break;
309 }
310 }
311
312 if (found == false) HICR_THROW_RUNTIME("NUMA Domain not detected for compute resource (%lu)", logicalProcessorId);
313
314 return ret;
315 }
316
317 protected:
318
319 __INLINE__ void serializeImpl(nlohmann::json &output) const override
320 {
321 // Writing core's information into the serialized object
322 output["Logical Processor Id"] = _logicalProcessorId;
323 output["Physical Processor Id"] = _physicalProcessorId;
324 output["NUMA Affinity"] = _numaAffinity;
325
326 // Writing Cache information
327 std::string cachesKey = "Caches";
328 output[cachesKey] = std::vector<nlohmann::json>();
329 for (const auto &cache : _caches) output[cachesKey] += cache->serialize();
330 }
331
332 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
333 {
334 // Checking whether the type is correct
335 if (_type != "Processing Unit") HICR_THROW_LOGIC("The passed compute resource type '%s' is not compatible with this topology manager", _type.c_str());
336
337 std::string key = "Logical Processor Id";
338 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
339 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
340 _logicalProcessorId = input[key].get<logicalProcessorId_t>();
341
342 key = "Physical Processor Id";
343 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
344 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
345 _physicalProcessorId = input[key].get<physicalProcessorId_t>();
346
347 key = "NUMA Affinity";
348 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
349 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
350 _numaAffinity = input[key].get<numaAffinity_t>();
351
352 key = "Caches";
353 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
354 if (input[key].is_array() == false) HICR_THROW_LOGIC("The '%s' entry is not an array", key.c_str());
355
356 _caches.clear();
357 for (const auto &c : input[key])
358 {
359 // Deserializing cache
360 auto cache = std::make_shared<backend::hwloc::Cache>(c);
361
362 // Adding it to the list
363 _caches.insert(cache);
364 }
365 }
366
367 private:
368
372 logicalProcessorId_t _logicalProcessorId{};
373
379 physicalProcessorId_t _physicalProcessorId{};
380
384 numaAffinity_t _numaAffinity{};
385
390 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> _caches;
391};
392
393} // namespace HiCR::backend::hwloc
Defines the Cache class for interacting with the host (CPUs) device type.
Definition computeResource.hpp:40
std::string _type
Definition computeResource.hpp:109
cacheLevel_t
Definition cache.hpp:46
@ L1
Cache Level L1.
Definition cache.hpp:48
@ L3
Cache Level L3.
Definition cache.hpp:54
@ L2
Cache Level L2.
Definition cache.hpp:51
@ L4
Cache Level L4.
Definition cache.hpp:57
@ L5
Cache Level L5.
Definition cache.hpp:60
Definition computeResource.hpp:41
__INLINE__ void serializeImpl(nlohmann::json &output) const override
Definition computeResource.hpp:319
__INLINE__ void deserializeImpl(const nlohmann::json &input) override
Definition computeResource.hpp:332
ComputeResource(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:64
unsigned int logicalProcessorId_t
Definition computeResource.hpp:47
static __INLINE__ physicalProcessorId_t detectPhysicalProcessorId(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:137
static __INLINE__ std::unordered_set< std::shared_ptr< backend::hwloc::Cache > > detectCpuCaches(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:210
unsigned int numaAffinity_t
Definition computeResource.hpp:57
ComputeResource()
Definition computeResource.hpp:99
unsigned int physicalProcessorId_t
Definition computeResource.hpp:52
ComputeResource(const logicalProcessorId_t logicalProcessorId, const physicalProcessorId_t physicalProcessorId, const numaAffinity_t numaAffinity, std::unordered_set< std::shared_ptr< backend::hwloc::Cache > > caches)
Definition computeResource.hpp:81
static __INLINE__ numaAffinity_t detectCoreNUMAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:158
__INLINE__ logicalProcessorId_t getProcessorId() const
Definition computeResource.hpp:106
__INLINE__ physicalProcessorId_t getPhysicalProcessorId() const
Definition computeResource.hpp:114
static __INLINE__ numaAffinity_t getCpuNumaAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:279
static __INLINE__ void detectThreadPUs(hwloc_topology_t topology, hwloc_obj_t obj, int depth, std::vector< logicalProcessorId_t > &threadPUs)
Definition computeResource.hpp:124
Provides a base definition for a HiCR ComputeResource class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67