Skip to content

Commit 6acbdc9

Browse files
committed
add vtr::thread_pool
1 parent 24295a2 commit 6acbdc9

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

vpr/src/route/RouterThreadPool.h renamed to libs/libvtrutil/src/vtr_thread_pool.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
/**
44
* @file RouterThreadPool.h
5-
* @brief A generic thread pool for parallel task execution.
6-
*
7-
* This class manages a pool of threads and allows tasks to be executed in parallel.
5+
* @brief A generic thread pool for parallel task execution
86
*/
97

108
#include <thread>
@@ -19,7 +17,9 @@
1917
#include "vtr_log.h"
2018
#include "vtr_time.h"
2119

22-
class RouterThreadPool {
20+
namespace vtr {
21+
22+
class thread_pool {
2323
private:
2424
struct ThreadData {
2525
std::thread thread;
@@ -42,16 +42,16 @@ class RouterThreadPool {
4242
std::condition_variable completion_cv;
4343

4444
public:
45-
RouterThreadPool(size_t thread_count) {
45+
thread_pool(size_t thread_count) {
4646
// VTR_LOG("Creating thread pool with %zu threads\n", thread_count);
4747
threads.reserve(thread_count);
48-
48+
4949
for (size_t i = 0; i < thread_count; i++) {
5050
auto thread_data = std::make_unique<ThreadData>(i);
51-
52-
thread_data->thread = std::thread([this, td = thread_data.get()]() {
51+
52+
thread_data->thread = std::thread([td = thread_data.get()]() {
5353
// VTR_LOG("Thread %zu started\n", td->thread_id);
54-
54+
5555
while (true) {
5656
std::function<void()> task;
5757
{
@@ -93,14 +93,14 @@ class RouterThreadPool {
9393
template<typename F>
9494
void schedule_work(F&& f) {
9595
active_tasks++;
96-
96+
9797
// Round-robin thread assignment
9898
size_t thread_idx = (next_thread++) % threads.size();
9999
auto thread_data = threads[thread_idx].get();
100100
size_t task_id = ++total_tasks_queued;
101-
101+
102102
// VTR_LOG("Scheduling task %zu to thread %zu\n", task_id, thread_data->thread_id);
103-
103+
104104
// Wrap the work with task completion tracking
105105
auto task = [this, f = std::forward<F>(f), thread_id = thread_data->thread_id, task_id]() {
106106
vtr::Timer task_timer;
@@ -126,7 +126,7 @@ class RouterThreadPool {
126126
completion_cv.notify_all();
127127
}
128128
};
129-
129+
130130
// Queue the task
131131
{
132132
std::lock_guard<std::mutex> lock(thread_data->queue_mutex);
@@ -142,7 +142,7 @@ class RouterThreadPool {
142142
completion_cv.wait(lock, [this]() { return active_tasks == 0; });
143143
}
144144

145-
~RouterThreadPool() {
145+
~thread_pool() {
146146
// VTR_LOG("Shutting down thread pool after %.3f seconds, processed %zu total tasks\n",
147147
// pool_timer.elapsed_sec(), total_tasks_queued.load());
148148

@@ -156,7 +156,7 @@ class RouterThreadPool {
156156
}
157157
thread_data->cv.notify_one();
158158
}
159-
159+
160160
// Join all threads
161161
for (auto& thread_data : threads) {
162162
if (thread_data->thread.joinable()) {
@@ -167,3 +167,5 @@ class RouterThreadPool {
167167
}
168168
}
169169
};
170+
171+
} // namespace vtr

vpr/src/route/ParallelNetlistRouter.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
* [0]: "Parallel FPGA Routing with On-the-Fly Net Decomposition", FPT'24 */
1212
#include "netlist_routers.h"
1313
#include "vtr_optional.h"
14-
#include "RouterThreadPool.h"
14+
#include "vtr_thread_pool.h"
1515

1616
#include <tbb/task_group.h>
17-
#include <future>
1817

1918
/** Parallel impl for NetlistRouter.
2019
* Holds enough context members to glue together ConnectionRouter and net routing functions,
@@ -105,7 +104,7 @@ class ParallelNetlistRouter : public NetlistRouter {
105104
/** The partition tree. Holds the groups of nets for each partition */
106105
vtr::optional<PartitionTree> _tree;
107106

108-
RouterThreadPool _thread_pool;
107+
vtr::thread_pool _thread_pool;
109108
};
110109

111110
#include "ParallelNetlistRouter.tpp"

vpr/src/route/ParallelNetlistRouter.tpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "netlist_routers.h"
77
#include "route_net.h"
88
#include "vtr_time.h"
9-
#include "RouterThreadPool.h"
109

1110
template<typename HeapType>
1211
inline RouteIterResults ParallelNetlistRouter<HeapType>::route_netlist(int itry, float pres_fac, float worst_neg_slack) {
@@ -33,7 +32,7 @@ inline RouteIterResults ParallelNetlistRouter<HeapType>::route_netlist(int itry,
3332
_thread_pool.schedule_work([this]() {
3433
route_partition_tree_node(_tree->root());
3534
});
36-
35+
3736
/* Wait for all tasks in the thread pool to complete */
3837
_thread_pool.wait_for_all();
3938

0 commit comments

Comments
 (0)