mirror of
https://github.com/sockspls/badfish
synced 2025-05-02 01:29:36 +00:00

Allow for NUMA memory replication for NNUE weights. Bind threads to ensure 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 #5253 closes https://github.com/official-stockfish/Stockfish/pull/5285 No functional change
187 lines
5.2 KiB
C++
187 lines
5.2 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2024 The Stockfish developers (see AUTHORS file)
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "ucioption.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cctype>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <utility>
|
|
|
|
#include "misc.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
bool CaseInsensitiveLess::operator()(const std::string& s1, const std::string& s2) const {
|
|
|
|
return std::lexicographical_compare(
|
|
s1.begin(), s1.end(), s2.begin(), s2.end(),
|
|
[](char c1, char c2) { return std::tolower(c1) < std::tolower(c2); });
|
|
}
|
|
|
|
void OptionsMap::setoption(std::istringstream& is) {
|
|
std::string token, name, value;
|
|
|
|
is >> token; // Consume the "name" token
|
|
|
|
// Read the option name (can contain spaces)
|
|
while (is >> token && token != "value")
|
|
name += (name.empty() ? "" : " ") + token;
|
|
|
|
// Read the option value (can contain spaces)
|
|
while (is >> token)
|
|
value += (value.empty() ? "" : " ") + token;
|
|
|
|
if (options_map.count(name))
|
|
options_map[name] = value;
|
|
else
|
|
sync_cout << "No such option: " << name << sync_endl;
|
|
}
|
|
|
|
Option OptionsMap::operator[](const std::string& name) const {
|
|
auto it = options_map.find(name);
|
|
return it != options_map.end() ? it->second : Option();
|
|
}
|
|
|
|
Option& OptionsMap::operator[](const std::string& name) { return options_map[name]; }
|
|
|
|
std::size_t OptionsMap::count(const std::string& name) const { return options_map.count(name); }
|
|
|
|
Option::Option(const char* v, OnChange f) :
|
|
type("string"),
|
|
min(0),
|
|
max(0),
|
|
on_change(std::move(f)) {
|
|
defaultValue = currentValue = v;
|
|
}
|
|
|
|
Option::Option(bool v, OnChange f) :
|
|
type("check"),
|
|
min(0),
|
|
max(0),
|
|
on_change(std::move(f)) {
|
|
defaultValue = currentValue = (v ? "true" : "false");
|
|
}
|
|
|
|
Option::Option(OnChange f) :
|
|
type("button"),
|
|
min(0),
|
|
max(0),
|
|
on_change(std::move(f)) {}
|
|
|
|
Option::Option(double v, int minv, int maxv, OnChange f) :
|
|
type("spin"),
|
|
min(minv),
|
|
max(maxv),
|
|
on_change(std::move(f)) {
|
|
defaultValue = currentValue = std::to_string(v);
|
|
}
|
|
|
|
Option::Option(const char* v, const char* cur, OnChange f) :
|
|
type("combo"),
|
|
min(0),
|
|
max(0),
|
|
on_change(std::move(f)) {
|
|
defaultValue = v;
|
|
currentValue = cur;
|
|
}
|
|
|
|
Option::operator int() const {
|
|
assert(type == "check" || type == "spin");
|
|
return (type == "spin" ? std::stoi(currentValue) : currentValue == "true");
|
|
}
|
|
|
|
Option::operator std::string() const {
|
|
assert(type == "string");
|
|
return currentValue;
|
|
}
|
|
|
|
bool Option::operator==(const char* s) const {
|
|
assert(type == "combo");
|
|
return !CaseInsensitiveLess()(currentValue, s) && !CaseInsensitiveLess()(s, currentValue);
|
|
}
|
|
|
|
bool Option::operator!=(const char* s) const { return !(*this == s); }
|
|
|
|
|
|
// Inits options and assigns idx in the correct printing order
|
|
|
|
void Option::operator<<(const Option& o) {
|
|
|
|
static size_t insert_order = 0;
|
|
|
|
*this = o;
|
|
idx = insert_order++;
|
|
}
|
|
|
|
|
|
// Updates currentValue and triggers on_change() action. It's up to
|
|
// the GUI to check for option's limits, but we could receive the new value
|
|
// from the user by console window, so let's check the bounds anyway.
|
|
Option& Option::operator=(const std::string& v) {
|
|
|
|
assert(!type.empty());
|
|
|
|
if ((type != "button" && type != "string" && v.empty())
|
|
|| (type == "check" && v != "true" && v != "false")
|
|
|| (type == "spin" && (std::stof(v) < min || std::stof(v) > max)))
|
|
return *this;
|
|
|
|
if (type == "combo")
|
|
{
|
|
OptionsMap comboMap; // To have case insensitive compare
|
|
std::string token;
|
|
std::istringstream ss(defaultValue);
|
|
while (ss >> token)
|
|
comboMap[token] << Option();
|
|
if (!comboMap.count(v) || v == "var")
|
|
return *this;
|
|
}
|
|
|
|
if (type != "button")
|
|
currentValue = v;
|
|
|
|
if (on_change)
|
|
on_change(*this);
|
|
|
|
return *this;
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
|
for (size_t idx = 0; idx < om.options_map.size(); ++idx)
|
|
for (const auto& it : om.options_map)
|
|
if (it.second.idx == idx)
|
|
{
|
|
const Option& o = it.second;
|
|
os << "\noption name " << it.first << " type " << o.type;
|
|
|
|
if (o.type == "string" || o.type == "check" || o.type == "combo")
|
|
os << " default " << o.defaultValue;
|
|
|
|
if (o.type == "spin")
|
|
os << " default " << int(stof(o.defaultValue)) << " min " << o.min << " max "
|
|
<< o.max;
|
|
|
|
break;
|
|
}
|
|
|
|
return os;
|
|
}
|
|
}
|