mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
Merge commit '36eb9bc783d35842571d0d4313349b964892d9ca' into cluster
This is the last commit before there are more conflicts.
This commit is contained in:
@@ -133,6 +133,11 @@ void Engine::set_numa_config_from_option(const std::string& o) {
|
||||
{
|
||||
numaContext.set_numa_config(NumaConfig::from_system());
|
||||
}
|
||||
else if (o == "hardware")
|
||||
{
|
||||
// Don't respect affinity set in the system.
|
||||
numaContext.set_numa_config(NumaConfig::from_system(false));
|
||||
}
|
||||
else if (o == "none")
|
||||
{
|
||||
numaContext.set_numa_config(NumaConfig{});
|
||||
|
||||
+16
-10
@@ -24,8 +24,9 @@
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <tuple>
|
||||
|
||||
#include "nnue/network.h"
|
||||
#include "nnue/nnue_misc.h"
|
||||
@@ -46,8 +47,7 @@ int Eval::simple_eval(const Position& pos, Color c) {
|
||||
|
||||
bool Eval::use_smallnet(const Position& pos) {
|
||||
int simpleEval = simple_eval(pos, pos.side_to_move());
|
||||
int pawnCount = pos.count<PAWN>();
|
||||
return std::abs(simpleEval) > 992 + 6 * pawnCount * pawnCount / 16;
|
||||
return std::abs(simpleEval) > 992;
|
||||
}
|
||||
|
||||
// Evaluate is the evaluator for the outer world. It returns a static evaluation
|
||||
@@ -61,17 +61,22 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks,
|
||||
|
||||
int simpleEval = simple_eval(pos, pos.side_to_move());
|
||||
bool smallNet = use_smallnet(pos);
|
||||
int nnueComplexity;
|
||||
int v;
|
||||
|
||||
Value nnue = smallNet ? networks.small.evaluate(pos, &caches.small, true, &nnueComplexity)
|
||||
: networks.big.evaluate(pos, &caches.big, true, &nnueComplexity);
|
||||
auto [psqt, positional] = smallNet ? networks.small.evaluate(pos, &caches.small)
|
||||
: networks.big.evaluate(pos, &caches.big);
|
||||
|
||||
constexpr int delta = 3;
|
||||
Value nnue = ((128 - delta) * psqt + (128 + delta) * positional) / 128;
|
||||
int nnueComplexity = std::abs(psqt - positional);
|
||||
|
||||
// Re-evaluate the position when higher eval accuracy is worth the time spent
|
||||
if (smallNet && (nnue * simpleEval < 0 || std::abs(nnue) < 250))
|
||||
{
|
||||
nnue = networks.big.evaluate(pos, &caches.big, true, &nnueComplexity);
|
||||
smallNet = false;
|
||||
std::tie(psqt, positional) = networks.big.evaluate(pos, &caches.big);
|
||||
nnue = ((128 - delta) * psqt + (128 + delta) * positional) / 128;
|
||||
nnueComplexity = std::abs(psqt - positional);
|
||||
smallNet = false;
|
||||
}
|
||||
|
||||
// Blend optimism and eval with nnue complexity
|
||||
@@ -109,8 +114,9 @@ std::string Eval::trace(Position& pos, const Eval::NNUE::Networks& networks) {
|
||||
|
||||
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
|
||||
|
||||
Value v = networks.big.evaluate(pos, &caches->big, false);
|
||||
v = pos.side_to_move() == WHITE ? v : -v;
|
||||
auto [psqt, positional] = networks.big.evaluate(pos, &caches->big);
|
||||
Value v = psqt + positional;
|
||||
v = pos.side_to_move() == WHITE ? v : -v;
|
||||
ss << "NNUE evaluation " << 0.01 * UCIEngine::to_cp(v, pos) << " (white side)\n";
|
||||
|
||||
v = evaluate(networks, pos, *caches, VALUE_ZERO);
|
||||
|
||||
+1
-1
@@ -182,7 +182,7 @@ void MovePicker::score() {
|
||||
m.value += 2 * (*pawnHistory)[pawn_structure_index(pos)][pc][to];
|
||||
m.value += 2 * (*continuationHistory[0])[pc][to];
|
||||
m.value += (*continuationHistory[1])[pc][to];
|
||||
m.value += (*continuationHistory[2])[pc][to] / 4;
|
||||
m.value += (*continuationHistory[2])[pc][to] / 3;
|
||||
m.value += (*continuationHistory[3])[pc][to];
|
||||
m.value += (*continuationHistory[5])[pc][to];
|
||||
|
||||
|
||||
@@ -43,39 +43,6 @@ namespace Stockfish::Simd {
|
||||
return _mm512_reduce_add_epi32(sum) + bias;
|
||||
}
|
||||
|
||||
/*
|
||||
Parameters:
|
||||
sum0 = [zmm0.i128[0], zmm0.i128[1], zmm0.i128[2], zmm0.i128[3]]
|
||||
sum1 = [zmm1.i128[0], zmm1.i128[1], zmm1.i128[2], zmm1.i128[3]]
|
||||
sum2 = [zmm2.i128[0], zmm2.i128[1], zmm2.i128[2], zmm2.i128[3]]
|
||||
sum3 = [zmm3.i128[0], zmm3.i128[1], zmm3.i128[2], zmm3.i128[3]]
|
||||
|
||||
Returns:
|
||||
ret = [
|
||||
reduce_add_epi32(zmm0.i128[0]), reduce_add_epi32(zmm1.i128[0]), reduce_add_epi32(zmm2.i128[0]), reduce_add_epi32(zmm3.i128[0]),
|
||||
reduce_add_epi32(zmm0.i128[1]), reduce_add_epi32(zmm1.i128[1]), reduce_add_epi32(zmm2.i128[1]), reduce_add_epi32(zmm3.i128[1]),
|
||||
reduce_add_epi32(zmm0.i128[2]), reduce_add_epi32(zmm1.i128[2]), reduce_add_epi32(zmm2.i128[2]), reduce_add_epi32(zmm3.i128[2]),
|
||||
reduce_add_epi32(zmm0.i128[3]), reduce_add_epi32(zmm1.i128[3]), reduce_add_epi32(zmm2.i128[3]), reduce_add_epi32(zmm3.i128[3])
|
||||
]
|
||||
*/
|
||||
[[maybe_unused]] static __m512i
|
||||
m512_hadd128x16_interleave(__m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3) {
|
||||
|
||||
__m512i sum01a = _mm512_unpacklo_epi32(sum0, sum1);
|
||||
__m512i sum01b = _mm512_unpackhi_epi32(sum0, sum1);
|
||||
|
||||
__m512i sum23a = _mm512_unpacklo_epi32(sum2, sum3);
|
||||
__m512i sum23b = _mm512_unpackhi_epi32(sum2, sum3);
|
||||
|
||||
__m512i sum01 = _mm512_add_epi32(sum01a, sum01b);
|
||||
__m512i sum23 = _mm512_add_epi32(sum23a, sum23b);
|
||||
|
||||
__m512i sum0123a = _mm512_unpacklo_epi64(sum01, sum23);
|
||||
__m512i sum0123b = _mm512_unpackhi_epi64(sum01, sum23);
|
||||
|
||||
return _mm512_add_epi32(sum0123a, sum0123b);
|
||||
}
|
||||
|
||||
[[maybe_unused]] static void m512_add_dpbusd_epi32(__m512i& acc, __m512i a, __m512i b) {
|
||||
|
||||
#if defined(USE_VNNI)
|
||||
|
||||
+4
-16
@@ -18,7 +18,6 @@
|
||||
|
||||
#include "network.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -207,15 +206,13 @@ bool Network<Arch, Transformer>::save(const std::optional<std::string>& filename
|
||||
|
||||
|
||||
template<typename Arch, typename Transformer>
|
||||
Value Network<Arch, Transformer>::evaluate(const Position& pos,
|
||||
AccumulatorCaches::Cache<FTDimensions>* cache,
|
||||
bool adjusted,
|
||||
int* complexity) const {
|
||||
NetworkOutput
|
||||
Network<Arch, Transformer>::evaluate(const Position& pos,
|
||||
AccumulatorCaches::Cache<FTDimensions>* cache) const {
|
||||
// We manually align the arrays on the stack because with gcc < 9.3
|
||||
// overaligning stack variables with alignas() doesn't work correctly.
|
||||
|
||||
constexpr uint64_t alignment = CacheLineSize;
|
||||
constexpr int delta = 24;
|
||||
|
||||
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
|
||||
TransformedFeatureType
|
||||
@@ -233,16 +230,7 @@ Value Network<Arch, Transformer>::evaluate(const Position&
|
||||
const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
|
||||
const auto psqt = featureTransformer->transform(pos, cache, transformedFeatures, bucket);
|
||||
const auto positional = network[bucket].propagate(transformedFeatures);
|
||||
|
||||
if (complexity)
|
||||
*complexity = std::abs(psqt - positional) / OutputScale;
|
||||
|
||||
// Give more value to positional evaluation when adjusted flag is set
|
||||
if (adjusted)
|
||||
return static_cast<Value>(((1024 - delta) * psqt + (1024 + delta) * positional)
|
||||
/ (1024 * OutputScale));
|
||||
else
|
||||
return static_cast<Value>((psqt + positional) / OutputScale);
|
||||
return {static_cast<Value>(psqt / OutputScale), static_cast<Value>(positional / OutputScale)};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -23,6 +23,7 @@
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include "../memory.h"
|
||||
@@ -40,6 +41,7 @@ enum class EmbeddedNNUEType {
|
||||
SMALL,
|
||||
};
|
||||
|
||||
using NetworkOutput = std::tuple<Value, Value>;
|
||||
|
||||
template<typename Arch, typename Transformer>
|
||||
class Network {
|
||||
@@ -59,10 +61,8 @@ class Network {
|
||||
void load(const std::string& rootDirectory, std::string evalfilePath);
|
||||
bool save(const std::optional<std::string>& filename) const;
|
||||
|
||||
Value evaluate(const Position& pos,
|
||||
AccumulatorCaches::Cache<FTDimensions>* cache,
|
||||
bool adjusted = false,
|
||||
int* complexity = nullptr) const;
|
||||
NetworkOutput evaluate(const Position& pos,
|
||||
AccumulatorCaches::Cache<FTDimensions>* cache) const;
|
||||
|
||||
|
||||
void hint_common_access(const Position& pos,
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
|
||||
#include "../evaluate.h"
|
||||
#include "../position.h"
|
||||
@@ -131,8 +132,9 @@ trace(Position& pos, const Eval::NNUE::Networks& networks, Eval::NNUE::Accumulat
|
||||
|
||||
// We estimate the value of each piece by doing a differential evaluation from
|
||||
// the current base eval, simulating the removal of the piece from its square.
|
||||
Value base = networks.big.evaluate(pos, &caches.big);
|
||||
base = pos.side_to_move() == WHITE ? base : -base;
|
||||
auto [psqt, positional] = networks.big.evaluate(pos, &caches.big);
|
||||
Value base = psqt + positional;
|
||||
base = pos.side_to_move() == WHITE ? base : -base;
|
||||
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
for (Rank r = RANK_1; r <= RANK_8; ++r)
|
||||
@@ -148,9 +150,10 @@ trace(Position& pos, const Eval::NNUE::Networks& networks, Eval::NNUE::Accumulat
|
||||
pos.remove_piece(sq);
|
||||
st->accumulatorBig.computed[WHITE] = st->accumulatorBig.computed[BLACK] = false;
|
||||
|
||||
Value eval = networks.big.evaluate(pos, &caches.big);
|
||||
eval = pos.side_to_move() == WHITE ? eval : -eval;
|
||||
v = base - eval;
|
||||
std::tie(psqt, positional) = networks.big.evaluate(pos, &caches.big);
|
||||
Value eval = psqt + positional;
|
||||
eval = pos.side_to_move() == WHITE ? eval : -eval;
|
||||
v = base - eval;
|
||||
|
||||
pos.put_piece(pc, sq);
|
||||
st->accumulatorBig.computed[WHITE] = st->accumulatorBig.computed[BLACK] = false;
|
||||
|
||||
+227
-167
@@ -19,6 +19,7 @@
|
||||
#ifndef NUMA_H_INCLUDED
|
||||
#define NUMA_H_INCLUDED
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
@@ -63,21 +64,9 @@ static constexpr size_t WIN_PROCESSOR_GROUP_SIZE = 64;
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadselectedcpusetmasks
|
||||
using SetThreadSelectedCpuSetMasks_t = BOOL (*)(HANDLE, PGROUP_AFFINITY, USHORT);
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-setthreadgroupaffinity
|
||||
using SetThreadGroupAffinity_t = BOOL (*)(HANDLE, const GROUP_AFFINITY*, PGROUP_AFFINITY);
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadselectedcpusetmasks
|
||||
using GetThreadSelectedCpuSetMasks_t = BOOL (*)(HANDLE, PGROUP_AFFINITY, USHORT, PUSHORT);
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprocessaffinitymask
|
||||
using GetProcessAffinityMask_t = BOOL (*)(HANDLE, PDWORD_PTR, PDWORD_PTR);
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-getprocessgroupaffinity
|
||||
using GetProcessGroupAffinity_t = BOOL (*)(HANDLE, PUSHORT, PUSHORT);
|
||||
|
||||
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getactiveprocessorcount
|
||||
using GetActiveProcessorCount_t = DWORD (*)(WORD);
|
||||
|
||||
#endif
|
||||
|
||||
#include "misc.h"
|
||||
@@ -94,14 +83,7 @@ inline CpuIndex get_hardware_concurrency() {
|
||||
// only returns the number of processors in the first group, because only these
|
||||
// are available to std::thread.
|
||||
#ifdef _WIN64
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto GetActiveProcessorCount_f =
|
||||
GetActiveProcessorCount_t((void (*)()) GetProcAddress(k32, "GetActiveProcessorCount"));
|
||||
|
||||
if (GetActiveProcessorCount_f != nullptr)
|
||||
{
|
||||
concurrency = GetActiveProcessorCount_f(ALL_PROCESSOR_GROUPS);
|
||||
}
|
||||
concurrency = std::max<CpuIndex>(concurrency, GetActiveProcessorCount(ALL_PROCESSOR_GROUPS));
|
||||
#endif
|
||||
|
||||
return concurrency;
|
||||
@@ -109,6 +91,214 @@ inline CpuIndex get_hardware_concurrency() {
|
||||
|
||||
inline const CpuIndex SYSTEM_THREADS_NB = std::max<CpuIndex>(1, get_hardware_concurrency());
|
||||
|
||||
#if defined(_WIN64)
|
||||
|
||||
struct WindowsAffinity {
|
||||
std::optional<std::set<CpuIndex>> oldApi;
|
||||
std::optional<std::set<CpuIndex>> newApi;
|
||||
bool isDeterminate = true;
|
||||
|
||||
std::optional<std::set<CpuIndex>> get_combined() const {
|
||||
// When the affinity is not determinate we treat it as no affinity,
|
||||
// because otherwise we would have to set affinity to fewer
|
||||
// processors than we currently have affinity to.
|
||||
if (!isDeterminate)
|
||||
return std::nullopt;
|
||||
|
||||
if (!oldApi.has_value())
|
||||
return newApi;
|
||||
if (!newApi.has_value())
|
||||
return oldApi;
|
||||
|
||||
std::set<CpuIndex> intersect;
|
||||
std::set_intersection(oldApi->begin(), oldApi->end(), newApi->begin(), newApi->end(),
|
||||
std::inserter(intersect, intersect.begin()));
|
||||
return intersect;
|
||||
}
|
||||
};
|
||||
|
||||
inline std::pair<BOOL, std::vector<USHORT>> get_process_group_affinity() {
|
||||
WORD numProcGroups = GetActiveProcessorGroupCount();
|
||||
|
||||
// GetProcessGroupAffinity requires the GroupArray argument to be
|
||||
// aligned to 4 bytes instead of just 2.
|
||||
static constexpr size_t GroupArrayMinimumAlignment = 4;
|
||||
static_assert(GroupArrayMinimumAlignment >= alignof(USHORT));
|
||||
|
||||
auto GroupArray = std::make_unique<USHORT[]>(
|
||||
numProcGroups + (GroupArrayMinimumAlignment / alignof(USHORT) - 1));
|
||||
|
||||
USHORT GroupCount = static_cast<USHORT>(numProcGroups);
|
||||
const BOOL status = GetProcessGroupAffinity(GetCurrentProcess(), &GroupCount, GroupArray.get());
|
||||
|
||||
return std::make_pair(status, std::vector(GroupArray.get(), GroupArray.get() + GroupCount));
|
||||
}
|
||||
|
||||
// Since Windows 11 and Windows Server 2022 thread affinities can span
|
||||
// processor groups and can be set as such by a new WinAPI function.
|
||||
// However, we may need to force using the old API if we detect
|
||||
// that the process has affinity set by the old API already and we want to override that.
|
||||
inline bool use_old_affinity_api() {
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto SetThreadSelectedCpuSetMasks_f = SetThreadSelectedCpuSetMasks_t(
|
||||
(void (*)()) GetProcAddress(k32, "SetThreadSelectedCpuSetMasks"));
|
||||
|
||||
if (SetThreadSelectedCpuSetMasks_f == nullptr)
|
||||
return true;
|
||||
|
||||
auto [status, groupAffinity] = get_process_group_affinity();
|
||||
|
||||
// If GroupCount > 1 then we know old API was never used and we can stick
|
||||
// to the new API safely.
|
||||
if (status != 0 && groupAffinity.size() > 1)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
// On Windows there are two ways to set affinity, and therefore 2 ways to get it.
|
||||
// These are not consistent, so we have to check both.
|
||||
// In some cases it is actually not possible to determine affinity.
|
||||
// For example when two different threads have affinity on different processor groups,
|
||||
// set using SetThreadAffinityMask, we can't retrieve the actual affinities.
|
||||
// From documentation on GetProcessAffinityMask:
|
||||
// > If the calling process contains threads in multiple groups,
|
||||
// > the function returns zero for both affinity masks.
|
||||
// In such cases we just give up and assume we have affinity for all processors.
|
||||
// nullopt means no affinity is set, that is, all processors are allowed
|
||||
inline WindowsAffinity get_process_affinity() {
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto GetThreadSelectedCpuSetMasks_f = GetThreadSelectedCpuSetMasks_t(
|
||||
(void (*)()) GetProcAddress(k32, "GetThreadSelectedCpuSetMasks"));
|
||||
|
||||
WindowsAffinity affinity;
|
||||
|
||||
if (GetThreadSelectedCpuSetMasks_f != nullptr)
|
||||
{
|
||||
USHORT RequiredMaskCount;
|
||||
BOOL status =
|
||||
GetThreadSelectedCpuSetMasks_f(GetCurrentThread(), nullptr, 0, &RequiredMaskCount);
|
||||
|
||||
// If RequiredMaskCount then these affinities were never set, but it's not consistent
|
||||
// so GetProcessAffinityMask may still return some affinity.
|
||||
if (status == 0)
|
||||
{
|
||||
affinity.isDeterminate = false;
|
||||
return affinity;
|
||||
}
|
||||
|
||||
if (RequiredMaskCount > 0)
|
||||
{
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
auto groupAffinities = std::make_unique<GROUP_AFFINITY[]>(RequiredMaskCount);
|
||||
|
||||
GetThreadSelectedCpuSetMasks_f(GetCurrentThread(), groupAffinities.get(),
|
||||
RequiredMaskCount, &RequiredMaskCount);
|
||||
|
||||
for (USHORT i = 0; i < RequiredMaskCount; ++i)
|
||||
{
|
||||
const size_t procGroupIndex = groupAffinities[i].Group;
|
||||
|
||||
for (size_t j = 0; j < WIN_PROCESSOR_GROUP_SIZE; ++j)
|
||||
{
|
||||
if (groupAffinities[i].Mask & (KAFFINITY(1) << j))
|
||||
cpus.insert(procGroupIndex * WIN_PROCESSOR_GROUP_SIZE + j);
|
||||
}
|
||||
}
|
||||
|
||||
affinity.newApi = std::move(cpus);
|
||||
}
|
||||
}
|
||||
|
||||
DWORD_PTR proc, sys;
|
||||
BOOL status = GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
|
||||
|
||||
// If proc == 0 then we can't determine affinity because it spans processor groups.
|
||||
if (status == 0 || proc == 0)
|
||||
{
|
||||
affinity.isDeterminate = false;
|
||||
return affinity;
|
||||
}
|
||||
|
||||
// If SetProcessAffinityMask was never called the affinity
|
||||
// must span all processor groups, but if it was called it must only span one.
|
||||
auto [status2, groupAffinity] = get_process_group_affinity();
|
||||
if (status2 == 0)
|
||||
{
|
||||
affinity.isDeterminate = false;
|
||||
return affinity;
|
||||
}
|
||||
|
||||
// If we have affinity for more than 1 group then at this point we
|
||||
// can assume SetProcessAffinityMask has never been called and therefore
|
||||
// according ot old API we do not have any affinity set.
|
||||
// Otherwise we have to assume we have affinity set and gather the processor IDs.
|
||||
if (groupAffinity.size() == 1)
|
||||
{
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
const size_t procGroupIndex = groupAffinity[0];
|
||||
|
||||
uint64_t mask = static_cast<uint64_t>(proc);
|
||||
for (size_t j = 0; j < WIN_PROCESSOR_GROUP_SIZE; ++j)
|
||||
{
|
||||
if (mask & (KAFFINITY(1) << j))
|
||||
cpus.insert(procGroupIndex * WIN_PROCESSOR_GROUP_SIZE + j);
|
||||
}
|
||||
|
||||
affinity.oldApi = std::move(cpus);
|
||||
}
|
||||
|
||||
return affinity;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) && !defined(__ANDROID__)
|
||||
|
||||
inline std::set<CpuIndex> get_process_affinity() {
|
||||
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
// For unsupported systems, or in case of a soft error, we may assume all processors
|
||||
// are available for use.
|
||||
[[maybe_unused]] auto set_to_all_cpus = [&]() {
|
||||
for (CpuIndex c = 0; c < SYSTEM_THREADS_NB; ++c)
|
||||
cpus.insert(c);
|
||||
};
|
||||
|
||||
// cpu_set_t by default holds 1024 entries. This may not be enough soon,
|
||||
// but there is no easy way to determine how many threads there actually is.
|
||||
// In this case we just choose a reasonable upper bound.
|
||||
static constexpr CpuIndex MaxNumCpus = 1024 * 64;
|
||||
|
||||
cpu_set_t* mask = CPU_ALLOC(MaxNumCpus);
|
||||
if (mask == nullptr)
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
||||
const size_t masksize = CPU_ALLOC_SIZE(MaxNumCpus);
|
||||
|
||||
CPU_ZERO_S(masksize, mask);
|
||||
|
||||
const int status = sched_getaffinity(0, masksize, mask);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
CPU_FREE(mask);
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (CpuIndex c = 0; c < MaxNumCpus; ++c)
|
||||
if (CPU_ISSET_S(c, masksize, mask))
|
||||
cpus.insert(c);
|
||||
|
||||
CPU_FREE(mask);
|
||||
|
||||
return cpus;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// We want to abstract the purpose of storing the numa node index somewhat.
|
||||
// Whoever is using this does not need to know the specifics of the replication
|
||||
@@ -224,7 +414,7 @@ class NumaConfig {
|
||||
std::optional<std::set<CpuIndex>> allowedCpus;
|
||||
|
||||
if (respectProcessAffinity)
|
||||
allowedCpus = get_process_affinity();
|
||||
allowedCpus = get_process_affinity().get_combined();
|
||||
|
||||
// The affinity can't be determined in all cases on Windows, but we at least guarantee
|
||||
// that the number of allowed processors is >= number of processors in the affinity mask.
|
||||
@@ -233,15 +423,6 @@ class NumaConfig {
|
||||
return !allowedCpus.has_value() || allowedCpus->count(c) == 1;
|
||||
};
|
||||
|
||||
// Since Windows 11 and Windows Server 2022 thread affinities can span
|
||||
// processor groups and can be set as such by a new WinAPI function.
|
||||
static const bool CanAffinitySpanProcessorGroups = []() {
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto SetThreadSelectedCpuSetMasks_f = SetThreadSelectedCpuSetMasks_t(
|
||||
(void (*)()) GetProcAddress(k32, "SetThreadSelectedCpuSetMasks"));
|
||||
return SetThreadSelectedCpuSetMasks_f != nullptr;
|
||||
}();
|
||||
|
||||
WORD numProcGroups = GetActiveProcessorGroupCount();
|
||||
for (WORD procGroup = 0; procGroup < numProcGroups; ++procGroup)
|
||||
{
|
||||
@@ -269,7 +450,8 @@ class NumaConfig {
|
||||
// the new NUMA allocation behaviour was introduced while there was
|
||||
// still no way to set thread affinity spanning multiple processor groups.
|
||||
// See https://learn.microsoft.com/en-us/windows/win32/procthread/numa-support
|
||||
if (!CanAffinitySpanProcessorGroups)
|
||||
// We also do this is if need to force old API for some reason.
|
||||
if (use_old_affinity_api())
|
||||
{
|
||||
NumaConfig splitCfg = empty();
|
||||
|
||||
@@ -307,6 +489,12 @@ class NumaConfig {
|
||||
// We have to ensure no empty NUMA nodes persist.
|
||||
cfg.remove_empty_numa_nodes();
|
||||
|
||||
// If the user explicitly opts out from respecting the current process affinity
|
||||
// then it may be inconsistent with the current affinity (obviously), so we
|
||||
// consider it custom.
|
||||
if (!respectProcessAffinity)
|
||||
cfg.customAffinity = true;
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@@ -510,9 +698,11 @@ class NumaConfig {
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto SetThreadSelectedCpuSetMasks_f = SetThreadSelectedCpuSetMasks_t(
|
||||
(void (*)()) GetProcAddress(k32, "SetThreadSelectedCpuSetMasks"));
|
||||
auto SetThreadGroupAffinity_f =
|
||||
SetThreadGroupAffinity_t((void (*)()) GetProcAddress(k32, "SetThreadGroupAffinity"));
|
||||
|
||||
// We ALWAYS set affinity with the new API if available,
|
||||
// because there's no downsides, and we forcibly keep it consistent
|
||||
// with the old API should we need to use it. I.e. we always keep this as a superset
|
||||
// of what we set with SetThreadGroupAffinity.
|
||||
if (SetThreadSelectedCpuSetMasks_f != nullptr)
|
||||
{
|
||||
// Only available on Windows 11 and Windows Server 2022 onwards.
|
||||
@@ -541,7 +731,9 @@ class NumaConfig {
|
||||
// This is defensive, allowed because this code is not performance critical.
|
||||
SwitchToThread();
|
||||
}
|
||||
else if (SetThreadGroupAffinity_f != nullptr)
|
||||
|
||||
// Sometimes we need to force the old API, but do not use it unless necessary.
|
||||
if (SetThreadSelectedCpuSetMasks_f == nullptr || use_old_affinity_api())
|
||||
{
|
||||
// On earlier windows version (since windows 7) we can't run a single thread
|
||||
// on multiple processor groups, so we need to restrict the group.
|
||||
@@ -576,7 +768,7 @@ class NumaConfig {
|
||||
|
||||
HANDLE hThread = GetCurrentThread();
|
||||
|
||||
const BOOL status = SetThreadGroupAffinity_f(hThread, &affinity, nullptr);
|
||||
const BOOL status = SetThreadGroupAffinity(hThread, &affinity, nullptr);
|
||||
if (status == 0)
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
||||
@@ -665,138 +857,6 @@ class NumaConfig {
|
||||
return true;
|
||||
}
|
||||
|
||||
#if defined(__linux__) && !defined(__ANDROID__)
|
||||
|
||||
static std::set<CpuIndex> get_process_affinity() {
|
||||
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
// For unsupported systems, or in case of a soft error, we may assume all processors
|
||||
// are available for use.
|
||||
[[maybe_unused]] auto set_to_all_cpus = [&]() {
|
||||
for (CpuIndex c = 0; c < SYSTEM_THREADS_NB; ++c)
|
||||
cpus.insert(c);
|
||||
};
|
||||
|
||||
// cpu_set_t by default holds 1024 entries. This may not be enough soon,
|
||||
// but there is no easy way to determine how many threads there actually is.
|
||||
// In this case we just choose a reasonable upper bound.
|
||||
static constexpr CpuIndex MaxNumCpus = 1024 * 64;
|
||||
|
||||
cpu_set_t* mask = CPU_ALLOC(MaxNumCpus);
|
||||
if (mask == nullptr)
|
||||
std::exit(EXIT_FAILURE);
|
||||
|
||||
const size_t masksize = CPU_ALLOC_SIZE(MaxNumCpus);
|
||||
|
||||
CPU_ZERO_S(masksize, mask);
|
||||
|
||||
const int status = sched_getaffinity(0, masksize, mask);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
CPU_FREE(mask);
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (CpuIndex c = 0; c < MaxNumCpus; ++c)
|
||||
if (CPU_ISSET_S(c, masksize, mask))
|
||||
cpus.insert(c);
|
||||
|
||||
CPU_FREE(mask);
|
||||
|
||||
return cpus;
|
||||
}
|
||||
|
||||
#elif defined(_WIN64)
|
||||
|
||||
// On Windows there are two ways to set affinity, and therefore 2 ways to get it.
|
||||
// These are not consistent, so we have to check both.
|
||||
// In some cases it is actually not possible to determine affinity.
|
||||
// For example when two different threads have affinity on different processor groups,
|
||||
// set using SetThreadAffinityMask, we can't retrieve the actual affinities.
|
||||
// From documentation on GetProcessAffinityMask:
|
||||
// > If the calling process contains threads in multiple groups,
|
||||
// > the function returns zero for both affinity masks.
|
||||
// In such cases we just give up and assume we have affinity for all processors.
|
||||
// nullopt means no affinity is set, that is, all processors are allowed
|
||||
static std::optional<std::set<CpuIndex>> get_process_affinity() {
|
||||
HMODULE k32 = GetModuleHandle(TEXT("Kernel32.dll"));
|
||||
auto GetThreadSelectedCpuSetMasks_f = GetThreadSelectedCpuSetMasks_t(
|
||||
(void (*)()) GetProcAddress(k32, "GetThreadSelectedCpuSetMasks"));
|
||||
auto GetProcessAffinityMask_f =
|
||||
GetProcessAffinityMask_t((void (*)()) GetProcAddress(k32, "GetProcessAffinityMask"));
|
||||
auto GetProcessGroupAffinity_f =
|
||||
GetProcessGroupAffinity_t((void (*)()) GetProcAddress(k32, "GetProcessGroupAffinity"));
|
||||
|
||||
if (GetThreadSelectedCpuSetMasks_f != nullptr)
|
||||
{
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
USHORT RequiredMaskCount;
|
||||
GetThreadSelectedCpuSetMasks_f(GetCurrentThread(), nullptr, 0, &RequiredMaskCount);
|
||||
|
||||
// If RequiredMaskCount then these affinities were never set, but it's not consistent
|
||||
// so GetProcessAffinityMask may still return some affinity.
|
||||
if (RequiredMaskCount > 0)
|
||||
{
|
||||
auto groupAffinities = std::make_unique<GROUP_AFFINITY[]>(RequiredMaskCount);
|
||||
|
||||
GetThreadSelectedCpuSetMasks_f(GetCurrentThread(), groupAffinities.get(),
|
||||
RequiredMaskCount, &RequiredMaskCount);
|
||||
|
||||
for (USHORT i = 0; i < RequiredMaskCount; ++i)
|
||||
{
|
||||
const size_t procGroupIndex = groupAffinities[i].Group;
|
||||
|
||||
for (size_t j = 0; j < WIN_PROCESSOR_GROUP_SIZE; ++j)
|
||||
{
|
||||
if (groupAffinities[i].Mask & (KAFFINITY(1) << j))
|
||||
cpus.insert(procGroupIndex * WIN_PROCESSOR_GROUP_SIZE + j);
|
||||
}
|
||||
}
|
||||
|
||||
return cpus;
|
||||
}
|
||||
}
|
||||
|
||||
if (GetProcessAffinityMask_f != nullptr && GetProcessGroupAffinity_f != nullptr)
|
||||
{
|
||||
std::set<CpuIndex> cpus;
|
||||
|
||||
DWORD_PTR proc, sys;
|
||||
BOOL status = GetProcessAffinityMask_f(GetCurrentProcess(), &proc, &sys);
|
||||
if (status == 0)
|
||||
return std::nullopt;
|
||||
|
||||
// We can't determine affinity because it spans processor groups.
|
||||
if (proc == 0)
|
||||
return std::nullopt;
|
||||
|
||||
// We are expecting a single group.
|
||||
USHORT GroupCount = 1;
|
||||
alignas(4) USHORT GroupArray[1];
|
||||
status = GetProcessGroupAffinity_f(GetCurrentProcess(), &GroupCount, GroupArray);
|
||||
if (status == 0 || GroupCount != 1)
|
||||
return std::nullopt;
|
||||
|
||||
const size_t procGroupIndex = GroupArray[0];
|
||||
|
||||
uint64_t mask = static_cast<uint64_t>(proc);
|
||||
for (size_t j = 0; j < WIN_PROCESSOR_GROUP_SIZE; ++j)
|
||||
{
|
||||
if (mask & (KAFFINITY(1) << j))
|
||||
cpus.insert(procGroupIndex * WIN_PROCESSOR_GROUP_SIZE + j);
|
||||
}
|
||||
|
||||
return cpus;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static std::vector<size_t> indices_from_shortened_string(const std::string& s) {
|
||||
std::vector<size_t> indices;
|
||||
|
||||
|
||||
+54
-55
@@ -61,9 +61,9 @@ static constexpr double EvalLevel[10] = {0.981, 0.956, 0.895, 0.949, 0.913,
|
||||
|
||||
// Futility margin
|
||||
Value futility_margin(Depth d, bool noTtCutNode, bool improving, bool oppWorsening) {
|
||||
Value futilityMult = 129 - 43 * noTtCutNode;
|
||||
Value improvingDeduction = 56 * improving * futilityMult / 32;
|
||||
Value worseningDeduction = 336 * oppWorsening * futilityMult / 1024;
|
||||
Value futilityMult = 109 - 40 * noTtCutNode;
|
||||
Value improvingDeduction = 59 * improving * futilityMult / 32;
|
||||
Value worseningDeduction = 328 * oppWorsening * futilityMult / 1024;
|
||||
|
||||
return futilityMult * d - improvingDeduction - worseningDeduction;
|
||||
}
|
||||
@@ -75,15 +75,15 @@ constexpr int futility_move_count(bool improving, Depth depth) {
|
||||
// Add correctionHistory value to raw staticEval and guarantee evaluation does not hit the tablebase range
|
||||
Value to_corrected_static_eval(Value v, const Worker& w, const Position& pos) {
|
||||
auto cv = w.correctionHistory[pos.side_to_move()][pawn_structure_index<Correction>(pos)];
|
||||
v += cv * std::abs(cv) / 5435;
|
||||
v += cv * std::abs(cv) / 4990;
|
||||
return std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
|
||||
}
|
||||
|
||||
// History and stats update bonus, based on depth
|
||||
int stat_bonus(Depth d) { return std::clamp(205 * d - 283, 18, 1544); }
|
||||
int stat_bonus(Depth d) { return std::clamp(186 * d - 285, 20, 1524); }
|
||||
|
||||
// History and stats update malus, based on depth
|
||||
int stat_malus(Depth d) { return (d < 4 ? 767 * d - 275 : 1911); }
|
||||
int stat_malus(Depth d) { return (d < 4 ? 707 * d - 260 : 2073); }
|
||||
|
||||
// Add a small random component to draw evaluations to avoid 3-fold blindness
|
||||
Value value_draw(size_t nodes) { return VALUE_DRAW - 1 + Value(nodes & 0x2); }
|
||||
@@ -138,10 +138,10 @@ void update_all_stats(const Position& pos,
|
||||
|
||||
Search::Worker::Worker(SharedState& sharedState,
|
||||
std::unique_ptr<ISearchManager> sm,
|
||||
size_t thread_id,
|
||||
size_t threadId,
|
||||
NumaReplicatedAccessToken token) :
|
||||
// Unpack the SharedState struct into member variables
|
||||
thread_idx(thread_id),
|
||||
threadIdx(threadId),
|
||||
numaAccessToken(token),
|
||||
manager(std::move(sm)),
|
||||
options(sharedState.options),
|
||||
@@ -356,12 +356,12 @@ void Search::Worker::iterative_deepening() {
|
||||
|
||||
// Reset aspiration window starting size
|
||||
Value avg = rootMoves[pvIdx].averageScore;
|
||||
delta = 9 + avg * avg / 10502;
|
||||
delta = 9 + avg * avg / 10182;
|
||||
alpha = std::max(avg - delta, -VALUE_INFINITE);
|
||||
beta = std::min(avg + delta, VALUE_INFINITE);
|
||||
|
||||
// Adjust optimism based on root move's averageScore (~4 Elo)
|
||||
optimism[us] = 122 * avg / (std::abs(avg) + 92);
|
||||
optimism[us] = 127 * avg / (std::abs(avg) + 86);
|
||||
optimism[~us] = -optimism[us];
|
||||
|
||||
// Start with a small aspiration window and, in the case of a fail
|
||||
@@ -548,17 +548,17 @@ void Search::Worker::clear() {
|
||||
counterMoves.fill(Move::none());
|
||||
mainHistory.fill(0);
|
||||
captureHistory.fill(0);
|
||||
pawnHistory.fill(-1300);
|
||||
pawnHistory.fill(-1193);
|
||||
correctionHistory.fill(0);
|
||||
|
||||
for (bool inCheck : {false, true})
|
||||
for (StatsType c : {NoCaptures, Captures})
|
||||
for (auto& to : continuationHistory[inCheck][c])
|
||||
for (auto& h : to)
|
||||
h->fill(-60);
|
||||
h->fill(-56);
|
||||
|
||||
for (size_t i = 1; i < reductions.size(); ++i)
|
||||
reductions[i] = int((19.90 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
|
||||
reductions[i] = int((19.26 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
|
||||
|
||||
refreshTable.clear(networks[numaAccessToken]);
|
||||
}
|
||||
@@ -602,7 +602,7 @@ Value Search::Worker::search(
|
||||
bool givesCheck, improving, priorCapture, opponentWorsening;
|
||||
bool capture, moveCountPruning, ttCapture;
|
||||
Piece movedPiece;
|
||||
int moveCount, captureCount, quietCount;
|
||||
int moveCount, captureCount, quietCount, futilityMargin;
|
||||
Bound singularBound;
|
||||
|
||||
// Step 1. Initialize node
|
||||
@@ -791,7 +791,7 @@ Value Search::Worker::search(
|
||||
// Use static evaluation difference to improve quiet move ordering (~9 Elo)
|
||||
if (((ss - 1)->currentMove).is_ok() && !(ss - 1)->inCheck && !priorCapture)
|
||||
{
|
||||
int bonus = std::clamp(-11 * int((ss - 1)->staticEval + ss->staticEval), -1592, 1390);
|
||||
int bonus = std::clamp(-10 * int((ss - 1)->staticEval + ss->staticEval), -1590, 1371);
|
||||
bonus = bonus > 0 ? 2 * bonus : bonus / 2;
|
||||
thisThread->mainHistory[~us][((ss - 1)->currentMove).from_to()] << bonus;
|
||||
if (type_of(pos.piece_on(prevSq)) != PAWN && ((ss - 1)->currentMove).type_of() != PROMOTION)
|
||||
@@ -810,10 +810,12 @@ Value Search::Worker::search(
|
||||
|
||||
opponentWorsening = ss->staticEval + (ss - 1)->staticEval > 2;
|
||||
|
||||
futilityMargin = futility_margin(depth, cutNode && !ss->ttHit, improving, opponentWorsening);
|
||||
|
||||
// Step 7. Razoring (~1 Elo)
|
||||
// If eval is really low check with qsearch if it can exceed alpha, if it can't,
|
||||
// return a fail low.
|
||||
if (eval < alpha - 501 - 305 * depth * depth)
|
||||
if (eval < alpha - 465 - futilityMargin * depth * 33 / 32)
|
||||
{
|
||||
value = qsearch<NonPV>(pos, ss, alpha - 1, alpha);
|
||||
if (value < alpha)
|
||||
@@ -822,23 +824,20 @@ Value Search::Worker::search(
|
||||
|
||||
// Step 8. Futility pruning: child node (~40 Elo)
|
||||
// The depth condition is important for mate finding.
|
||||
if (!ss->ttPv && depth < 12
|
||||
&& eval - futility_margin(depth, cutNode && !ss->ttHit, improving, opponentWorsening)
|
||||
- (ss - 1)->statScore / 248
|
||||
>= beta
|
||||
if (!ss->ttPv && depth < 13 && eval - futilityMargin - (ss - 1)->statScore / 263 >= beta
|
||||
&& eval >= beta && eval < VALUE_TB_WIN_IN_MAX_PLY && (!ttMove || ttCapture))
|
||||
return beta > VALUE_TB_LOSS_IN_MAX_PLY ? beta + (eval - beta) / 3 : eval;
|
||||
|
||||
// Step 9. Null move search with verification search (~35 Elo)
|
||||
if (!PvNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 13999
|
||||
&& eval >= beta && ss->staticEval >= beta - 21 * depth + 390 && !excludedMove
|
||||
if (!PvNode && (ss - 1)->currentMove != Move::null() && (ss - 1)->statScore < 14369
|
||||
&& eval >= beta && ss->staticEval >= beta - 21 * depth + 393 && !excludedMove
|
||||
&& pos.non_pawn_material(us) && ss->ply >= thisThread->nmpMinPly
|
||||
&& beta > VALUE_TB_LOSS_IN_MAX_PLY)
|
||||
{
|
||||
assert(eval - beta >= 0);
|
||||
|
||||
// Null move dynamic reduction based on depth and eval
|
||||
Depth R = std::min(int(eval - beta) / 177, 6) + depth / 3 + 5;
|
||||
Depth R = std::min(int(eval - beta) / 197, 6) + depth / 3 + 5;
|
||||
|
||||
ss->currentMove = Move::null();
|
||||
ss->continuationHistory = &thisThread->continuationHistory[0][0][NO_PIECE][0];
|
||||
@@ -896,7 +895,7 @@ Value Search::Worker::search(
|
||||
// Step 11. ProbCut (~10 Elo)
|
||||
// If we have a good enough capture (or queen promotion) and a reduced search returns a value
|
||||
// much above beta, we can (almost) safely prune the previous move.
|
||||
probCutBeta = beta + 185 - 60 * improving;
|
||||
probCutBeta = beta + 177 - 57 * improving;
|
||||
if (
|
||||
!PvNode && depth > 3
|
||||
&& std::abs(beta) < VALUE_TB_WIN_IN_MAX_PLY
|
||||
@@ -953,7 +952,7 @@ Value Search::Worker::search(
|
||||
moves_loop: // When in check, search starts here
|
||||
|
||||
// Step 12. A small Probcut idea, when we are in check (~4 Elo)
|
||||
probCutBeta = beta + 361;
|
||||
probCutBeta = beta + 388;
|
||||
if (ss->inCheck && !PvNode && ttCapture && (tte->bound() & BOUND_LOWER)
|
||||
&& tte->depth() >= depth - 4 && ttValue >= probCutBeta
|
||||
&& std::abs(ttValue) < VALUE_TB_WIN_IN_MAX_PLY && std::abs(beta) < VALUE_TB_WIN_IN_MAX_PLY)
|
||||
@@ -1041,15 +1040,15 @@ moves_loop: // When in check, search starts here
|
||||
// Futility pruning for captures (~2 Elo)
|
||||
if (!givesCheck && lmrDepth < 7 && !ss->inCheck)
|
||||
{
|
||||
Value futilityValue = ss->staticEval + 283 + 235 * lmrDepth
|
||||
Value futilityValue = ss->staticEval + 287 + 248 * lmrDepth
|
||||
+ PieceValue[capturedPiece] + captHist / 7;
|
||||
if (futilityValue <= alpha)
|
||||
continue;
|
||||
}
|
||||
|
||||
// SEE based pruning for captures and checks (~11 Elo)
|
||||
int seeHist = std::clamp(captHist / 32, -183 * depth, 162 * depth);
|
||||
if (!pos.see_ge(move, -166 * depth - seeHist))
|
||||
int seeHist = std::clamp(captHist / 32, -180 * depth, 163 * depth);
|
||||
if (!pos.see_ge(move, -160 * depth - seeHist))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
@@ -1060,18 +1059,18 @@ moves_loop: // When in check, search starts here
|
||||
+ thisThread->pawnHistory[pawn_structure_index(pos)][movedPiece][move.to_sq()];
|
||||
|
||||
// Continuation history based pruning (~2 Elo)
|
||||
if (lmrDepth < 6 && history < -4427 * depth)
|
||||
if (lmrDepth < 6 && history < -4151 * depth)
|
||||
continue;
|
||||
|
||||
history += 2 * thisThread->mainHistory[us][move.from_to()];
|
||||
|
||||
lmrDepth += history / 3670;
|
||||
lmrDepth += history / 3678;
|
||||
|
||||
Value futilityValue =
|
||||
ss->staticEval + (bestValue < ss->staticEval - 51 ? 149 : 55) + 141 * lmrDepth;
|
||||
ss->staticEval + (bestValue < ss->staticEval - 51 ? 138 : 54) + 140 * lmrDepth;
|
||||
|
||||
// Futility pruning: parent node (~13 Elo)
|
||||
if (!ss->inCheck && lmrDepth < 11 && futilityValue <= alpha)
|
||||
if (!ss->inCheck && lmrDepth < 12 && futilityValue <= alpha)
|
||||
{
|
||||
if (bestValue <= futilityValue && std::abs(bestValue) < VALUE_TB_WIN_IN_MAX_PLY
|
||||
&& futilityValue < VALUE_TB_WIN_IN_MAX_PLY)
|
||||
@@ -1082,7 +1081,7 @@ moves_loop: // When in check, search starts here
|
||||
lmrDepth = std::max(lmrDepth, 0);
|
||||
|
||||
// Prune moves with negative SEE (~4 Elo)
|
||||
if (!pos.see_ge(move, -26 * lmrDepth * lmrDepth))
|
||||
if (!pos.see_ge(move, -24 * lmrDepth * lmrDepth))
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -1105,11 +1104,11 @@ moves_loop: // When in check, search starts here
|
||||
// margins scale well.
|
||||
|
||||
if (!rootNode && move == ttMove && !excludedMove
|
||||
&& depth >= 4 - (thisThread->completedDepth > 38) + ss->ttPv
|
||||
&& depth >= 4 - (thisThread->completedDepth > 35) + ss->ttPv
|
||||
&& std::abs(ttValue) < VALUE_TB_WIN_IN_MAX_PLY && (tte->bound() & BOUND_LOWER)
|
||||
&& tte->depth() >= depth - 3)
|
||||
{
|
||||
Value singularBeta = ttValue - (58 + 64 * (ss->ttPv && !PvNode)) * depth / 64;
|
||||
Value singularBeta = ttValue - (52 + 80 * (ss->ttPv && !PvNode)) * depth / 64;
|
||||
Depth singularDepth = newDepth / 2;
|
||||
|
||||
ss->excludedMove = move;
|
||||
@@ -1120,13 +1119,13 @@ moves_loop: // When in check, search starts here
|
||||
|
||||
if (value < singularBeta)
|
||||
{
|
||||
int doubleMargin = 304 * PvNode - 203 * !ttCapture;
|
||||
int tripleMargin = 117 + 259 * PvNode - 296 * !ttCapture + 97 * ss->ttPv;
|
||||
int doubleMargin = 290 * PvNode - 200 * !ttCapture;
|
||||
int tripleMargin = 107 + 247 * PvNode - 278 * !ttCapture + 99 * ss->ttPv;
|
||||
|
||||
extension = 1 + (value < singularBeta - doubleMargin)
|
||||
+ (value < singularBeta - tripleMargin);
|
||||
|
||||
depth += ((!PvNode) && (depth < 16));
|
||||
depth += ((!PvNode) && (depth < 18));
|
||||
}
|
||||
|
||||
// Multi-cut pruning
|
||||
@@ -1153,10 +1152,10 @@ moves_loop: // When in check, search starts here
|
||||
}
|
||||
|
||||
// Extension for capturing the previous moved piece (~0 Elo on STC, ~1 Elo on LTC)
|
||||
else if (PvNode && move == ttMove && move.to_sq() == prevSq
|
||||
else if (PvNode && move.to_sq() == prevSq
|
||||
&& thisThread->captureHistory[movedPiece][move.to_sq()]
|
||||
[type_of(pos.piece_on(move.to_sq()))]
|
||||
> 3988)
|
||||
> 3922)
|
||||
extension = 1;
|
||||
}
|
||||
|
||||
@@ -1212,10 +1211,10 @@ moves_loop: // When in check, search starts here
|
||||
|
||||
ss->statScore = 2 * thisThread->mainHistory[us][move.from_to()]
|
||||
+ (*contHist[0])[movedPiece][move.to_sq()]
|
||||
+ (*contHist[1])[movedPiece][move.to_sq()] - 5169;
|
||||
+ (*contHist[1])[movedPiece][move.to_sq()] - 4747;
|
||||
|
||||
// Decrease/increase reduction for moves with a good/bad history (~8 Elo)
|
||||
r -= ss->statScore / 11049;
|
||||
r -= ss->statScore / 11125;
|
||||
|
||||
// Step 17. Late moves reduction / extension (LMR, ~117 Elo)
|
||||
if (depth >= 2 && moveCount > 1 + rootNode)
|
||||
@@ -1234,7 +1233,7 @@ moves_loop: // When in check, search starts here
|
||||
{
|
||||
// Adjust full-depth search based on LMR results - if the result
|
||||
// was good enough search deeper, if it was bad enough search shallower.
|
||||
const bool doDeeperSearch = value > (bestValue + 36 + 2 * newDepth); // (~1 Elo)
|
||||
const bool doDeeperSearch = value > (bestValue + 35 + 2 * newDepth); // (~1 Elo)
|
||||
const bool doShallowerSearch = value < bestValue + newDepth; // (~2 Elo)
|
||||
|
||||
newDepth += doDeeperSearch - doShallowerSearch;
|
||||
@@ -1395,10 +1394,10 @@ moves_loop: // When in check, search starts here
|
||||
// Bonus for prior countermove that caused the fail low
|
||||
else if (!priorCapture && prevSq != SQ_NONE)
|
||||
{
|
||||
int bonus = (116 * (depth > 5) + 115 * (PvNode || cutNode)
|
||||
+ 186 * ((ss - 1)->statScore < -14144) + 121 * ((ss - 1)->moveCount > 9)
|
||||
+ 64 * (!ss->inCheck && bestValue <= ss->staticEval - 115)
|
||||
+ 137 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 81));
|
||||
int bonus = (113 * (depth > 5) + 118 * (PvNode || cutNode)
|
||||
+ 191 * ((ss - 1)->statScore < -14396) + 119 * ((ss - 1)->moveCount > 8)
|
||||
+ 64 * (!ss->inCheck && bestValue <= ss->staticEval - 107)
|
||||
+ 147 * (!(ss - 1)->inCheck && bestValue <= -(ss - 1)->staticEval - 75));
|
||||
update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
|
||||
stat_bonus(depth) * bonus / 100);
|
||||
thisThread->mainHistory[~us][((ss - 1)->currentMove).from_to()]
|
||||
@@ -1572,7 +1571,7 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
|
||||
if (bestValue > alpha)
|
||||
alpha = bestValue;
|
||||
|
||||
futilityBase = ss->staticEval + 279;
|
||||
futilityBase = ss->staticEval + 294;
|
||||
}
|
||||
|
||||
const PieceToHistory* contHist[] = {(ss - 1)->continuationHistory,
|
||||
@@ -1644,11 +1643,11 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
|
||||
+ (*contHist[1])[pos.moved_piece(move)][move.to_sq()]
|
||||
+ thisThread->pawnHistory[pawn_structure_index(pos)][pos.moved_piece(move)]
|
||||
[move.to_sq()]
|
||||
<= 4181)
|
||||
<= 4452)
|
||||
continue;
|
||||
|
||||
// Do not search moves with bad enough SEE values (~5 Elo)
|
||||
if (!pos.see_ge(move, -67))
|
||||
if (!pos.see_ge(move, -74))
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1712,9 +1711,9 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta,
|
||||
return bestValue;
|
||||
}
|
||||
|
||||
Depth Search::Worker::reduction(bool i, Depth d, int mn, int delta) {
|
||||
Depth Search::Worker::reduction(bool i, Depth d, int mn, int delta) const {
|
||||
int reductionScale = reductions[d] * reductions[mn];
|
||||
return (reductionScale + 1222 - delta * 733 / rootDelta) / 1024 + (!i && reductionScale > 1231);
|
||||
return (reductionScale + 1236 - delta * 746 / rootDelta) / 1024 + (!i && reductionScale > 1326);
|
||||
}
|
||||
|
||||
// elapsed() returns the time elapsed since the search started. If the
|
||||
@@ -1817,7 +1816,7 @@ void update_all_stats(const Position& pos,
|
||||
|
||||
if (!pos.capture_stage(bestMove))
|
||||
{
|
||||
int bestMoveBonus = bestValue > beta + 176 ? quietMoveBonus // larger bonus
|
||||
int bestMoveBonus = bestValue > beta + 164 ? quietMoveBonus // larger bonus
|
||||
: stat_bonus(depth); // smaller bonus
|
||||
|
||||
update_quiet_stats(pos, ss, workerThread, bestMove, bestMoveBonus);
|
||||
@@ -1855,7 +1854,7 @@ void update_all_stats(const Position& pos,
|
||||
// by moves at ply -1, -2, -3, -4, and -6 with current move.
|
||||
void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {
|
||||
|
||||
bonus = bonus * 47 / 64;
|
||||
bonus = bonus * 51 / 64;
|
||||
|
||||
for (int i : {1, 2, 3, 4, 6})
|
||||
{
|
||||
@@ -1863,7 +1862,7 @@ void update_continuation_histories(Stack* ss, Piece pc, Square to, int bonus) {
|
||||
if (ss->inCheck && i > 2)
|
||||
break;
|
||||
if (((ss - i)->currentMove).is_ok())
|
||||
(*(ss - i)->continuationHistory)[pc][to] << bonus / (1 + 3 * (i == 3));
|
||||
(*(ss - i)->continuationHistory)[pc][to] << bonus / (1 + (i == 3));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -254,7 +254,7 @@ class Worker {
|
||||
// It searches from the root position and outputs the "bestmove".
|
||||
void start_searching();
|
||||
|
||||
bool is_mainthread() const { return thread_idx == 0; }
|
||||
bool is_mainthread() const { return threadIdx == 0; }
|
||||
|
||||
// Public because they need to be updatable by the stats
|
||||
CounterMoveHistory counterMoves;
|
||||
@@ -297,12 +297,12 @@ class Worker {
|
||||
template<NodeType nodeType>
|
||||
Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth = 0);
|
||||
|
||||
Depth reduction(bool i, Depth d, int mn, int delta);
|
||||
Depth reduction(bool i, Depth d, int mn, int delta) const;
|
||||
|
||||
// Get a pointer to the search manager, only allowed to be called by the
|
||||
// main thread.
|
||||
SearchManager* main_manager() const {
|
||||
assert(thread_idx == 0);
|
||||
assert(threadIdx == 0);
|
||||
return static_cast<SearchManager*>(manager.get());
|
||||
}
|
||||
|
||||
@@ -323,7 +323,7 @@ class Worker {
|
||||
Depth rootDepth, completedDepth;
|
||||
Value rootDelta;
|
||||
|
||||
size_t thread_idx;
|
||||
size_t threadIdx;
|
||||
NumaReplicatedAccessToken numaAccessToken;
|
||||
|
||||
// Reductions lookup table initialized at startup
|
||||
|
||||
+1
-3
@@ -129,9 +129,7 @@ void Thread::idle_loop() {
|
||||
}
|
||||
}
|
||||
|
||||
Search::SearchManager* ThreadPool::main_manager() {
|
||||
return static_cast<Search::SearchManager*>(main_thread()->worker.get()->manager.get());
|
||||
}
|
||||
Search::SearchManager* ThreadPool::main_manager() { return main_thread()->worker->main_manager(); }
|
||||
|
||||
uint64_t ThreadPool::nodes_searched() const { return accumulate(&Search::Worker::nodes); }
|
||||
uint64_t ThreadPool::tb_hits() const { return accumulate(&Search::Worker::tbHits); }
|
||||
|
||||
+3
-1
@@ -75,9 +75,11 @@ uint8_t TTEntry::relative_age(const uint8_t generation8) const {
|
||||
// measured in megabytes. Transposition table consists
|
||||
// of clusters and each cluster consists of ClusterSize number of TTEntry.
|
||||
void TranspositionTable::resize(size_t mbSize, ThreadPool& threads) {
|
||||
aligned_large_pages_free(table);
|
||||
|
||||
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
|
||||
|
||||
table = make_unique_large_page<Cluster[]>(clusterCount);
|
||||
table = static_cast<Cluster*>(aligned_large_pages_alloc(clusterCount * sizeof(Cluster)));
|
||||
|
||||
if (!table)
|
||||
{
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "memory.h"
|
||||
#include "misc.h"
|
||||
@@ -104,6 +103,7 @@ class TranspositionTable {
|
||||
static constexpr int GENERATION_MASK = (0xFF << GENERATION_BITS) & 0xFF;
|
||||
|
||||
public:
|
||||
~TranspositionTable() { aligned_large_pages_free(table); }
|
||||
void new_search() {
|
||||
// increment by delta to keep lower bits as is
|
||||
generation8 += GENERATION_DELTA;
|
||||
@@ -123,9 +123,9 @@ class TranspositionTable {
|
||||
private:
|
||||
friend struct TTEntry;
|
||||
|
||||
size_t clusterCount;
|
||||
LargePagePtr<Cluster[]> table;
|
||||
uint8_t generation8 = 0; // Size must be not bigger than TTEntry::genBound8
|
||||
size_t clusterCount;
|
||||
Cluster* table = nullptr;
|
||||
uint8_t generation8 = 0; // Size must be not bigger than TTEntry::genBound8
|
||||
};
|
||||
|
||||
} // namespace Stockfish
|
||||
|
||||
+2
-2
@@ -196,8 +196,8 @@ enum : int {
|
||||
// For TT entries where no searching at all was done (whether regular or qsearch) we use
|
||||
// _UNSEARCHED, which should thus compare lower than any QS or regular depth. _ENTRY_OFFSET is used
|
||||
// only for the TT entry occupancy check (see tt.cpp), and should thus be lower than _UNSEARCHED.
|
||||
DEPTH_UNSEARCHED = -6,
|
||||
DEPTH_ENTRY_OFFSET = -7
|
||||
DEPTH_UNSEARCHED = -2,
|
||||
DEPTH_ENTRY_OFFSET = -3
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
|
||||
+1
-1
@@ -301,7 +301,7 @@ void UCIEngine::bench(std::istream& args) {
|
||||
Search::LimitsType limits = parse_limits(is);
|
||||
|
||||
if (limits.perft)
|
||||
nodes = perft(limits);
|
||||
nodesSearched = perft(limits);
|
||||
else
|
||||
{
|
||||
engine.go(limits);
|
||||
|
||||
Reference in New Issue
Block a user