/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 hwlocObjectIndex_t = unsigned int;
48
52 using logicalProcessorId_t = unsigned int;
53
57 using physicalProcessorId_t = unsigned int;
58
62 using numaAffinity_t = unsigned int;
63
69 ComputeResource(hwloc_topology_t topology, const hwlocObjectIndex_t hwlocObjectIndex)
70 : HiCR::ComputeResource(),
71 _logicalProcessorId(detectLogicalProcessorId(topology, hwlocObjectIndex)),
72 _physicalProcessorId(detectPhysicalProcessorId(topology, hwlocObjectIndex)),
73 _numaAffinity(detectCoreNUMAffinity(topology, hwlocObjectIndex)),
74 _caches(detectCpuCaches(topology, hwlocObjectIndex))
75 {
76 _type = "Processing Unit";
77 };
78
87 ComputeResource(const hwlocObjectIndex_t hwlocObjectIndex,
88 const logicalProcessorId_t logicalProcessorId,
89 const physicalProcessorId_t physicalProcessorId,
90 const numaAffinity_t numaAffinity,
91 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> caches)
92 : HiCR::ComputeResource(),
93 _logicalProcessorId(logicalProcessorId),
94 _physicalProcessorId(physicalProcessorId),
95 _numaAffinity(numaAffinity),
96 _caches(caches)
97 {
98 _type = "Processing Unit";
99 };
100
101 ~ComputeResource() override = default;
102
106 ComputeResource() { _type = "Processing Unit"; };
107
113 __INLINE__ logicalProcessorId_t getProcessorId() const { return _logicalProcessorId; }
114
121 __INLINE__ physicalProcessorId_t getPhysicalProcessorId() const { return _physicalProcessorId; }
122
131 __INLINE__ static void detectThreadPUs(hwloc_topology_t topology, hwloc_obj_t obj, int depth, std::vector<logicalProcessorId_t> &threadPUs)
132 {
133 if (obj->arity == 0) threadPUs.push_back(obj->logical_index);
134 for (unsigned int i = 0; i < obj->arity; i++) detectThreadPUs(topology, obj->children[i], depth + 1, threadPUs);
135 }
136
144 __INLINE__ static logicalProcessorId_t detectLogicalProcessorId(hwloc_topology_t topology, const hwlocObjectIndex_t objectId)
145 {
146 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, objectId);
147 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%u) in this backend", objectId);
148
149 return obj->logical_index;
150 }
151
159 __INLINE__ static physicalProcessorId_t detectPhysicalProcessorId(hwloc_topology_t topology, const hwlocObjectIndex_t objectId)
160 {
161 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, objectId);
162 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%u) in this backend", objectId);
163
164 return obj->os_index;
165 }
166
174 __INLINE__ static numaAffinity_t detectCoreNUMAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
175 {
176 // Sanitize input? So far we only call it internally so assume ID given is safe?
177 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
178
179 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
180
181 size_t ret = 0;
182
183 // obj is a leaf/PU; get to its parents to discover the hwloc memory space it belongs to
184 hwloc_obj_t ancestor = obj->parent;
185 hwloc_obj_t nodeNUMA = nullptr;
186 bool found = false;
187
188 // iterate over parents until we find a memory node
189 while (ancestor && !ancestor->memory_arity) ancestor = ancestor->parent;
190
191 // iterate over potential sibling nodes (the likely behavior though is to run only once)
192 for (size_t memChild = 0; memChild < ancestor->memory_arity; memChild++)
193 {
194 if (memChild == 0)
195 nodeNUMA = ancestor->memory_first_child;
196 else if (nodeNUMA)
197 nodeNUMA = nodeNUMA->next_sibling;
198
199 if (hwloc_obj_type_is_memory(nodeNUMA->type) && hwloc_bitmap_isset(obj->nodeset, nodeNUMA->os_index))
200 {
201 found = true;
202 ret = nodeNUMA->logical_index;
203 break;
204 }
205 }
206
207 if (!found) HICR_THROW_RUNTIME("NUMA Domain not detected for compute resource (%lu)", logicalProcessorId);
208
209 return ret;
210 }
211
226 __INLINE__ static std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> detectCpuCaches(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
227 {
228 // Sanitize input? So far we only call it internally so assume ID given is safe?
229 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
230
231 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
232
233 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> ret;
234
235 // Start from 1 level above our leaf/PU
236 hwloc_obj_t cache = obj->parent;
237 while (cache)
238 {
240 std::string type;
241
242 // Check if the current object is a cache-type object
243 if (cache->type == HWLOC_OBJ_L1CACHE || cache->type == HWLOC_OBJ_L2CACHE || cache->type == HWLOC_OBJ_L3CACHE || cache->type == HWLOC_OBJ_L4CACHE ||
244 cache->type == HWLOC_OBJ_L5CACHE || cache->type == HWLOC_OBJ_L1ICACHE || cache->type == HWLOC_OBJ_L2ICACHE || cache->type == HWLOC_OBJ_L3ICACHE)
245 {
246 // In case it is a cache, deduce the level from the types HWloc supports
247 switch (cache->type)
248 {
249 case HWLOC_OBJ_L1CACHE:
250 case HWLOC_OBJ_L1ICACHE: level = Cache::cacheLevel_t::L1; break;
251 case HWLOC_OBJ_L2CACHE:
252 case HWLOC_OBJ_L2ICACHE: level = Cache::cacheLevel_t::L2; break;
253 case HWLOC_OBJ_L3CACHE:
254 case HWLOC_OBJ_L3ICACHE: level = Cache::cacheLevel_t::L3; break;
255 case HWLOC_OBJ_L4CACHE: level = Cache::cacheLevel_t::L4; break;
256 case HWLOC_OBJ_L5CACHE: level = Cache::cacheLevel_t::L5; break;
257 // We never expect to get here; this is for compiler warning suppresion
258 default: HICR_THROW_RUNTIME("Unsupported Cache level detected (%lu)", cache->type);
259 }
260
261 // Storage for cache type
262 std::string type = "Unknown";
263
264 // Discover the type: Instruction, Data or Unified
265 switch (cache->attr->cache.type)
266 {
267 case HWLOC_OBJ_CACHE_UNIFIED: type = "Unified"; break;
268 case HWLOC_OBJ_CACHE_INSTRUCTION: type = "Instruction"; break;
269 case HWLOC_OBJ_CACHE_DATA: type = "Data"; break;
270 }
271
272 // Storage for more cache information
273 const bool shared = cache->arity > 1;
274 const auto size = cache->attr->cache.size;
275 const auto lineSize = cache->attr->cache.linesize;
276
277 // Insert element to our return container
278 ret.insert(std::make_shared<backend::hwloc::Cache>(level, type, size, lineSize, shared));
279 }
280
281 // Repeat the search 1 level above
282 cache = cache->parent;
283 }
284
285 return ret;
286 }
287
295 __INLINE__ static numaAffinity_t getCpuNumaAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
296 {
297 // Sanitize input? So far we only call it internally so assume ID given is safe?
298 hwloc_obj_t obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, logicalProcessorId);
299
300 if (!obj) HICR_THROW_RUNTIME("Attempting to access a compute resource that does not exist (%lu) in this backend", logicalProcessorId);
301
302 numaAffinity_t ret = 0;
303
304 // obj is a leaf/PU; get to its parents to discover the hwloc memory space it belongs to
305 hwloc_obj_t ancestor = obj->parent;
306 hwloc_obj_t nodeNUMA = nullptr;
307 bool found = false;
308
309 // iterate over parents until we find a memory node
310 while (ancestor && !ancestor->memory_arity) ancestor = ancestor->parent;
311
312 // iterate over potential sibling nodes (the likely behavior though is to run only once)
313 for (size_t memChild = 0; memChild < ancestor->memory_arity; memChild++)
314 {
315 if (memChild == 0)
316 nodeNUMA = ancestor->memory_first_child;
317 else if (nodeNUMA)
318 nodeNUMA = nodeNUMA->next_sibling;
319
320 if (hwloc_obj_type_is_memory(nodeNUMA->type) && hwloc_bitmap_isset(obj->nodeset, nodeNUMA->os_index))
321 {
322 found = true;
323 ret = (numaAffinity_t)nodeNUMA->logical_index;
324 break;
325 }
326 }
327
328 if (found == false) HICR_THROW_RUNTIME("NUMA Domain not detected for compute resource (%lu)", logicalProcessorId);
329
330 return ret;
331 }
332
333 protected:
334
335 __INLINE__ void serializeImpl(nlohmann::json &output) const override
336 {
337 // Writing core's information into the serialized object
338 output["Logical Processor Id"] = _logicalProcessorId;
339 output["Physical Processor Id"] = _physicalProcessorId;
340 output["NUMA Affinity"] = _numaAffinity;
341
342 // Writing Cache information
343 std::string cachesKey = "Caches";
344 output[cachesKey] = std::vector<nlohmann::json>();
345 for (const auto &cache : _caches) output[cachesKey] += cache->serialize();
346 }
347
348 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
349 {
350 // Checking whether the type is correct
351 if (_type != "Processing Unit") HICR_THROW_LOGIC("The passed compute resource type '%s' is not compatible with this topology manager", _type.c_str());
352
353 std::string key = "Logical Processor Id";
354 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
355 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
356 _logicalProcessorId = input[key].get<logicalProcessorId_t>();
357
358 key = "Physical Processor Id";
359 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
360 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
361 _physicalProcessorId = input[key].get<physicalProcessorId_t>();
362
363 key = "NUMA Affinity";
364 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
365 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
366 _numaAffinity = input[key].get<numaAffinity_t>();
367
368 key = "Caches";
369 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
370 if (input[key].is_array() == false) HICR_THROW_LOGIC("The '%s' entry is not an array", key.c_str());
371
372 _caches.clear();
373 for (const auto &c : input[key])
374 {
375 // Deserializing cache
376 auto cache = std::make_shared<backend::hwloc::Cache>(c);
377
378 // Adding it to the list
379 _caches.insert(cache);
380 }
381 }
382
383 private:
384
388 logicalProcessorId_t _logicalProcessorId;
389
395 physicalProcessorId_t _physicalProcessorId;
396
400 numaAffinity_t _numaAffinity;
401
406 std::unordered_set<std::shared_ptr<backend::hwloc::Cache>> _caches;
407};
408
409} // 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:335
static __INLINE__ logicalProcessorId_t detectLogicalProcessorId(hwloc_topology_t topology, const hwlocObjectIndex_t objectId)
Definition computeResource.hpp:144
unsigned int hwlocObjectIndex_t
Definition computeResource.hpp:47
__INLINE__ void deserializeImpl(const nlohmann::json &input) override
Definition computeResource.hpp:348
ComputeResource(const hwlocObjectIndex_t hwlocObjectIndex, 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:87
unsigned int logicalProcessorId_t
Definition computeResource.hpp:52
static __INLINE__ std::unordered_set< std::shared_ptr< backend::hwloc::Cache > > detectCpuCaches(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:226
unsigned int numaAffinity_t
Definition computeResource.hpp:62
ComputeResource()
Definition computeResource.hpp:106
ComputeResource(hwloc_topology_t topology, const hwlocObjectIndex_t hwlocObjectIndex)
Definition computeResource.hpp:69
unsigned int physicalProcessorId_t
Definition computeResource.hpp:57
static __INLINE__ physicalProcessorId_t detectPhysicalProcessorId(hwloc_topology_t topology, const hwlocObjectIndex_t objectId)
Definition computeResource.hpp:159
static __INLINE__ numaAffinity_t detectCoreNUMAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:174
__INLINE__ logicalProcessorId_t getProcessorId() const
Definition computeResource.hpp:113
__INLINE__ physicalProcessorId_t getPhysicalProcessorId() const
Definition computeResource.hpp:121
static __INLINE__ numaAffinity_t getCpuNumaAffinity(hwloc_topology_t topology, const logicalProcessorId_t logicalProcessorId)
Definition computeResource.hpp:295
static __INLINE__ void detectThreadPUs(hwloc_topology_t topology, hwloc_obj_t obj, int depth, std::vector< logicalProcessorId_t > &threadPUs)
Definition computeResource.hpp:131
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