fixed windows compatibility | bench 1743358

This commit is contained in:
rektdie committed 2026-03-04 23:41:42 +01:00
1 parent 345c6d21d9
commit ce3ed3b6d1
2 files changed
+243 -151

No files matched your search

+33 -31
View File
@@ -3,49 +3,51 @@ 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
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
UNAME_S := Windows UNAME_S := Windows
EXE_EXT := .exe EXE_EXT := .exe
MKDIR := mkdir MKDIR := mkdir
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)
EXE_EXT := EXE_EXT :=
MKDIR := mkdir -p MKDIR := mkdir -p
RM := rm -rf RM := rm -rf
DEL := rm -f DEL := rm -f
ifeq ($(UNAME_S),Darwin) CXX := clang++
PLATFORM := mac ifeq ($(UNAME_S),Darwin)
else PLATFORM := mac
PLATFORM := unix else
endif PLATFORM := unix
endif
endif endif
CXXFLAGS := -std=c++20 -O3 -flto -Wall CXXFLAGS := -std=c++20 -O3 -flto -Wall
LDFLAGS := -O3 -flto 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 LDFLAGS += -fuse-ld=lld -static
LDFLAGS += -fuse-ld=lld -static endif
endif
endif endif
CXXFLAGS += $(ARCH_FLAGS) CXXFLAGS += $(ARCH_FLAGS)
+210 -120
View File
@@ -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++;
@@ -195,9 +236,9 @@ static void SetupSignalHandlers() {
std::signal(SIGINT, SignalHandler); std::signal(SIGINT, SignalHandler);
std::signal(SIGTERM, SignalHandler); std::signal(SIGTERM, SignalHandler);
#ifndef _WIN32 #ifndef _WIN32
std::signal(SIGHUP, SignalHandler); std::signal(SIGHUP, SignalHandler);
#endif #endif
} }
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) { static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
@@ -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;
} }
@@ -279,12 +324,12 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, const std::string& baseP
tmpFile.write(buffer.data(), buffer.size()); tmpFile.write(buffer.data(), buffer.size());
tmpFile.flush(); tmpFile.flush();
#ifdef _WIN32 #ifdef _WIN32
tmpFile.close(); tmpFile.close();
#else #else
tmpFile.close(); tmpFile.close();
sync(); sync();
#endif #endif
std::error_code ec; std::error_code ec;
std::filesystem::rename(tmpFileName, finalFileName, ec); std::filesystem::rename(tmpFileName, finalFileName, ec);
@@ -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
// ============================================================================ // ============================================================================
@@ -459,22 +560,25 @@ static void* ThreadFunc(void* arg) {
void Run(int targetPositions, int threads) { void Run(int targetPositions, int threads) {
SetupSignalHandlers(); SetupSignalHandlers();
#ifdef _WIN32 #ifdef _WIN32
system("cls"); system("cls");
HideCursor(); HideCursor();
#else #else
std::cout << "\033[2J\033[H"; std::cout << "\033[2J\033[H";
HideCursor(); HideCursor();
#endif #endif
std::atomic<int> positions = 0; std::atomic<int> positions = 0;
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);
@@ -525,11 +625,11 @@ void Run(int targetPositions, int threads) {
std::cout << "All threads completed. Merging files..." << std::endl; std::cout << "All threads completed. Merging files..." << std::endl;
MergeThreadFiles(); MergeThreadFiles();
#ifdef _WIN32 #ifdef _WIN32
ShowCursor(); ShowCursor();
#else #else
ShowCursor(); ShowCursor();
#endif #endif
std::cout << "\nShutdown complete. All data saved safely." << std::endl; std::cout << "\nShutdown complete. All data saved safely." << std::endl;
} }
@@ -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,27 +679,28 @@ 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(); #else
#else std::cout << "\033[2J\033[H";
std::cout << "\033[2J\033[H"; HideCursor();
HideCursor(); #endif
#endif
std::atomic<int> positions = 0; std::atomic<int> positions = 0;
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();
@@ -660,11 +754,11 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
std::cout << "All threads completed. Merging files..." << std::endl; std::cout << "All threads completed. Merging files..." << std::endl;
std::string mergedFile = MergeThreadFiles(); std::string mergedFile = MergeThreadFiles();
#ifdef _WIN32 #ifdef _WIN32
ShowCursor(); ShowCursor();
#else #else
ShowCursor(); ShowCursor();
#endif #endif
if (!mergedFile.empty()) { if (!mergedFile.empty()) {
std::cout << "Uploading file to server..." << std::endl; std::cout << "Uploading file to server..." << std::endl;
@@ -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,44 +811,43 @@ 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);
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 - 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);
@@ -766,14 +858,15 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
std::ostringstream remainingTimeStream; std::ostringstream remainingTimeStream;
remainingTimeStream << "Estimated Time Left: " remainingTimeStream << "Estimated Time Left: "
<< std::setfill('0') << std::setw(2) << remainingHours << "h " << std::setfill('0') << std::setw(2) << remainingHours << "h "
<< std::setw(2) << remainingMinutes << "m " << std::setw(2) << remainingMinutes << "m "
<< 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
<< remainingTimeText << COLOR_RESET << std::endl; << std::setw(remainingPadding + static_cast<int>(remainingTimeText.length()))
<< 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