diff --git a/src/support/hwloc.cpp b/src/support/hwloc.cpp index b503bb20258a8f539556c00f89f67ca0bd90e801..82f9bdb368161968d634586280d1e162bb14c85a 100644 --- a/src/support/hwloc.cpp +++ b/src/support/hwloc.cpp @@ -21,6 +21,7 @@ #include "debug.hpp" #ifdef HWLOC + #include #ifdef GPU_DEV #include #endif @@ -176,4 +177,42 @@ bool Hwloc::isCpuAvailable( unsigned int cpu ) const return hwloc_get_pu_obj_by_os_index( _hwlocTopology, cpu ) != NULL; #endif } + +CpuSet Hwloc::getCoreCpusetOf( unsigned int cpu ) +{ + CpuSet core_cpuset; +#ifdef HWLOC + hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index( _hwlocTopology, cpu ); + hwloc_obj_t core = hwloc_get_ancestor_obj_by_type( _hwlocTopology, HWLOC_OBJ_CORE, pu ); + hwloc_cpuset_to_glibc_sched_affinity( _hwlocTopology, core->cpuset, + core_cpuset.get_cpu_set_pointer(), sizeof(cpu_set_t)); +#endif + return core_cpuset; +} + +std::list Hwloc::getCoreCpusetsOf( const CpuSet& parent ) +{ + std::list core_cpusets; +#ifdef HWLOC + // Covert parent cpuset to hwlocset + hwloc_cpuset_t hwlocset = hwloc_bitmap_alloc(); + hwloc_cpuset_from_glibc_sched_affinity( _hwlocTopology, hwlocset, + parent.get_cpu_set_pointer(), sizeof(cpu_set_t)); + + // Iterate cores inside parent cpuset + hwloc_obj_t core = NULL; + while ( (core = hwloc_get_next_obj_inside_cpuset_by_type( + _hwlocTopology, hwlocset, HWLOC_OBJ_CORE, core)) != NULL ) { + CpuSet core_cpuset; + hwloc_cpuset_to_glibc_sched_affinity( _hwlocTopology, core->cpuset, + core_cpuset.get_cpu_set_pointer(), sizeof(cpu_set_t)); + // Append core cpuset to list + core_cpusets.push_back( core_cpuset ); + } + + hwloc_bitmap_free(hwlocset); +#endif + return core_cpusets; +} + } diff --git a/src/support/hwloc_decl.hpp b/src/support/hwloc_decl.hpp index c23c6fe51d973e14264ede97ecac8d9efba09a05..86adcdce03b1b273b976f8bf43949a9968ef6fb4 100644 --- a/src/support/hwloc_decl.hpp +++ b/src/support/hwloc_decl.hpp @@ -23,6 +23,7 @@ #include #include +#include "cpuset.hpp" #ifdef HWLOC #include @@ -63,6 +64,8 @@ class Hwloc { */ bool isCpuAvailable( unsigned int cpu ) const; + CpuSet getCoreCpusetOf( unsigned int cpu ); + std::list getCoreCpusetsOf( const CpuSet& parent ); }; } // namespace nanos