19 #include "isched_hwloc.h"
22 #if defined(HAVE_HWLOC)
25 static int first_init = 0;
26 static int initialized = 0;
27 static volatile pastix_atomic_lock_t topo_lock = PASTIX_ATOMIC_UNLOCKED;
29 #define HWLOC_GET_PARENT(OBJ) (OBJ)->parent
31 int isched_hwloc_init(
void)
34 pastix_atomic_lock( &topo_lock );
35 if ( first_init == 0 ) {
36 hwloc_bitmap_t cpuset = hwloc_bitmap_alloc();
38 unsigned version = hwloc_get_api_version();
39 if ((version >> 16) != (HWLOC_API_VERSION >> 16)) {
41 "isched_hwloc_init: PaStiX is compiled for hwloc API 0x%x but running on incompatible library API 0x%x.\n",
42 HWLOC_API_VERSION, version );
46 rc = hwloc_topology_init( &
topology );
49 "isched_hwloc_init: Failed to initialize HwLoc topology. Binding will not be available\n");
51 pastix_atomic_unlock( &topo_lock );
55 rc = hwloc_topology_load(
topology );
58 "isched_hwloc_init: Failed to load the HwLoc topology. Binding will not be available\n");
60 pastix_atomic_unlock( &topo_lock );
64 rc = hwloc_get_cpubind(
topology, cpuset, HWLOC_CPUBIND_PROCESS );
66 #if HWLOC_API_VERSION >= 0x20000
67 rc = hwloc_topology_restrict(
topology, cpuset, HWLOC_RESTRICT_FLAG_REMOVE_CPULESS );
69 rc = hwloc_topology_restrict(
topology, cpuset, 0 );
73 "isched_hwloc_init: Failed to restrict the topology to the correct cpuset\n"
74 " This may generate incorrect bindings\n");
77 hwloc_bitmap_free(cpuset);
82 pastix_atomic_unlock( &topo_lock );
86 int isched_hwloc_destroy(
void)
88 pastix_atomic_lock( &topo_lock );
90 if ( (first_init == 0) && initialized ) {
93 pastix_atomic_unlock( &topo_lock );
97 unsigned int isched_hwloc_nb_sockets_per_obj( hwloc_obj_type_t type,
int index )
99 hwloc_obj_t obj = hwloc_get_obj_by_type(
topology, type, index);
100 assert( obj != NULL );
101 return hwloc_get_nbobjs_inside_cpuset_by_type(
topology, obj->cpuset, HWLOC_OBJ_PACKAGE);
104 int isched_hwloc_socketsnbr()
106 return isched_hwloc_nb_sockets_per_obj( HWLOC_OBJ_MACHINE, 0 );
109 unsigned int isched_hwloc_nb_cores_per_obj( hwloc_obj_type_t type,
int index )
111 hwloc_obj_t obj = hwloc_get_obj_by_type(
topology, type, index);
112 assert( obj != NULL );
113 return hwloc_get_nbobjs_inside_cpuset_by_type(
topology, obj->cpuset, HWLOC_OBJ_CORE);
116 int isched_hwloc_world_size()
118 return isched_hwloc_nb_cores_per_obj( HWLOC_OBJ_MACHINE, 0 );
121 int isched_hwloc_bind_on_core_index(
int cpu_index)
124 hwloc_bitmap_t cpuset;
127 core = hwloc_get_obj_by_type(
topology, HWLOC_OBJ_CORE, cpu_index);
130 "isched_hwloc_bind_on_core_index: unable to get the core of index %i (nb physical cores = %i )\n",
131 cpu_index, isched_hwloc_world_size());
136 cpuset = hwloc_bitmap_dup(core->cpuset);
137 hwloc_bitmap_singlify(cpuset);
140 if (hwloc_set_cpubind(
topology, cpuset, HWLOC_CPUBIND_THREAD)) {
142 hwloc_bitmap_asprintf(&str, core->cpuset);
143 fprintf(stderr,
"isched_hwloc: couldn't bind to cpuset %s\n", str);
147 hwloc_bitmap_free(cpuset);
152 cpu_index = core->os_index;
155 hwloc_bitmap_free(cpuset);
159 int isched_hwloc_unbind()
164 if ( first_init <= 0 ) {
169 obj = hwloc_get_obj_by_type(
topology, HWLOC_OBJ_MACHINE, 0 );
171 fprintf(stderr,
"isched_hwloc_unbind: Could not get object\n");
175 if (hwloc_set_cpubind(
topology, obj->cpuset, HWLOC_CPUBIND_THREAD)) {
177 hwloc_bitmap_asprintf(&str, obj->cpuset);
178 fprintf(stderr,
"isched_hwloc_unbind: Couldn't unbind with cpuset %s\n", str);
static hwloc_topology_t topology