CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Thread/Core affinity

    I use the Windows CPU Meter on my dual-core machine. It has two dials which I (wrongly) thought were showing me the usage for core #1 and core #2. But I've just realised this morning that the 2nd dial actually shows memory usage. Does anyone know of a gadget which can show me the load distribution between my cores?

    I'm just interested to know how Windows allocates threads to cores for a multicore processor. Does it use some kind of 'round robin' strategy? Or does it wait until the first core's getting quite stressed before then allocating the second core and so on?

    Or to put it another way... is there ever anything to be gained by using SetThreadAffinityMask() or SetThreadIdealProcessor() ??
    Last edited by John E; March 27th, 2015 at 07:04 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    May 2007
    Posts
    811

    Re: Thread/Core affinity

    Maybe this Process Explorer.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Thread/Core affinity

    depending on your windows version.
    task manager.

    Open taskmanager, select the performance tab, right click the CPU graph and select "change graph to" > "logical processors"
    iirc this was present since vista

    There are even more elaborate tools that even breakdown the process-per-processor usage. But this is something you should rarely need.

    Allocation of cores is quite complex and not just round robin. part of the power saving modes is that windows may slow down (or even shut down) entire processors on a multiprocessor machine, so it will then prefer the cores of the fastest running processors.
    Processor slow down can also happen as part of overheating protection.
    Depending on your motherboard features, either all processors will slow down or only a few (and may not slow down equally).
    A Multi processor motherboard may have different CPU's.
    Depending on architecture your main CPU may be responsible for delegation tasks and/or bus control so additional CPU's might be slightly slower.
    Depending on usage, non hyper-threaded cores may be prefered for some threads (or just the opposite).

    Windows technically accounts for all of that, but it does depend on proper hardware drivers for some of those features. Not all motherboard manufacturers are equally good at managing this. Which in turn means Windows may not be able to optimize allocation as efficient as possible.


    You want to avoid locking your program to specific (logical) processors, whenever you do, you're giving windows less wiggle room and it may even make your program slower overall (suppose you set an affinity to logical processor 1, and another app does the same... now both apps will wait on eachother rather than running side by side). Even if processor 1 is twice as fast. the other gazillion processors on the machine don't even get used so you loose out overall.
    It's ok to do this if you have a legacy app that barfs out otherwise and you need this as a 'quick fix'. But for all other cases, setting an affinity means you have a bad design or hardware dependency somewhere.

    Many apps "don't like" hyperthreaded cores. and set an affinity, but this makes the issue worse (again if you have multiple of those apps). If it is really an issue, then setting the affinity should be a user choice, not the developer choice. There are some restrictions on hyperthreaded cores, but disabling them alltogether means your code gets lower throughput overall.
    There are good ways to detect capabilities and adjust your code to make best use of what's available, rather than trying it the other way around and try to enforce (or worse "assume") specific hardware affinity.

    bad assumptions: I have seen apps set affinity masks on all even cores in an attempt to avoid the hyper threaded cores, but not all systems have the hyper threaded cores as even values. Some are odd, some are in blocks (all the regular cores, followed by all the hyperthreaded ones). Such assumptions are especially bad if your system doesn't even have hyperthreading and you now made your app only use half of the available system resources.

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Thread/Core affinity

    Thanks guys. I tried both the Task Manager and Process Explorer and they both showed the same results (as you might expect!) Setting a process's affinity to one particular core did show a noticeable affinity for that core when that process got busy. And in the absence of any particular affinity, the load seemed to get shared (very approximately) between the available cores.

    Interesting stuff....
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Thread/Core affinity

    The Windows task scheduler attempts to evenly distribute the load across cores/processors and the algorithm that does this changes from version to version.

    I generally try to avoid setting the affinity and just let the scheduler do its thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured