Le 28/01/2011 15:32, guillaume Arnal a écrit :
> Hi everyone,
> I'm beginner in using hwloc and I have some questions.
> First: I'm looking for a way to find which core is using by the
> current thread. (maybe with hwloc_get_thread_cpubind ??)
> Second: is there a way to set the number of core usable for the
> current process ?
> Thank you in advance for your help.
> Guillaume Arnal


First, are you talking about cores for real (one core that may contain
multiple hyperthread), or are physical processors ok (those that you see
in /proc/cpuinfo) ?

hwloc_get_thread_cpubind() returns the binding of a thread specified by
its Linux thread id number. For the current thread, you may also use
hwloc_get_cpubind() with flag HWLOC_CPUBIND_THREAD. Both give a cpuset
listing all physical processors where the thread is currently allowed to
run. Processes are not bound by default, so the cpuset would contain all
existing processors, even if your thread actually uses a single core.

hwloc_cpuset_t cpuset = hwloc_bitmap_alloc();
hwloc_get_cpubind(topology, cpuset, HWLOC_CPUBIND_THREAD);

Once you have a cpuset, you may get the corresponding cores with:

hwloc_obj_t prev = NULL;
while ((prev = hwloc_get_next_obj_inside_cpuset_by_type(topology, cpuset, 
   /* do what you want with object core */

If the thread is bound to a single physical processors, you can get its
physical id with:


If you want to force your current process to run on the 3rd core, you
can do something like:

hwloc_obj_t core = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, 2);
hwloc_set_cpubind(topology, core->cpuset, HWLOC_CPUBIND_PROCESS);

Hope this helps,

Reply via email to