Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUMA memory replication for NNUE weights #5285

Closed
wants to merge 3 commits into from

Commits on May 28, 2024

  1. Allow for NUMA memory replication for NNUE weights. Bind threads to e…

    …nsure execution on a specific NUMA node.
    
    This patch introduces NUMA memory replication, currently only utilized for the NNUE weights. Along with it comes all machinery required to identify NUMA nodes and bind threads to specific processors/nodes. It also comes with small changes to Thread and ThreadPool to allow easier execution of custom functions on the designated thread. Old thread binding (WinProcGroup) machinery is removed because it's incompatible with this patch. Small changes to unrelated parts of the code were made to ensure correctness, like some classes being made unmovable, raw pointers replaced with unique_ptr. etc.
    
    Windows 7 and Windows 10 is partially supported. Windows 11 is fully supported. Linux is fully supported, with explicit exclusion of Android. No additional dependencies.
    
    -----------------
    
    A new UCI option `NumaPolicy` is introduced. It can take the following values:
    ```
    system - gathers NUMA node information from the system (lscpu or windows api), for each threads binds it to a single NUMA node
    none - assumes there is 1 NUMA node, never binds threads
    auto - this is the default value, depends on the number of set threads and NUMA nodes, will only enable binding on multinode systems and when the number of threads reaches a threshold (dependent on node size and count)
    [[custom]] - 
      // ':'-separated numa nodes
      // ','-separated cpu indices
      // supports "first-last" range syntax for cpu indices, 
      for example '0-15,32-47:16-31,48-63'
    ```
    
    Setting `NumaPolicy` forces recreation of the threads in the ThreadPool, which in turn forces the recreation of the TT.
    
    The threads are distributed among NUMA nodes in a round-robin fashion based on fill percentage (i.e. it will strive to fill all NUMA nodes evenly). Threads are bound to NUMA nodes, not specific processors, because that's our only requirement and the OS can schedule them better.
    
    Special care is made that maximum memory usage on systems that do not require memory replication stays as previously, that is, unnecessary copies are avoided.
    
    On linux the process' processor affinity is respected. This means that if you for example use taskset to restrict Stockfish to a single NUMA node then the `system` and `auto` settings will only see a single NUMA node (more precisely, the processors included in the current affinity mask) and act accordingly.
    
    -----------------
    
    We can't ensure that a memory allocation takes place on a given NUMA node without using libnuma on linux, or using appropriate custom allocators on windows (https://learn.microsoft.com/en-us/windows/win32/memory/allocating-memory-from-a-numa-node), so to avoid complications the current implementation relies on first-touch policy. Due to this we also rely on the memory allocator to give us a new chunk of untouched memory from the system. This appears to work reliably on linux, but results may vary.
    
    MacOS is not supported, because AFAIK it's not affected, and implementation would be problematic anyway.
    
    Windows is supported since Windows 7 (https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-setthreadgroupaffinity). Until Windows 11/Server 2022 NUMA nodes are split such that they cannot span processor groups. This is because before Windows 11/Server 2022 it's not possible to set thread affinity spanning processor groups. The splitting is done manually in some cases (required after Windows 10 Build 20348). Since Windows 11/Server 2022 we can set affinites spanning processor group so this splitting is not done, so the behaviour is pretty much like on linux.
    
    Linux is supported, **without** libnuma requirement. `lscpu` is expected.
    
    -----------------
    
    Passed 60+1 @ 256t 16000MB hash: https://tests.stockfishchess.org/tests/view/6654e443a86388d5e27db0d8
    ```
    LLR: 2.95 (-2.94,2.94) <0.00,10.00>
    Total: 278 W: 110 L: 29 D: 139
    Ptnml(0-2): 0, 1, 56, 82, 0
    ```
    
    Passed SMP STC: https://tests.stockfishchess.org/tests/view/6654fc74a86388d5e27db1cd
    ```
    LLR: 2.95 (-2.94,2.94) <-1.75,0.25>
    Total: 67152 W: 17354 L: 17177 D: 32621
    Ptnml(0-2): 64, 7428, 18408, 7619, 57
    ```
    
    Passed STC: https://tests.stockfishchess.org/tests/view/6654fb27a86388d5e27db15c
    ```
    LLR: 2.94 (-2.94,2.94) <-1.75,0.25>
    Total: 131648 W: 34155 L: 34045 D: 63448
    Ptnml(0-2): 426, 13878, 37096, 14008, 416
    ```
    
    fixes official-stockfish#5253
    Sopel97 committed May 28, 2024
    Configuration menu
    Copy the full SHA
    5325384 View commit details
    Browse the repository at this point in the history
  2. WIP

    vondele committed May 28, 2024
    Configuration menu
    Copy the full SHA
    59ae284 View commit details
    Browse the repository at this point in the history
  3. ci shall be fixed

    Disservin committed May 28, 2024
    Configuration menu
    Copy the full SHA
    934d6e3 View commit details
    Browse the repository at this point in the history