Skip to content

Commit

Permalink
Restore NPS output for Perft
Browse files Browse the repository at this point in the history
Previously it was possible to also get the node counter after running a bench with perft, i.e.
`./stockfish bench 1 1 5 current perft`, caused by a small regression from the uci refactoring.

```
Nodes searched: 4865609

===========================
Total time (ms) : 18
Nodes searched  : 4865609
Nodes/second    : 270311611
````

closes official-stockfish#5188

No functional change
  • Loading branch information
Disservin committed Apr 24, 2024
1 parent d47aa63 commit ddd250b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const std::vector<std::string> Defaults = {

} // namespace

namespace Stockfish {
namespace Stockfish::Benchmark {

// Builds a list of UCI commands to be run by bench. There
// are five parameters: TT size in MB, number of search threads that
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <string>
#include <vector>

namespace Stockfish {
namespace Stockfish::Benchmark {

std::vector<std::string> setup_bench(const std::string&, std::istream&);

Expand Down
14 changes: 8 additions & 6 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>
#include <sstream>
#include <iosfwd>
#include <cassert>

#include "evaluate.h"
#include "misc.h"
Expand Down Expand Up @@ -54,14 +55,15 @@ Engine::Engine(std::string path) :
pos.set(StartFEN, false, &states->back());
}

void Engine::go(const Search::LimitsType& limits) {
std::uint64_t Engine::perft(const std::string& fen, Depth depth, bool isChess960) {
verify_networks();

if (limits.perft)
{
perft(pos.fen(), limits.perft, options["UCI_Chess960"]);
return;
}
return Benchmark::perft(fen, depth, isChess960);
}

void Engine::go(const Search::LimitsType& limits) {
assert(limits.perft == 0);
verify_networks();

threads.start_thinking(options, pos, states, limits);
}
Expand Down
4 changes: 4 additions & 0 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
#include <string_view>
#include <utility>
#include <vector>
#include <cstdint>

#include "nnue/network.h"
#include "position.h"
#include "search.h"
#include "thread.h"
#include "tt.h"
#include "ucioption.h"
#include "syzygy/tbprobe.h" // for Stockfish::Depth

namespace Stockfish {

Expand All @@ -45,6 +47,8 @@ class Engine {
Engine(std::string path = "");
~Engine() { wait_for_search_finished(); }

std::uint64_t perft(const std::string& fen, Depth depth, bool isChess960);

// non blocking call to start searching
void go(const Search::LimitsType&);
// non blocking call to stop searching
Expand Down
7 changes: 3 additions & 4 deletions src/perft.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "types.h"
#include "uci.h"

namespace Stockfish {
namespace Stockfish::Benchmark {

// Utility to verify move generation. All the leaf nodes up
// to the given depth are generated and counted, and the sum is returned.
Expand Down Expand Up @@ -56,13 +56,12 @@ uint64_t perft(Position& pos, Depth depth) {
return nodes;
}

inline void perft(const std::string& fen, Depth depth, bool isChess960) {
inline uint64_t perft(const std::string& fen, Depth depth, bool isChess960) {
StateListPtr states(new std::deque<StateInfo>(1));
Position p;
p.set(fen, isChess960, &states->back());

uint64_t nodes = perft<true>(p, depth);
sync_cout << "\nNodes searched: " << nodes << "\n" << sync_endl;
return perft<true>(p, depth);
}
}

Expand Down
26 changes: 22 additions & 4 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ Search::LimitsType UCIEngine::parse_limits(std::istream& is) {
void UCIEngine::go(std::istringstream& is) {

Search::LimitsType limits = parse_limits(is);
engine.go(limits);

if (limits.perft)
perft(limits);
else
engine.go(limits);
}

void UCIEngine::bench(std::istream& args) {
Expand All @@ -233,7 +237,7 @@ void UCIEngine::bench(std::istream& args) {
on_update_full(i, options["UCI_ShowWDL"]);
});

std::vector<std::string> list = setup_bench(engine.fen(), args);
std::vector<std::string> list = Benchmark::setup_bench(engine.fen(), args);

num = count_if(list.begin(), list.end(),
[](const std::string& s) { return s.find("go ") == 0 || s.find("eval") == 0; });
Expand All @@ -251,8 +255,16 @@ void UCIEngine::bench(std::istream& args) {
<< std::endl;
if (token == "go")
{
go(is);
engine.wait_for_search_finished();
Search::LimitsType limits = parse_limits(is);

if (limits.perft)
nodes = perft(limits);
else
{
engine.go(limits);
engine.wait_for_search_finished();
}

nodes += nodesSearched;
nodesSearched = 0;
}
Expand Down Expand Up @@ -288,6 +300,12 @@ void UCIEngine::setoption(std::istringstream& is) {
engine.get_options().setoption(is);
}

std::uint64_t UCIEngine::perft(const Search::LimitsType& limits) {
auto nodes = engine.perft(engine.fen(), limits.perft, engine.get_options()["UCI_Chess960"]);
sync_cout << "\nNodes searched: " << nodes << "\n" << sync_endl;
return nodes;
}

void UCIEngine::position(std::istringstream& is) {
std::string token, fen;

Expand Down
10 changes: 6 additions & 4 deletions src/uci.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <iostream>
#include <string>
#include <string_view>
#include <cstdint>

#include "engine.h"
#include "misc.h"
Expand Down Expand Up @@ -57,10 +58,11 @@ class UCIEngine {
Engine engine;
CommandLine cli;

void go(std::istringstream& is);
void bench(std::istream& args);
void position(std::istringstream& is);
void setoption(std::istringstream& is);
void go(std::istringstream& is);
void bench(std::istream& args);
void position(std::istringstream& is);
void setoption(std::istringstream& is);
std::uint64_t perft(const Search::LimitsType&);

static void on_update_no_moves(const Engine::InfoShort& info);
static void on_update_full(const Engine::InfoFull& info, bool showWDL);
Expand Down

0 comments on commit ddd250b

Please sign in to comment.