Compare commits
7 Commits
c55645ad73
...
a47dc9b695
Author | SHA1 | Date | |
---|---|---|---|
|
a47dc9b695 | ||
|
e8ca1db8d6 | ||
|
73b2a5b51b | ||
|
3f3017a389 | ||
|
f768d16a7e | ||
|
a658673d2b | ||
|
222adbb8f0 |
@ -182,7 +182,9 @@ Board Board::make_move(Move move) const {
|
||||
ret.squares[move.target_square] = this->squares[move.source_square];
|
||||
|
||||
// -- Handle en passant target being eaten
|
||||
if (move.en_passant)
|
||||
if (en_passant_target != -1
|
||||
&& (squares[move.source_square] & 0b111) == Piece::Pawn
|
||||
&& squares[move.target_square] == Piece::None)
|
||||
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
|
||||
Piece::None;
|
||||
|
||||
@ -203,23 +205,26 @@ Board Board::make_move(Move move) const {
|
||||
|
||||
// -- Handle castling (just move the rook over)
|
||||
Coords c = Coords::from_index(move.source_square);
|
||||
if (move.castle_side & KingSide) {
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.castle_side & QueenSide) {
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
if ((squares[move.source_square] & 0b111) == Piece::King) {
|
||||
if (move.target_square - move.source_square == 2) { // king side castle
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.target_square - move.source_square == -2) { // queen
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
}
|
||||
}
|
||||
|
||||
// -- Check for castling rights
|
||||
ret.w_castle_rights = w_castle_rights;
|
||||
ret.b_castle_rights = b_castle_rights;
|
||||
bool is_capturing = squares[move.target_square] != Piece::None;
|
||||
if (white_to_play) {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.w_castle_rights = NeitherSide;
|
||||
@ -232,7 +237,7 @@ Board Board::make_move(Move move) const {
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 7
|
||||
if (is_capturing && target.y == 7
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
@ -251,7 +256,7 @@ Board Board::make_move(Move move) const {
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (move.is_capturing && target.y == 0
|
||||
if (is_capturing && target.y == 0
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string pos =
|
||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5";
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
// Board b = Board::setup_fen_position(pos);
|
||||
perft();
|
||||
return 0;
|
||||
|
@ -11,9 +11,6 @@ struct Move {
|
||||
int8_t source_square;
|
||||
int8_t target_square;
|
||||
|
||||
bool is_capturing = false;
|
||||
CastleSide castle_side = CastleSide::NeitherSide;
|
||||
bool en_passant = false;
|
||||
int8_t promoting_to = Piece::None;
|
||||
|
||||
std::string to_string() const {
|
||||
|
@ -60,7 +60,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{6, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::KingSide
|
||||
});
|
||||
}
|
||||
|
||||
@ -68,7 +67,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{2, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::QueenSide
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,6 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
left.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
@ -43,7 +42,6 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
right.to_index(),
|
||||
.is_capturing = true,
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
@ -55,14 +53,8 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
if (b.en_passant_target != -1) {
|
||||
Coords c = Coords::from_index(b.en_passant_target);
|
||||
int dy = my_colour == Colour::White ? 1 : -1;
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1)) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
c.to_index(),
|
||||
.is_capturing = true,
|
||||
.en_passant = true
|
||||
});
|
||||
}
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1))
|
||||
ret.push_back(Move{xy.to_index(), c.to_index()});
|
||||
}
|
||||
|
||||
// -- Normal move + promotion
|
||||
|
@ -53,18 +53,11 @@ legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
|
||||
|
||||
std::optional<Move>
|
||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
if (!dest.is_within_bounds())
|
||||
if (!dest.is_within_bounds()
|
||||
|| board.colour_at(source) == board.colour_at(dest))
|
||||
return {};
|
||||
|
||||
int8_t piece = board.squares[dest.to_index()];
|
||||
if (piece == Piece::None)
|
||||
return Move{source.to_index(), dest.to_index()};
|
||||
|
||||
int8_t source_colour = board.colour_at(source);
|
||||
int8_t dest_colour = board.colour_at(dest);
|
||||
if (source_colour != dest_colour)
|
||||
return Move{source.to_index(), dest.to_index(), true};
|
||||
return {};
|
||||
return Move{source.to_index(), dest.to_index()};
|
||||
}
|
||||
|
||||
std::vector<Move>
|
||||
@ -74,11 +67,11 @@ look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
int dx = mult_dx * d;
|
||||
int dy = mult_dy * d;
|
||||
|
||||
std::optional<Move> move =
|
||||
move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
|
||||
Coords target{xy.x + dx, xy.y + dy};
|
||||
std::optional<Move> move = move_for_position(board, xy, target);
|
||||
if (move.has_value()) {
|
||||
ret.push_back(move.value());
|
||||
if (move.value().is_capturing)
|
||||
if (board.squares[target.to_index()] != Piece::None)
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include "board.hpp"
|
||||
#include "move.hpp"
|
||||
#include "threadpool.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
@ -102,7 +103,9 @@ static std::map<std::string, std::map<int, int>> pos2expected{
|
||||
|
||||
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) {
|
||||
res.str("");
|
||||
res.clear();
|
||||
@ -118,15 +121,34 @@ int move_generation_test(Board& b, int depth, int max_depth) {
|
||||
return moves.size();
|
||||
|
||||
int num_pos = 0;
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
// std::cout << ">" << move << std::endl;
|
||||
int n = move_generation_test(tmp_board, depth - 1, max_depth);
|
||||
// std::cout << "<" << move << std::endl;
|
||||
if (depth == max_depth)
|
||||
res << move << ": " << n << std::endl;
|
||||
num_pos += n;
|
||||
|
||||
if (depth == max_depth) {
|
||||
// Parallel execution at the top level
|
||||
std::vector<std::future<int>> futures;
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
futures.push_back(pool.enqueue(
|
||||
move_generation_test,
|
||||
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)
|
||||
res << move << ": " << n << std::endl;
|
||||
num_pos += n;
|
||||
}
|
||||
}
|
||||
|
||||
return num_pos;
|
||||
}
|
||||
|
||||
@ -139,11 +161,12 @@ void perft(std::string pos) {
|
||||
std::cout << pos << std::endl;
|
||||
std::map<int, int> expected = pos2expected[pos];
|
||||
Board b = Board::setup_fen_position(pos);
|
||||
ThreadPool pool(std::thread::hardware_concurrency());
|
||||
|
||||
for (const auto& [depth, expected_n_moves] : expected) {
|
||||
std::cout << "Depth: " << depth << " " << std::flush;
|
||||
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 elapsed =
|
||||
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