mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 16:47:37 +00:00
Allow execution of tasks on the global thread pool.
This commit is contained in:
+25
-2
@@ -80,6 +80,13 @@ void Thread::start_searching() {
|
|||||||
cv.notify_one(); // Wake up the thread in idle_loop()
|
cv.notify_one(); // Wake up the thread in idle_loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Thread::execute_task(std::function<void(Thread&)> t)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lk(mutex);
|
||||||
|
task = std::move(t);
|
||||||
|
cv.notify_one(); // Wake up the thread in idle_loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Thread::wait_for_search_finished() blocks on the condition variable
|
/// Thread::wait_for_search_finished() blocks on the condition variable
|
||||||
/// until the thread has finished searching.
|
/// until the thread has finished searching.
|
||||||
@@ -109,14 +116,22 @@ void Thread::idle_loop() {
|
|||||||
std::unique_lock<std::mutex> lk(mutex);
|
std::unique_lock<std::mutex> lk(mutex);
|
||||||
searching = false;
|
searching = false;
|
||||||
cv.notify_one(); // Wake up anyone waiting for search finished
|
cv.notify_one(); // Wake up anyone waiting for search finished
|
||||||
cv.wait(lk, [&]{ return searching; });
|
cv.wait(lk, [&]{ return searching || task; });
|
||||||
|
|
||||||
if (exit)
|
if (exit)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lk.unlock();
|
lk.unlock();
|
||||||
|
|
||||||
search();
|
if (task)
|
||||||
|
{
|
||||||
|
task(*this);
|
||||||
|
task = nullptr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
search();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +177,14 @@ void ThreadPool::clear() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ThreadPool::execute_parallel(std::function<void(Thread&)> task)
|
||||||
|
{
|
||||||
|
for(Thread* th : *this)
|
||||||
|
{
|
||||||
|
th->execute_task(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
||||||
/// returns immediately. Main thread will wake up other threads and start the search.
|
/// returns immediately. Main thread will wake up other threads and start the search.
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "material.h"
|
#include "material.h"
|
||||||
#include "movepick.h"
|
#include "movepick.h"
|
||||||
@@ -50,10 +51,12 @@ public:
|
|||||||
explicit Thread(size_t);
|
explicit Thread(size_t);
|
||||||
virtual ~Thread();
|
virtual ~Thread();
|
||||||
virtual void search();
|
virtual void search();
|
||||||
|
virtual void execute_task(std::function<void(Thread&)> t);
|
||||||
void clear();
|
void clear();
|
||||||
void idle_loop();
|
void idle_loop();
|
||||||
void start_searching();
|
void start_searching();
|
||||||
void wait_for_search_finished();
|
void wait_for_search_finished();
|
||||||
|
size_t thread_idx() const { return idx; }
|
||||||
|
|
||||||
Pawns::Table pawnsTable;
|
Pawns::Table pawnsTable;
|
||||||
Material::Table materialTable;
|
Material::Table materialTable;
|
||||||
@@ -78,6 +81,7 @@ public:
|
|||||||
bool UseRule50;
|
bool UseRule50;
|
||||||
Depth ProbeDepth;
|
Depth ProbeDepth;
|
||||||
|
|
||||||
|
std::function<void(Thread&)> task;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -105,6 +109,8 @@ struct MainThread : public Thread {
|
|||||||
|
|
||||||
struct ThreadPool : public std::vector<Thread*> {
|
struct ThreadPool : public std::vector<Thread*> {
|
||||||
|
|
||||||
|
void execute_parallel(std::function<void(Thread&)> task);
|
||||||
|
|
||||||
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
||||||
void clear();
|
void clear();
|
||||||
void set(size_t);
|
void set(size_t);
|
||||||
|
|||||||
@@ -345,6 +345,12 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
// Command to call qsearch(),search() directly for testing
|
// Command to call qsearch(),search() directly for testing
|
||||||
else if (token == "qsearch") qsearch_cmd(pos);
|
else if (token == "qsearch") qsearch_cmd(pos);
|
||||||
else if (token == "search") search_cmd(pos, is);
|
else if (token == "search") search_cmd(pos, is);
|
||||||
|
else if (token == "tasktest")
|
||||||
|
{
|
||||||
|
Threads.execute_parallel([](auto& th) {
|
||||||
|
std::cout << th.thread_idx() << '\n';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// test command
|
// test command
|
||||||
else if (token == "test") test_cmd(pos, is);
|
else if (token == "test") test_cmd(pos, is);
|
||||||
|
|||||||
Reference in New Issue
Block a user