changed std::thread to pthread | bench 10099292
This commit is contained in:
1 parent
82255ee46d
commit
462cb5b8ef
4 files changed
+69
-26
No files matched your search
@@ -28,8 +28,8 @@ else
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CXXFLAGS := -std=c++20 -O0 -flto -Wall -g
|
CXXFLAGS := -std=c++20 -O3 -flto -Wall
|
||||||
LDFLAGS := -O0 -flto
|
LDFLAGS := -O3 -flto
|
||||||
|
|
||||||
ifeq ($(PLATFORM),mac)
|
ifeq ($(PLATFORM),mac)
|
||||||
ifeq ($(ARCH),arm64)
|
ifeq ($(ARCH),arm64)
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
constexpr int BENCH_DEPTH = 5;
|
constexpr int BENCH_DEPTH = 13;
|
||||||
|
|
||||||
void RunBenchmark();
|
void RunBenchmark();
|
||||||
+33
-13
@@ -2,6 +2,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <tuple>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
@@ -54,7 +56,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
bool isLegal = copy.MakeMove(board.moveList[i]);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
if (!isLegal) continue;
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return moveSeen == 0;
|
return moveSeen == 0;
|
||||||
@@ -66,7 +68,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
|
|
||||||
std::uniform_int_distribution<int> moveDist(0, 1);
|
std::uniform_int_distribution<int> moveDist(0, 1);
|
||||||
bool plusOne = moveDist(rng);
|
bool plusOne = moveDist(rng);
|
||||||
|
|
||||||
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
|
||||||
@@ -141,7 +143,7 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
while (!stopFlag) {
|
while (!stopFlag) {
|
||||||
Board board;
|
Board board;
|
||||||
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
|
||||||
PlayRandMoves(board, ctx.get());
|
PlayRandMoves(board, ctx.get());
|
||||||
if (IsGameOver(board, ctx.get())) continue;
|
if (IsGameOver(board, ctx.get())) continue;
|
||||||
|
|
||||||
@@ -210,11 +212,31 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::atomic<int> positions = 0;
|
std::atomic<int> positions = 0;
|
||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
|
|
||||||
// Start N-1 threads
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
for (int i = 0; i < threads-1; i++) {
|
pthread_attr_t attr;
|
||||||
workerThreads.emplace_back(std::thread(PlayGames, i, std::ref(positions), std::ref(stopFlag)));
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 2 * 1024 * 1024); // 2 MB stack
|
||||||
|
|
||||||
|
auto* args = new std::tuple<int, std::atomic<int>*, std::atomic<bool>*>(i, &positions, &stopFlag);
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
if (pthread_create(&thread, &attr,
|
||||||
|
[](void* a) -> void* {
|
||||||
|
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(a);
|
||||||
|
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||||
|
delete tup;
|
||||||
|
return nullptr;
|
||||||
|
}, args) == 0)
|
||||||
|
{
|
||||||
|
workerThreads.push_back(thread);
|
||||||
|
} else {
|
||||||
|
std::cerr << "Failed to create thread " << i << "\n";
|
||||||
|
delete args;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
@@ -228,14 +250,12 @@ void Run(int targetPositions, int threads) {
|
|||||||
|
|
||||||
stopFlag = true;
|
stopFlag = true;
|
||||||
|
|
||||||
for (auto& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
if (thread.joinable()) {
|
pthread_join(thread, nullptr);
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#else
|
#else
|
||||||
ShowCursorLinux();
|
ShowCursorLinux();
|
||||||
@@ -278,7 +298,7 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
std::cout << std::fixed << std::setprecision(0);
|
std::cout << std::fixed << std::setprecision(0);
|
||||||
|
|
||||||
std::ostringstream datagenTextStream;
|
std::ostringstream datagenTextStream;
|
||||||
datagenTextStream << std::fixed << std::setprecision(2)
|
datagenTextStream << std::fixed << std::setprecision(2)
|
||||||
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
||||||
std::string datagenText = datagenTextStream.str();
|
std::string datagenText = datagenTextStream.str();
|
||||||
int datagenPadding = (width - datagenText.length()) / 2;
|
int datagenPadding = (width - datagenText.length()) / 2;
|
||||||
|
|||||||
+33
-10
@@ -4,6 +4,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <tuple>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -69,6 +71,33 @@ static int ReadParam(std::string param, std::string &command) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <SEARCH::searchMode mode>
|
||||||
|
static void* ThreadFunc(void* arg) {
|
||||||
|
auto* tup = static_cast<std::tuple<Board*, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||||
|
SEARCH::SearchPosition<mode>(*std::get<0>(*tup), std::get<1>(*tup), std::get<2>(*tup));
|
||||||
|
delete tup;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 2 * 1024 * 1024); // 2 MB stack
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
|
||||||
|
if (params.nodes) {
|
||||||
|
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||||
|
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
|
||||||
|
} else {
|
||||||
|
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||||
|
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
pthread_detach(thread);
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -87,13 +116,7 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
params.btime = 99999999;
|
params.btime = 99999999;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.nodes) {
|
StartSearchThread(board, params, ctx);
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::nodesMode>, std::ref(board), params, ctx);
|
|
||||||
worker.detach();
|
|
||||||
} else {
|
|
||||||
auto worker = std::thread(SEARCH::SearchPosition<SEARCH::normal>, std::ref(board), params, ctx);
|
|
||||||
worker.detach();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
|
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
|
||||||
@@ -136,10 +159,10 @@ 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) {
|
||||||
board.SetByFen(StartingFen);
|
board.SetByFen(StartingFen);
|
||||||
|
|
||||||
// Clearing
|
// Clearing
|
||||||
ctx->TT.Clear();
|
ctx->TT.Clear();
|
||||||
|
|
||||||
ctx->killerMoves = {};
|
ctx->killerMoves = {};
|
||||||
ctx->history.Clear();
|
ctx->history.Clear();
|
||||||
ctx->conthist.Clear();
|
ctx->conthist.Clear();
|
||||||
@@ -151,7 +174,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "go" command
|
// parse UCI "go" command
|
||||||
if (input.find("go") != std::string::npos) {
|
if (input.find("go") != std::string::npos) {
|
||||||
ParseGo(board, input, ctx.get());
|
ParseGo(board, input, ctx.get());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user