Merge branch 'main' into opti-2-compacting-move
This commit is contained in:
commit
56ea18758c
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "board.hpp"
|
#include "board.hpp"
|
||||||
#include "move.hpp"
|
#include "move.hpp"
|
||||||
|
#include "threadpool.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -102,7 +103,9 @@ static std::map<std::string, std::map<int, int>> pos2expected{
|
|||||||
|
|
||||||
static std::stringstream res;
|
static std::stringstream res;
|
||||||
|
|
||||||
int move_generation_test(Board& b, int depth, int max_depth) {
|
int move_generation_test(
|
||||||
|
const Board& b, int depth, int max_depth, ThreadPool& pool
|
||||||
|
) {
|
||||||
if (depth == max_depth) {
|
if (depth == max_depth) {
|
||||||
res.str("");
|
res.str("");
|
||||||
res.clear();
|
res.clear();
|
||||||
@ -118,15 +121,34 @@ int move_generation_test(Board& b, int depth, int max_depth) {
|
|||||||
return moves.size();
|
return moves.size();
|
||||||
|
|
||||||
int num_pos = 0;
|
int num_pos = 0;
|
||||||
|
|
||||||
|
if (depth == max_depth) {
|
||||||
|
// Parallel execution at the top level
|
||||||
|
std::vector<std::future<int>> futures;
|
||||||
for (const Move& move : moves) {
|
for (const Move& move : moves) {
|
||||||
Board tmp_board = b.make_move(move);
|
Board tmp_board = b.make_move(move);
|
||||||
// std::cout << ">" << move << std::endl;
|
futures.push_back(pool.enqueue(
|
||||||
int n = move_generation_test(tmp_board, depth - 1, max_depth);
|
move_generation_test,
|
||||||
// std::cout << "<" << move << std::endl;
|
tmp_board,
|
||||||
|
depth - 1,
|
||||||
|
max_depth,
|
||||||
|
std::ref(pool)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& future : futures)
|
||||||
|
num_pos += future.get(); // Retrieve the result of each task
|
||||||
|
} else {
|
||||||
|
// Regular sequential execution
|
||||||
|
for (const Move& move : moves) {
|
||||||
|
Board tmp_board = b.make_move(move);
|
||||||
|
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
|
||||||
if (depth == max_depth)
|
if (depth == max_depth)
|
||||||
res << move << ": " << n << std::endl;
|
res << move << ": " << n << std::endl;
|
||||||
num_pos += n;
|
num_pos += n;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return num_pos;
|
return num_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,11 +161,12 @@ void perft(std::string pos) {
|
|||||||
std::cout << pos << std::endl;
|
std::cout << pos << std::endl;
|
||||||
std::map<int, int> expected = pos2expected[pos];
|
std::map<int, int> expected = pos2expected[pos];
|
||||||
Board b = Board::setup_fen_position(pos);
|
Board b = Board::setup_fen_position(pos);
|
||||||
|
ThreadPool pool(std::thread::hardware_concurrency());
|
||||||
|
|
||||||
for (const auto& [depth, expected_n_moves] : expected) {
|
for (const auto& [depth, expected_n_moves] : expected) {
|
||||||
std::cout << "Depth: " << depth << " " << std::flush;
|
std::cout << "Depth: " << depth << " " << std::flush;
|
||||||
auto start = std::chrono::steady_clock::now();
|
auto start = std::chrono::steady_clock::now();
|
||||||
int moves = move_generation_test(b, depth, depth);
|
int moves = move_generation_test(b, depth, depth, pool);
|
||||||
auto end = std::chrono::steady_clock::now();
|
auto end = std::chrono::steady_clock::now();
|
||||||
auto elapsed =
|
auto elapsed =
|
||||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
||||||
|
73
cpp/src/threadpool.hpp
Normal file
73
cpp/src/threadpool.hpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <functional>
|
||||||
|
#include <future>
|
||||||
|
#include <iostream>
|
||||||
|
#include <queue>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class ThreadPool {
|
||||||
|
public:
|
||||||
|
ThreadPool(size_t numThreads) {
|
||||||
|
for (size_t i = 0; i < numThreads; ++i) {
|
||||||
|
workers.emplace_back([this] {
|
||||||
|
while (true) {
|
||||||
|
std::function<void()> task;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
condition.wait(lock, [this] {
|
||||||
|
return stop || !tasks.empty();
|
||||||
|
});
|
||||||
|
if (stop && tasks.empty())
|
||||||
|
return;
|
||||||
|
task = std::move(tasks.front());
|
||||||
|
tasks.pop();
|
||||||
|
}
|
||||||
|
task();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class F, class... Args>
|
||||||
|
auto enqueue(F&& f, Args&&... args)
|
||||||
|
-> std::future<typename std::invoke_result<F, Args...>::type> {
|
||||||
|
using return_type = typename std::invoke_result<F, Args...>::type;
|
||||||
|
|
||||||
|
auto task = std::make_shared<std::packaged_task<return_type()>>(
|
||||||
|
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
|
||||||
|
);
|
||||||
|
|
||||||
|
std::future<return_type> res = task->get_future();
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
tasks.emplace([task]() { (*task)(); });
|
||||||
|
}
|
||||||
|
condition.notify_one();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void waitAll() {
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
condition.wait(lock, [this] { return tasks.empty(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
~ThreadPool() {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queueMutex);
|
||||||
|
stop = true;
|
||||||
|
}
|
||||||
|
condition.notify_all();
|
||||||
|
for (std::thread& worker : workers)
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::thread> workers;
|
||||||
|
std::queue<std::function<void()>> tasks;
|
||||||
|
std::mutex queueMutex;
|
||||||
|
std::condition_variable condition;
|
||||||
|
bool stop = false;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user