Lazy smp #145
No files matched your search
+12
-12
@@ -207,18 +207,18 @@ static bool ShouldStop(SearchContext* ctx) {
|
|||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx->nodes % 1024 == 0) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
searchStopped = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx->nodes > ctx->nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
searchStopped = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
searchStopped = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -314,7 +314,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
}
|
}
|
||||||
|
|
||||||
results.score = bestScore;
|
results.score = bestScore;
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) return 0;
|
if (searchStopped) return 0;
|
||||||
if (!ctx->excluded) {
|
if (!ctx->excluded) {
|
||||||
ctx->TT->WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove, ttpv);
|
ctx->TT->WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove, ttpv);
|
||||||
}
|
}
|
||||||
@@ -410,7 +410,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
||||||
|
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) return 0;
|
if (searchStopped) return 0;
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
if (depth <= 14 || ctx->minNmpPly > 0) {
|
if (depth <= 14 || ctx->minNmpPly > 0) {
|
||||||
return score > MATE_SCORE - MAX_DEPTH ? beta : score;
|
return score > MATE_SCORE - MAX_DEPTH ? beta : score;
|
||||||
@@ -469,7 +469,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ply + 1, ctx, !cutnode).score;
|
ply + 1, ctx, !cutnode).score;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) return 0;
|
if (searchStopped) return 0;
|
||||||
|
|
||||||
if (score >= probcutBeta) {
|
if (score >= probcutBeta) {
|
||||||
ctx->TT->WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove, ttpv);
|
ctx->TT->WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove, ttpv);
|
||||||
@@ -640,7 +640,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ctx->nodesTable[currMove % 4096] += ctx->nodes - nodesBeforeSearch;
|
ctx->nodesTable[currMove % 4096] += ctx->nodes - nodesBeforeSearch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) return 0;
|
if (searchStopped) return 0;
|
||||||
|
|
||||||
if (currMove != 0 && currMove.IsQuiet()) {
|
if (currMove != 0 && currMove.IsQuiet()) {
|
||||||
seenQuiets[seenQuietsCount] = currMove;
|
seenQuiets[seenQuietsCount] = currMove;
|
||||||
@@ -754,7 +754,7 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) return 0;
|
if (searchStopped) return 0;
|
||||||
if (!ctx->excluded) {
|
if (!ctx->excluded) {
|
||||||
|
|
||||||
if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove))
|
if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove))
|
||||||
@@ -864,7 +864,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
|
|
||||||
aw.Set(currentResults.score);
|
aw.Set(currentResults.score);
|
||||||
|
|
||||||
if (searchStopped.load(std::memory_order_relaxed)) {
|
if (searchStopped) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (currentResults.bestMove) {
|
if (currentResults.bestMove) {
|
||||||
@@ -877,13 +877,13 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
|
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx->sw.GetElapsedMS() >= softTime) {
|
if (ctx->sw.GetElapsedMS() >= softTime) {
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx->nodes >= DATAGEN::SOFT_NODES) {
|
if (ctx->nodes >= DATAGEN::SOFT_NODES) {
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
searchStopped = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -895,7 +895,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
searchStopped.store(false, std::memory_order_relaxed);
|
searchStopped = false;
|
||||||
ctx->seldepth = 0;
|
ctx->seldepth = 0;
|
||||||
ctx->nodesTable = {};
|
ctx->nodesTable = {};
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
TTable SharedTT;
|
TTable SharedTT;
|
||||||
|
|
||||||
void TTable::WriteEntry(const U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) {
|
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) {
|
||||||
TTBucket *bucket = &table[hashKey % table.size()];
|
TTBucket *bucket = &table[hashKey % table.size()];
|
||||||
TTEntry *current = nullptr;
|
TTEntry *current = nullptr;
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -68,11 +68,11 @@ public:
|
|||||||
table.resize(size);
|
table.resize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrefetchEntry(const U64 &hashKey) {
|
void PrefetchEntry(U64 &hashKey) {
|
||||||
__builtin_prefetch(&table[hashKey % table.size()]);
|
__builtin_prefetch(&table[hashKey % table.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
TTEntry GetEntry(const U64 &hashKey) {
|
TTEntry GetEntry(U64 &hashKey) {
|
||||||
TTBucket *current = &table[hashKey % table.size()];
|
TTBucket *current = &table[hashKey % table.size()];
|
||||||
if (current->depthPreferred.hashKey == hashKey) {
|
if (current->depthPreferred.hashKey == hashKey) {
|
||||||
return current->depthPreferred;
|
return current->depthPreferred;
|
||||||
@@ -86,16 +86,15 @@ public:
|
|||||||
// returns used space per mill
|
// returns used space per mill
|
||||||
int GetUsedPercentage() {
|
int GetUsedPercentage() {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
const U64 sampleSize = std::min<U64>(1000, table.size());
|
|
||||||
|
|
||||||
for (U64 i = 0; i < sampleSize; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
if (table[i].depthPreferred || table[i].alwaysReplace) count++;
|
if (table[i].depthPreferred || table[i].alwaysReplace) count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteEntry(const U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
|
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern TTable SharedTT;
|
extern TTable SharedTT;
|
||||||
+8
-38
@@ -4,7 +4,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -89,38 +88,27 @@ static double ReadParam(const std::string& param, const std::string &command) {
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Windows implementation using std::thread
|
// Windows implementation using std::thread
|
||||||
static std::vector<std::thread> activeThreads;
|
|
||||||
|
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
static void ThreadFunc(Board board, SearchParams params, SEARCH::SearchContext* ctx) {
|
static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
SEARCH::SearchPosition<mode>(board, params, ctx);
|
SEARCH::SearchPosition<mode>(*board, params, ctx);
|
||||||
delete ctx;
|
delete ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||||
|
std::thread searchThread;
|
||||||
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||||
if (id == 0)
|
if (id == 0)
|
||||||
ctxCopy->doPrint = true;
|
ctxCopy->doPrint = true;
|
||||||
if (params.nodes) {
|
if (params.nodes) {
|
||||||
activeThreads.emplace_back(ThreadFunc<SEARCH::nodesMode>, board, params, ctxCopy);
|
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctxCopy);
|
||||||
} else {
|
} else {
|
||||||
activeThreads.emplace_back(ThreadFunc<SEARCH::normal>, board, params, ctxCopy);
|
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctxCopy);
|
||||||
}
|
}
|
||||||
}
|
searchThread.detach();
|
||||||
|
|
||||||
static void WaitForSearchThreads() {
|
|
||||||
for (auto &thread : activeThreads) {
|
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
activeThreads.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
// Unix/Linux implementation using pthread
|
// Unix/Linux implementation using pthread
|
||||||
static std::vector<pthread_t> activeThreads;
|
|
||||||
|
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
static void* ThreadFunc(void* arg) {
|
static void* ThreadFunc(void* arg) {
|
||||||
auto* tup = static_cast<std::tuple<Board, SearchParams, SEARCH::SearchContext*>*>(arg);
|
auto* tup = static_cast<std::tuple<Board, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||||
@@ -150,22 +138,10 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC
|
|||||||
}
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
activeThreads.push_back(thread);
|
pthread_detach(thread);
|
||||||
}
|
|
||||||
|
|
||||||
static void WaitForSearchThreads() {
|
|
||||||
for (pthread_t thread : activeThreads) {
|
|
||||||
pthread_join(thread, nullptr);
|
|
||||||
}
|
|
||||||
activeThreads.clear();
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void StopAndWaitForSearchThreads() {
|
|
||||||
searchStopped.store(true, std::memory_order_relaxed);
|
|
||||||
WaitForSearchThreads();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
SearchParams params;
|
SearchParams params;
|
||||||
|
|
||||||
@@ -184,8 +160,6 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
params.btime = 99999999;
|
params.btime = 99999999;
|
||||||
}
|
}
|
||||||
|
|
||||||
StopAndWaitForSearchThreads();
|
|
||||||
|
|
||||||
for (int i = 0; i < threads; i++) {
|
for (int i = 0; i < threads; i++) {
|
||||||
StartSearchThread(board, params, ctx, i);
|
StartSearchThread(board, params, ctx, i);
|
||||||
}
|
}
|
||||||
@@ -193,14 +167,12 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
|
|
||||||
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("Hash") != std::string::npos) {
|
if (command.find("Hash") != std::string::npos) {
|
||||||
StopAndWaitForSearchThreads();
|
|
||||||
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
||||||
ctx->TT->Resize(hashSize);
|
ctx->TT->Resize(hashSize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command.find("Threads") != std::string::npos) {
|
if (command.find("Threads") != std::string::npos) {
|
||||||
StopAndWaitForSearchThreads();
|
|
||||||
threads = ReadParam("value", command);
|
threads = ReadParam("value", command);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -284,7 +256,6 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "ucinewgame" command
|
// parse UCI "ucinewgame" command
|
||||||
if (input.find("ucinewgame") != std::string::npos) {
|
if (input.find("ucinewgame") != std::string::npos) {
|
||||||
StopAndWaitForSearchThreads();
|
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
|
||||||
// Clearing
|
// Clearing
|
||||||
@@ -308,7 +279,6 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "quit" command
|
// parse UCI "quit" command
|
||||||
if (input.find("quit") != std::string::npos) {
|
if (input.find("quit") != std::string::npos) {
|
||||||
StopAndWaitForSearchThreads();
|
|
||||||
// stop the loop
|
// stop the loop
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -316,7 +286,7 @@ void UCILoop(Board &board) {
|
|||||||
// parse UCI "stop" command
|
// parse UCI "stop" command
|
||||||
if (input.find("stop") != std::string::npos) {
|
if (input.find("stop") != std::string::npos) {
|
||||||
// stop the loop
|
// stop the loop
|
||||||
StopAndWaitForSearchThreads();
|
searchStopped = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -1,11 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include <atomic>
|
|
||||||
|
|
||||||
inline bool UCIEnabled = false;
|
inline bool UCIEnabled = false;
|
||||||
inline int threads = 1;
|
inline int threads = 1;
|
||||||
inline std::atomic_bool searchStopped = false;
|
inline bool searchStopped = false;
|
||||||
|
|
||||||
class SearchParams {
|
class SearchParams {
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in new issue
Block a user