fixed windows compatibility | bench 1743358
This commit is contained in:
1 parent
345c6d21d9
commit
ce3ed3b6d1
2 files changed
+181
-89
No files matched your search
@@ -3,7 +3,6 @@ SRC_DIR := source
|
|||||||
OBJ_DIR := obj
|
OBJ_DIR := obj
|
||||||
FMT_DIR := external/fmt
|
FMT_DIR := external/fmt
|
||||||
|
|
||||||
CXX := clang++
|
|
||||||
EXE ?= Eleanor
|
EXE ?= Eleanor
|
||||||
EVALFILE := ./nnue.bin
|
EVALFILE := ./nnue.bin
|
||||||
|
|
||||||
@@ -14,6 +13,7 @@ ifeq ($(OS),Windows_NT)
|
|||||||
RM := rmdir /s /q
|
RM := rmdir /s /q
|
||||||
DEL := del /q
|
DEL := del /q
|
||||||
PLATFORM := windows
|
PLATFORM := windows
|
||||||
|
CXX := g++
|
||||||
else
|
else
|
||||||
UNAME_S := $(shell uname -s)
|
UNAME_S := $(shell uname -s)
|
||||||
ARCH := $(shell uname -m)
|
ARCH := $(shell uname -m)
|
||||||
@@ -21,6 +21,7 @@ else
|
|||||||
MKDIR := mkdir -p
|
MKDIR := mkdir -p
|
||||||
RM := rm -rf
|
RM := rm -rf
|
||||||
DEL := rm -f
|
DEL := rm -f
|
||||||
|
CXX := clang++
|
||||||
ifeq ($(UNAME_S),Darwin)
|
ifeq ($(UNAME_S),Darwin)
|
||||||
PLATFORM := mac
|
PLATFORM := mac
|
||||||
else
|
else
|
||||||
@@ -33,14 +34,15 @@ LDFLAGS := -O3 -flto
|
|||||||
|
|
||||||
ifeq ($(PLATFORM),mac)
|
ifeq ($(PLATFORM),mac)
|
||||||
ifeq ($(ARCH),arm64)
|
ifeq ($(ARCH),arm64)
|
||||||
# Apple Silicon
|
|
||||||
ARCH_FLAGS := -mcpu=apple-m1
|
ARCH_FLAGS := -mcpu=apple-m1
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ARCH_FLAGS := -march=native
|
||||||
endif
|
endif
|
||||||
|
else ifeq ($(PLATFORM),windows)
|
||||||
|
ARCH_FLAGS := -march=native
|
||||||
|
LDFLAGS += -static
|
||||||
else
|
else
|
||||||
ifeq ($(ARCH),aarch64)
|
ifeq ($(ARCH),aarch64)
|
||||||
# Generic Linux ARM64
|
|
||||||
ARCH_FLAGS := -mcpu=native
|
ARCH_FLAGS := -mcpu=native
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ARCH_FLAGS := -march=native
|
||||||
|
|||||||
+176
-86
@@ -6,29 +6,61 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "movegen.h"
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <io.h> // for _commit
|
#include <io.h> // for _commit (if you ever use it)
|
||||||
|
#include <process.h> // _beginthreadex / _endthreadex
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h> // for sync
|
#include <unistd.h> // for sync
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static std::filesystem::path MakeTempPath(const std::string& prefix, const std::string& ext) {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
fs::path dir = fs::temp_directory_path();
|
||||||
|
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937_64 gen(rd());
|
||||||
|
std::uniform_int_distribution<uint64_t> dist;
|
||||||
|
auto name = prefix + std::to_string(dist(gen)) + ext;
|
||||||
|
|
||||||
|
return dir / name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string ShellQuote(const std::string& s) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
std::string out = "\"";
|
||||||
|
for (char c : s) {
|
||||||
|
if (c == '"') out += "\\\"";
|
||||||
|
else out += c;
|
||||||
|
}
|
||||||
|
out += "\"";
|
||||||
|
return out;
|
||||||
|
#else
|
||||||
|
std::string out = "'";
|
||||||
|
for (char c : s) {
|
||||||
|
if (c == '\'') out += "'\\''";
|
||||||
|
else out += c;
|
||||||
|
}
|
||||||
|
out += "'";
|
||||||
|
return out;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static void HideCursor() {
|
static void HideCursor() {
|
||||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
@@ -46,13 +78,8 @@ static void ShowCursor() {
|
|||||||
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
static void HideCursor() {
|
static void HideCursor() { std::cout << "\033[?25l"; }
|
||||||
std::cout << "\033[?25l";
|
static void ShowCursor() { std::cout << "\033[?25h"; }
|
||||||
}
|
|
||||||
|
|
||||||
static void ShowCursor() {
|
|
||||||
std::cout << "\033[?25h";
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
@@ -62,22 +89,31 @@ namespace DATAGEN {
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
static bool PostJSON(const std::string& url, const std::string& jsonBody, std::string& response) {
|
static bool PostJSON(const std::string& url, const std::string& jsonBody, std::string& response) {
|
||||||
std::string tempFile = "/tmp/datagen_request.json";
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
fs::path tempFile = MakeTempPath("datagen_request_", ".json");
|
||||||
|
fs::path responseFile = MakeTempPath("datagen_response_", ".txt");
|
||||||
|
|
||||||
std::ofstream out(tempFile);
|
std::ofstream out(tempFile);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
std::cerr << "Failed to create temp file for request" << std::endl;
|
std::cerr << "Failed to create temp file for request: " << tempFile.string() << "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out << jsonBody;
|
out << jsonBody;
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
std::string responseFile = "/tmp/datagen_response.txt";
|
|
||||||
std::ostringstream cmd;
|
std::ostringstream cmd;
|
||||||
cmd << "curl -s -X POST \"" << url << "\" "
|
cmd << "curl -s -X POST "
|
||||||
<< "-H \"Content-Type: application/json\" "
|
<< ShellQuote(url) << " "
|
||||||
<< "-d @" << tempFile << " "
|
<< "-H " << ShellQuote("Content-Type: application/json") << " "
|
||||||
<< "-o " << responseFile << " "
|
<< "-d " << ShellQuote("@" + tempFile.string()) << " "
|
||||||
<< "2>/dev/null";
|
<< "-o " << ShellQuote(responseFile.string()) << " ";
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
cmd << "2>nul";
|
||||||
|
#else
|
||||||
|
cmd << "2>/dev/null";
|
||||||
|
#endif
|
||||||
|
|
||||||
int result = system(cmd.str().c_str());
|
int result = system(cmd.str().c_str());
|
||||||
|
|
||||||
@@ -86,23 +122,34 @@ static bool PostJSON(const std::string& url, const std::string& jsonBody, std::s
|
|||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << responseIn.rdbuf();
|
buffer << responseIn.rdbuf();
|
||||||
response = buffer.str();
|
response = buffer.str();
|
||||||
responseIn.close();
|
} else {
|
||||||
|
response.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::remove(tempFile.c_str());
|
std::error_code ec;
|
||||||
std::remove(responseFile.c_str());
|
fs::remove(tempFile, ec);
|
||||||
|
fs::remove(responseFile, ec);
|
||||||
|
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool UploadFile(const std::string& url, const std::string& filepath,
|
static bool UploadFile(const std::string& url,
|
||||||
const std::string& username, int positions) {
|
const std::string& filepath,
|
||||||
|
const std::string& username,
|
||||||
|
int positions) {
|
||||||
std::ostringstream cmd;
|
std::ostringstream cmd;
|
||||||
cmd << "curl -s -X POST \"" << url << "\" "
|
|
||||||
<< "-F \"file=@" << filepath << "\" "
|
cmd << "curl -s -X POST "
|
||||||
<< "-F \"user=" << username << "\" "
|
<< ShellQuote(url) << " "
|
||||||
<< "-F \"positions=" << positions << "\" "
|
<< "-F " << ShellQuote("file=@" + filepath) << " "
|
||||||
<< "2>/dev/null";
|
<< "-F " << ShellQuote("user=" + username) << " "
|
||||||
|
<< "-F " << ShellQuote("positions=" + std::to_string(positions)) << " ";
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
cmd << "2>nul";
|
||||||
|
#else
|
||||||
|
cmd << "2>/dev/null";
|
||||||
|
#endif
|
||||||
|
|
||||||
int result = system(cmd.str().c_str());
|
int result = system(cmd.str().c_str());
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
@@ -126,30 +173,24 @@ static std::string BuildHeartbeatJSON(const std::string& username, int threads)
|
|||||||
return json.str();
|
return json.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple JSON value extractor
|
|
||||||
static std::string ExtractJSONValue(const std::string& json, const std::string& key) {
|
static std::string ExtractJSONValue(const std::string& json, const std::string& key) {
|
||||||
// Look for "key":"value" or "key":value pattern
|
|
||||||
std::string searchKey = "\"" + key + "\":";
|
std::string searchKey = "\"" + key + "\":";
|
||||||
size_t pos = json.find(searchKey);
|
size_t pos = json.find(searchKey);
|
||||||
if (pos == std::string::npos) return "";
|
if (pos == std::string::npos) return "";
|
||||||
|
|
||||||
pos += searchKey.length();
|
pos += searchKey.length();
|
||||||
|
|
||||||
// Skip whitespace
|
|
||||||
while (pos < json.length() && (json[pos] == ' ' || json[pos] == '\t' || json[pos] == '\n')) {
|
while (pos < json.length() && (json[pos] == ' ' || json[pos] == '\t' || json[pos] == '\n')) {
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos >= json.length()) return "";
|
if (pos >= json.length()) return "";
|
||||||
|
|
||||||
// Check if value is quoted
|
|
||||||
if (json[pos] == '"') {
|
if (json[pos] == '"') {
|
||||||
pos++; // Skip opening quote
|
pos++;
|
||||||
size_t endPos = json.find("\"", pos);
|
size_t endPos = json.find("\"", pos);
|
||||||
if (endPos == std::string::npos) return "";
|
if (endPos == std::string::npos) return "";
|
||||||
return json.substr(pos, endPos - pos);
|
return json.substr(pos, endPos - pos);
|
||||||
} else {
|
} else {
|
||||||
// Unquoted value (number, boolean, etc.)
|
|
||||||
size_t endPos = pos;
|
size_t endPos = pos;
|
||||||
while (endPos < json.length() && json[endPos] != ',' && json[endPos] != '}' && json[endPos] != ']') {
|
while (endPos < json.length() && json[endPos] != ',' && json[endPos] != '}' && json[endPos] != ']') {
|
||||||
endPos++;
|
endPos++;
|
||||||
@@ -206,11 +247,9 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
int moveSeen = 0;
|
int moveSeen = 0;
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
|
||||||
if (!board.IsLegal(board.moveList[i])) continue;
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,6 +266,10 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
|
if (board.currentMoveIndex <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||||
|
|
||||||
Move currMove = board.moveList[dist(rng)];
|
Move currMove = board.moveList[dist(rng)];
|
||||||
@@ -234,9 +277,11 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
count--;
|
count--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
board.MakeMove(currMove);
|
board.MakeMove(currMove);
|
||||||
|
|
||||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
if (IsGameOver(board, ctx)) break;
|
if (IsGameOver(board, ctx)) break;
|
||||||
}
|
}
|
||||||
@@ -433,23 +478,79 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gamesBuffer.size() > 0) {
|
if (!gamesBuffer.empty()) {
|
||||||
WriteToFile(gamesBuffer, basePath, fileCounter);
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
gamesBuffer.clear();
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!stopFlag.load() && !g_shutdownRequested.load()) {
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static void CreateWorkerThread(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag, std::vector<std::thread>& threads) {
|
|
||||||
threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag));
|
struct WinThreadArgs {
|
||||||
|
int id;
|
||||||
|
std::atomic<int>* positions;
|
||||||
|
std::atomic<bool>* stopFlag;
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned __stdcall WinThreadEntry(void* p) {
|
||||||
|
std::unique_ptr<WinThreadArgs> args(static_cast<WinThreadArgs*>(p));
|
||||||
|
PlayGames(args->id, *args->positions, *args->stopFlag);
|
||||||
|
_endthreadex(0);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool CreateWorkerThread(int id,
|
||||||
|
std::atomic<int>& positions,
|
||||||
|
std::atomic<bool>& stopFlag,
|
||||||
|
std::vector<HANDLE>& threads,
|
||||||
|
size_t stackSizeBytes) {
|
||||||
|
auto* args = new WinThreadArgs{ id, &positions, &stopFlag };
|
||||||
|
|
||||||
|
unsigned tid = 0;
|
||||||
|
HANDLE h = reinterpret_cast<HANDLE>(
|
||||||
|
_beginthreadex(
|
||||||
|
nullptr,
|
||||||
|
static_cast<unsigned>(stackSizeBytes),
|
||||||
|
&WinThreadEntry,
|
||||||
|
args,
|
||||||
|
0,
|
||||||
|
&tid
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!h) {
|
||||||
|
std::cerr << "Failed to create thread " << id << " (errno=" << errno << ")\n";
|
||||||
|
delete args;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
threads.push_back(h);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void JoinWorkerThreads(std::vector<HANDLE>& threads) {
|
||||||
|
for (HANDLE h : threads) {
|
||||||
|
if (h) {
|
||||||
|
WaitForSingleObject(h, INFINITE);
|
||||||
|
CloseHandle(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
threads.clear();
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static void* ThreadFunc(void* arg) {
|
static void* ThreadFunc(void* arg) {
|
||||||
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
||||||
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||||
delete tup;
|
delete tup;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -471,10 +572,13 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<HANDLE> workerThreads;
|
||||||
|
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||||
|
|
||||||
|
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull; // change to 16/32 MiB if needed
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
std::vector<pthread_t> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
@@ -511,11 +615,7 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
for (std::thread& thread : workerThreads) {
|
JoinWorkerThreads(workerThreads);
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
for (pthread_t& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
pthread_join(thread, nullptr);
|
pthread_join(thread, nullptr);
|
||||||
@@ -546,7 +646,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::cout << "===================" << std::endl << std::endl;
|
std::cout << "===================" << std::endl << std::endl;
|
||||||
|
|
||||||
while (!g_shutdownRequested) {
|
while (!g_shutdownRequested) {
|
||||||
// Send initial heartbeat
|
|
||||||
std::cout << "Sending heartbeat..." << std::endl;
|
std::cout << "Sending heartbeat..." << std::endl;
|
||||||
std::string heartbeatResponse;
|
std::string heartbeatResponse;
|
||||||
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
||||||
@@ -559,7 +658,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
|
|
||||||
std::cout << "Heartbeat response: " << heartbeatResponse << std::endl;
|
std::cout << "Heartbeat response: " << heartbeatResponse << std::endl;
|
||||||
|
|
||||||
// Start session
|
|
||||||
std::string startResponse;
|
std::string startResponse;
|
||||||
std::string startJSON = BuildStartJSON(username, targetPositions);
|
std::string startJSON = BuildStartJSON(username, targetPositions);
|
||||||
|
|
||||||
@@ -581,11 +679,9 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
|
|
||||||
std::cout << "Session ID: " << sessionId << std::endl;
|
std::cout << "Session ID: " << sessionId << std::endl;
|
||||||
|
|
||||||
// Start heartbeat thread
|
|
||||||
std::atomic<bool> heartbeatStop(false);
|
std::atomic<bool> heartbeatStop(false);
|
||||||
std::thread heartbeat(HeartbeatThread, username, threads, std::ref(heartbeatStop), SERVER_URL);
|
std::thread heartbeat(HeartbeatThread, username, threads, std::ref(heartbeatStop), SERVER_URL);
|
||||||
|
|
||||||
// Run datagen
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
system("cls");
|
system("cls");
|
||||||
HideCursor();
|
HideCursor();
|
||||||
@@ -598,10 +694,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<HANDLE> workerThreads;
|
||||||
|
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||||
|
|
||||||
|
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull; // change to 16/32 MiB if needed
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
std::vector<pthread_t> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
@@ -638,18 +737,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
for (std::thread& thread : workerThreads) {
|
JoinWorkerThreads(workerThreads);
|
||||||
if (thread.joinable()) {
|
|
||||||
thread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
for (pthread_t& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
pthread_join(thread, nullptr);
|
pthread_join(thread, nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Stop heartbeat
|
|
||||||
heartbeatStop = true;
|
heartbeatStop = true;
|
||||||
if (heartbeat.joinable()) {
|
if (heartbeat.joinable()) {
|
||||||
heartbeat.join();
|
heartbeat.join();
|
||||||
@@ -673,7 +767,6 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
|||||||
if (UploadFile(uploadUrl, mergedFile, username, positions.load())) {
|
if (UploadFile(uploadUrl, mergedFile, username, positions.load())) {
|
||||||
std::cout << "Upload successful!" << std::endl;
|
std::cout << "Upload successful!" << std::endl;
|
||||||
|
|
||||||
// Delete the file after successful upload
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
std::filesystem::remove(mergedFile, ec);
|
std::filesystem::remove(mergedFile, ec);
|
||||||
if (!ec) {
|
if (!ec) {
|
||||||
@@ -718,11 +811,10 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
int width = 60;
|
int width = 60;
|
||||||
|
|
||||||
std::string title = "Eleanor - Datagen";
|
std::string title = "Eleanor - Datagen";
|
||||||
int titlePadding = (width - title.length()) / 2;
|
int titlePadding = (width - static_cast<int>(title.length())) / 2;
|
||||||
std::cout << COLOR_TITLE << std::setw(titlePadding + title.length()) << title << COLOR_RESET << std::endl;
|
std::cout << COLOR_TITLE << std::setw(titlePadding + static_cast<int>(title.length())) << title << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::string(width, '=') << std::endl;
|
std::cout << std::string(width, '=') << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << std::fixed << std::setprecision(0);
|
std::cout << std::fixed << std::setprecision(0);
|
||||||
@@ -731,31 +823,31 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
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 - static_cast<int>(datagenText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(datagenPadding + datagenText.length()) << datagenText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(datagenPadding + static_cast<int>(datagenText.length())) << datagenText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast<int>(round(elapsed))) + " sec";
|
std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast<int>(round(elapsed))) + " sec";
|
||||||
int elapsedPadding = (width - elapsedText.length()) / 2;
|
int elapsedPadding = (width - static_cast<int>(elapsedText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(elapsedPadding + elapsedText.length()) << elapsedText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(elapsedPadding + static_cast<int>(elapsedText.length())) << elapsedText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast<int>(round(positionsPerSec)));
|
std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast<int>(round(positionsPerSec)));
|
||||||
int positionsPerSecPadding = (width - positionsPerSecText.length()) / 2;
|
int positionsPerSecPadding = (width - static_cast<int>(positionsPerSecText.length())) / 2;
|
||||||
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + positionsPerSecText.length()) << positionsPerSecText << COLOR_RESET << std::endl;
|
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + static_cast<int>(positionsPerSecText.length())) << positionsPerSecText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::string threadsText = "Threads: " + std::to_string(static_cast<int>(threads));
|
std::string threadsText = "Threads: " + std::to_string(static_cast<int>(threads));
|
||||||
int threadsTextPadding = (width - threadsText.length()) / 2;
|
int threadsTextPadding = (width - static_cast<int>(threadsText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + threadsText.length()) << threadsText << COLOR_RESET << std::endl;
|
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + static_cast<int>(threadsText.length())) << threadsText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
double remainingTime = 0.0;
|
double remainingTime = 0.0;
|
||||||
if (positions > 0 && elapsed > 0.0) {
|
if (positions > 0 && elapsed > 0.0) {
|
||||||
double positionsPerSec = positions / elapsed;
|
double pps = positions / elapsed;
|
||||||
remainingTime = (targetPositions - positions) / positionsPerSec;
|
remainingTime = (targetPositions - positions) / pps;
|
||||||
}
|
}
|
||||||
|
|
||||||
remainingTime = (std::max)(0.0, remainingTime);
|
remainingTime = (std::max)(0.0, remainingTime);
|
||||||
@@ -771,8 +863,9 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
<< std::setw(2) << remainingSeconds << "s";
|
<< std::setw(2) << remainingSeconds << "s";
|
||||||
|
|
||||||
std::string remainingTimeText = remainingTimeStream.str();
|
std::string remainingTimeText = remainingTimeStream.str();
|
||||||
int remainingPadding = (width - remainingTimeText.length()) / 2;
|
int remainingPadding = (width - static_cast<int>(remainingTimeText.length())) / 2;
|
||||||
std::cout << COLOR_SECTION << std::setw(remainingPadding + remainingTimeText.length())
|
std::cout << COLOR_SECTION
|
||||||
|
<< std::setw(remainingPadding + static_cast<int>(remainingTimeText.length()))
|
||||||
<< remainingTimeText << COLOR_RESET << std::endl;
|
<< remainingTimeText << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -784,21 +877,18 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
std::string progressBar = "[";
|
std::string progressBar = "[";
|
||||||
|
|
||||||
for (int i = 0; i < barWidth; ++i) {
|
for (int i = 0; i < barWidth; ++i) {
|
||||||
if (i < pos) {
|
if (i < pos) progressBar += "=";
|
||||||
progressBar += "=";
|
else if (i == pos) progressBar += ">";
|
||||||
} else if (i == pos) {
|
else progressBar += " ";
|
||||||
progressBar += ">";
|
|
||||||
} else {
|
|
||||||
progressBar += " ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
progressBar += "] " + std::to_string(static_cast<int>(round(progress * 100.0))) + " %";
|
progressBar += "] " + std::to_string(static_cast<int>(round(progress * 100.0))) + " %";
|
||||||
|
|
||||||
int progressPadding = (width - progressBar.length()) / 2;
|
int progressPadding = (width - static_cast<int>(progressBar.length())) / 2;
|
||||||
std::cout << COLOR_BAR << std::setw(progressPadding + progressBar.length()) << progressBar << COLOR_RESET << std::endl;
|
std::cout << COLOR_BAR << std::setw(progressPadding + static_cast<int>(progressBar.length()))
|
||||||
|
<< progressBar << COLOR_RESET << std::endl;
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace DATAGEN
|
||||||
Reference in new issue
Block a user