dylan-lang / opendylan

Open Dylan compiler and IDE
http://opendylan.org/
Other
456 stars 68 forks source link

Thread affinity #1038

Open waywardmonkeys opened 8 years ago

waywardmonkeys commented 8 years ago

I need to be able to set thread affinity. This should be added to the threads library. An interesting complication is that the capabilities on the various platforms are all different, so we should look at how this has been abstracted elsewhere.

BarAgent commented 8 years ago

Intel’s Threading Building Blocks library just tags tasks with a common affinity [1], and the scheduler tries to pick a CPU set accordingly. Mac OS X does the same thing, but also tells you how many CPUs share the different caches for strategizing [2].

Windows and .NET use a CPU mask [3, 4]. You can figure out what to use for the mask by inspecting CPU groups. .NET also has a Begin/EndThreadAffinity which tells the scheduler not to execute the bounded code on a different thread.

One of the Java libraries I found also uses CPU masks and an affinity lock [6]. There is also a way to create a new affinity in terms of the existing one, which seems interesting but I didn’t look into it.

There is a new Rust pre-RFC that seems to use an affinity lock as well [7]. The discussion points out a problem in thinking of affinity in terms of locks, considering that Mac OS X only offers affinity as a hint.

Another (commercial) Java class offers a strategy-based API; you tell it how to schedule directly [8].

C++ punts. It provides an abstract thread handle and you have call OS-specific functions [5].

[1] Affinity https://www.threadingbuildingblocks.org/docs/help/reference/task_scheduler/affinity.htm [2] https://developer.apple.com/library/mac/releasenotes/Performance/RN-AffinityAPI/index.html https://developer.apple.com/library/mac/releasenotes/Performance/RN-AffinityAPI/index.html [3] https://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx https://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx [4] https://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.processoraffinity.aspx https://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.processoraffinity.aspx [5] http://eli.thegreenplace.net/2016/c11-threads-affinity-and-hyperthreading/ http://eli.thegreenplace.net/2016/c11-threads-affinity-and-hyperthreading/ [6] http://openhft.github.io/Java-Thread-Affinity/apidocs/ http://openhft.github.io/Java-Thread-Affinity/apidocs/ [7] https://internals.rust-lang.org/t/pre-rfc-thread-affinity/3117 https://internals.rust-lang.org/t/pre-rfc-thread-affinity/3117 [8] http://mentaaffinity.soliveirajr.com/Page.mtw http://mentaaffinity.soliveirajr.com/Page.mtw

On Feb 7, 2016, at 7:51 AM, Bruce Mitchener notifications@github.com wrote:

I need to be able to set thread affinity. This should be added to the threads library. An interesting complication is that the capabilities on the various platforms are all different, so we should look at how this has been abstracted elsewhere.

— Reply to this email directly or view it on GitHub https://github.com/dylan-lang/opendylan/issues/1038.

BarAgent commented 8 years ago

The least common denominator is in the first paragraph; I suppose we should abstract it at that level, with just a tag and a way to get the number of useful groupings.