mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00

This patch implements the pure materialistic evaluation called simple_eval() to gain a speed-up during Stockfish search. We use the so-called lazy evaluation trick: replace the accurate but slow NNUE network evaluation by the super-fast simple_eval() if the position seems to be already won (high material advantage). To guard against some of the most obvious blunders introduced by this idea, this patch uses the following features which will raise the lazy evaluation threshold in some situations: - avoid lazy evals on shuffling branches in the search tree - avoid lazy evals if the position at root already has a material imbalance - avoid lazy evals if the search value at root is already winning/losing. Moreover, we add a small random noise to the simple_eval() term. This idea (stochastic mobility in the minimax tree) was worth about 200 Elo in the pure simple_eval() player on Lichess. Overall, the current implementation in this patch evaluates about 2% of the leaves in the search tree lazily. -------------------------------------------- STC: LLR: 2.94 (-2.94,2.94) <0.00,2.00> Total: 60352 W: 15585 L: 15234 D: 29533 Ptnml(0-2): 216, 6906, 15578, 7263, 213 https://tests.stockfishchess.org/tests/view/64f1d9bcbd9967ffae366209 LTC: LLR: 2.94 (-2.94,2.94) <0.50,2.50> Total: 35106 W: 8990 L: 8678 D: 17438 Ptnml(0-2): 14, 3668, 9887, 3960, 24 https://tests.stockfishchess.org/tests/view/64f25204f5b0c54e3f04c0e7 verification run at VLTC: LLR: 2.94 (-2.94,2.94) <0.50,2.50> Total: 74362 W: 19088 L: 18716 D: 36558 Ptnml(0-2): 6, 7226, 22348, 7592, 9 https://tests.stockfishchess.org/tests/view/64f2ecdbf5b0c54e3f04d3ae All three tests above were run with adjudication off, we also verified that there was no regression on matetracker (thanks Disservin!). ---------------------------------------------- closes https://github.com/official-stockfish/Stockfish/pull/4771 Bench: 1393714
140 lines
3.9 KiB
C++
140 lines
3.9 KiB
C++
/*
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
Copyright (C) 2004-2023 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/>.
|
|
*/
|
|
|
|
#ifndef THREAD_H_INCLUDED
|
|
#define THREAD_H_INCLUDED
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
#include "movepick.h"
|
|
#include "position.h"
|
|
#include "search.h"
|
|
#include "thread_win32_osx.h"
|
|
#include "types.h"
|
|
|
|
namespace Stockfish {
|
|
|
|
/// Thread class keeps together all the thread-related stuff. We use
|
|
/// per-thread pawn and material hash tables so that once we get a
|
|
/// pointer to an entry its life time is unlimited and we don't have
|
|
/// to care about someone changing the entry under our feet.
|
|
|
|
class Thread {
|
|
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
size_t idx;
|
|
bool exit = false, searching = true; // Set before starting std::thread
|
|
NativeThread stdThread;
|
|
|
|
public:
|
|
explicit Thread(size_t);
|
|
virtual ~Thread();
|
|
virtual void search();
|
|
void clear();
|
|
void idle_loop();
|
|
void start_searching();
|
|
void wait_for_search_finished();
|
|
size_t id() const { return idx; }
|
|
|
|
size_t pvIdx, pvLast;
|
|
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
|
|
int selDepth, nmpMinPly;
|
|
Value bestValue, optimism[COLOR_NB];
|
|
|
|
Position rootPos;
|
|
StateInfo rootState;
|
|
Search::RootMoves rootMoves;
|
|
Depth rootDepth, completedDepth;
|
|
Value rootDelta;
|
|
Value rootSimpleEval;
|
|
CounterMoveHistory counterMoves;
|
|
ButterflyHistory mainHistory;
|
|
CapturePieceToHistory captureHistory;
|
|
ContinuationHistory continuationHistory[2][2];
|
|
};
|
|
|
|
|
|
/// MainThread is a derived class specific for main thread
|
|
|
|
struct MainThread : public Thread {
|
|
|
|
using Thread::Thread;
|
|
|
|
void search() override;
|
|
void check_time();
|
|
|
|
double previousTimeReduction;
|
|
Value bestPreviousScore;
|
|
Value bestPreviousAverageScore;
|
|
Value iterValue[4];
|
|
int callsCnt;
|
|
bool stopOnPonderhit;
|
|
std::atomic_bool ponder;
|
|
};
|
|
|
|
|
|
/// ThreadPool struct handles all the threads-related stuff like init, starting,
|
|
/// parking and, most importantly, launching a thread. All the access to threads
|
|
/// is done through this class.
|
|
|
|
struct ThreadPool {
|
|
|
|
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
|
void clear();
|
|
void set(size_t);
|
|
|
|
MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
|
|
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
|
|
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
|
|
Thread* get_best_thread() const;
|
|
void start_searching();
|
|
void wait_for_search_finished() const;
|
|
|
|
std::atomic_bool stop, increaseDepth;
|
|
|
|
auto cbegin() const noexcept { return threads.cbegin(); }
|
|
auto begin() noexcept { return threads.begin(); }
|
|
auto end() noexcept { return threads.end(); }
|
|
auto cend() const noexcept { return threads.cend(); }
|
|
auto size() const noexcept { return threads.size(); }
|
|
auto empty() const noexcept { return threads.empty(); }
|
|
|
|
private:
|
|
StateListPtr setupStates;
|
|
std::vector<Thread*> threads;
|
|
|
|
uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
|
|
|
|
uint64_t sum = 0;
|
|
for (Thread* th : threads)
|
|
sum += (th->*member).load(std::memory_order_relaxed);
|
|
return sum;
|
|
}
|
|
};
|
|
|
|
extern ThreadPool Threads;
|
|
|
|
} // namespace Stockfish
|
|
|
|
#endif // #ifndef THREAD_H_INCLUDED
|