hwloc icon indicating copy to clipboard operation
hwloc copied to clipboard

CPU binding on MacOS

Open isaevil opened this issue 2 years ago • 4 comments

Hi, AFAIK, CPU binding is not supported on MacOS due to the lack of the necessary interface from Apple.

hwloc-bind core:0-3 hwloc-bind --get
hwloc_set_cpubind 0x0000000f (flags 0x0) failed (errno 78 Function not implemented)

But still, Apple provide interface to set QOS classes (for example, see pthread section at Apple's doc. Is it possible to have some implemented hwloc API that is based on those hints? It would allow to make hints to different core types on hybrid CPUs like Apple M1/M2.

isaevil avatar Dec 05 '22 15:12 isaevil

Hello We have some Linux specific API to manipulate binding, we could certainly do something in a new hwloc/darwin.h or so. Did you have an idea of what the new hwloc API would look like?

bgoglin avatar Dec 05 '22 16:12 bgoglin

As far as I can understand from docs I've read, Apple's pthread QOS interface allows to (based on reading of qos.h):

  • Set QOS class to pthread attributes and create a thread.
  • Set QOS class to calling thread (pthread_set_qos_class_self_np(...)).
  • Start/End QOS class override of specified thread (pthread_override_qos_class_start_np(...)).
  • Get QOS class from attribute, from calling thread.

Honestly, I don't know how high-level (or low-level) API should be used here. It can be either a possibility to set those QOS classes directly or maybe leave it internal and make hints based on cpukind (e.g. set QOS_CLASS_USER_INTERACTIVE or QOS_CLASS_USER_INITIATED to hint using Performance cores first or set QOS_CLASS_BACKGROUND to hint using Efficient cores).

I am personally interested in using it with threads, so maybe some hwloc_darwin_{set|get}_thread_qos_class name for this type of functionality is appropriate.

isaevil avatar Dec 05 '22 18:12 isaevil

It seems to me that such hwloc functions wouldn't do anything clever, they would only call the equivalent function from MacOS X, why would be the benefit of a hwloc function then?

For Linux, the point of functions such as hwloc_linux_set_tid_cpubind() is that they use hwloc cpusets instead of Linux cpusets for describing the binding, hence it's easier to integrate inside an existing hwloc code.

bgoglin avatar Dec 05 '22 20:12 bgoglin

Maybe it can be some additional logic also based on CPU sets? Like if we specify CPU set with P cores it will use "User interactive" or "User initiated", if we specify CPU set with E cores, it will use "Background" QOS class. If it is something at an intersection, it can be "Utility" QOS class. I am not sure, whether this logic seems reasonable or not, but just for example.

I guess the only benefit is having some alternative to real CPU Binding on MacOS and being able to use it as a part of HWLOC. I think it would be easier to fit this functionality into the existing code that uses HWLOC.

isaevil avatar Dec 06 '22 08:12 isaevil