Loading...
Searching...
No Matches
gpr::linalg::TilePool Class Reference

#include <gpr/linalg/TileParallel.h>

Public Member Functions

template<class F>
void run (long count, F &&fn)
template<class F>
void runWith (int threads, long count, F &&fn)
 run() with an explicit thread count in place of the configured one.
template<class F>
void start (long count, F &&fn)
void finish ()
 Joins the items of start(); see there.
 ~TilePool ()
 TilePool (const TilePool &)=delete
TilePool & operator= (const TilePool &)=delete

Static Public Member Functions

static TilePool & instance ()

Detailed Description

Runs fn(0) .. fn(count - 1) on the calling thread and a small pool of workers. Items are claimed from a shared counter, so the item-to-thread assignment varies between runs; the callers pass items that write disjoint output and run each item's arithmetic on one thread, which makes the result independent of the thread count. Called from a worker, or with one thread configured, the items run in order on the calling thread.

Definition at line 41 of file TileParallel.h.

Constructor & Destructor Documentation

◆ ~TilePool()

gpr::linalg::TilePool::~TilePool ( )
inline

Definition at line 162 of file TileParallel.h.

163 {
164 {
165 std::lock_guard<std::mutex> lock(mutex_);
166 stop_ = true;
167 ++generation_;
168 }
169 wake_.notify_all();
170 for (std::thread& worker: workers_)
171 worker.join();
172 }

◆ TilePool()

gpr::linalg::TilePool::TilePool ( const TilePool & )
delete

Member Function Documentation

◆ finish()

void gpr::linalg::TilePool::finish ( )
inline

Joins the items of start(); see there.

Definition at line 133 of file TileParallel.h.

134 {
135 if (!started_) return;
136 started_ = false;
137 if (!async_) {
138 // Restores the caller's state when an item throws, too.
139 struct Restore {
140 bool value;
141 ~Restore()
142 {
143 in_worker() = value;
144 }
145 } restore{caller_was_worker_};
146 for (long i = 0; i < pending_count_; ++i)
147 pending_(i);
148 pending_ = nullptr;
149 return;
150 }
151 drain();
152 in_worker() = caller_was_worker_;
153 std::unique_lock<std::mutex> lock(mutex_);
154 idle_.wait(lock, [&] { return busy_ == 0; });
155 job_ = nullptr;
156 lock.unlock();
157 pending_ = nullptr;
158 launch_lock_.unlock();
159 if (error_) std::rethrow_exception(error_);
160 }

◆ instance()

TilePool & gpr::linalg::TilePool::instance ( )
inlinestatic

Definition at line 43 of file TileParallel.h.

44 {
45 static TilePool pool;
46 return pool;
47 }
TilePool(const TilePool &)=delete

◆ operator=()

TilePool & gpr::linalg::TilePool::operator= ( const TilePool & )
delete

◆ run()

template<class F>
void gpr::linalg::TilePool::run ( long count,
F && fn )
inline

Definition at line 50 of file TileParallel.h.

51 {
52 runWith(tileThreadCount().load(), count, std::forward<F>(fn));
53 }
void runWith(int threads, long count, F &&fn)
run() with an explicit thread count in place of the configured one.
std::atomic< int > & tileThreadCount() noexcept
Number of threads one rank devotes to independent tile products.

◆ runWith()

template<class F>
void gpr::linalg::TilePool::runWith ( int threads,
long count,
F && fn )
inline

run() with an explicit thread count in place of the configured one.

Definition at line 57 of file TileParallel.h.

58 {
59 if (count <= 0) return;
60 if (threads <= 1 || count == 1 || in_worker()) {
61 for (long i = 0; i < count; ++i)
62 fn(i);
63 return;
64 }
65 std::unique_lock<std::mutex> launch(launch_mutex_);
66 ensureWorkers(threads - 1);
67 const std::function<void(long)> job = [&fn](long i) { fn(i); };
68 {
69 std::lock_guard<std::mutex> lock(mutex_);
70 job_ = &job;
71 count_ = count;
72 next_.store(0);
73 error_ = nullptr;
74 // Only as many workers as there are items beyond the caller's own.
75 wanted_ = static_cast<int>(
76 std::min<long>({count - 1, static_cast<long>(workers_.size()),
77 static_cast<long>(threads - 1)}));
78 busy_ = wanted_;
79 ++generation_;
80 }
81 wake_.notify_all();
82 // Loops nested in an item run serially on the thread that owns it.
83 in_worker() = true;
84 drain();
85 in_worker() = false;
86 std::unique_lock<std::mutex> lock(mutex_);
87 idle_.wait(lock, [&] { return busy_ == 0; });
88 job_ = nullptr;
89 if (error_) std::rethrow_exception(error_);
90 }

◆ start()

template<class F>
void gpr::linalg::TilePool::start ( long count,
F && fn )
inline

Starts fn(0) .. fn(count - 1) on the workers and returns at once; the calling thread joins with finish(), which runs the items no worker has claimed and waits for the rest. Between the two calls, loops started by the calling thread run serially on it, which leaves it free for communication and for work the items do not touch. fn must stay valid until finish() returns. With one thread configured, or from inside a worker, every item runs in finish().

Definition at line 102 of file TileParallel.h.

103 {
104 if (started_) throw std::logic_error("TilePool::start while started");
105 started_ = true;
106 pending_ = std::function<void(long)>(std::forward<F>(fn));
107 pending_count_ = count;
108 const int threads = tileThreadCount().load();
109 caller_was_worker_ = in_worker();
110 async_ = threads > 1 && count > 0 && !caller_was_worker_;
111 // Loops of the caller run serially on it until finish(), with or
112 // without workers.
113 in_worker() = true;
114 if (!async_) return;
115 launch_lock_ = std::unique_lock<std::mutex>(launch_mutex_);
116 ensureWorkers(threads - 1);
117 {
118 std::lock_guard<std::mutex> lock(mutex_);
119 job_ = &pending_;
120 count_ = count;
121 next_.store(0);
122 error_ = nullptr;
123 wanted_ = static_cast<int>(
124 std::min<long>({count, static_cast<long>(workers_.size()),
125 static_cast<long>(threads - 1)}));
126 busy_ = wanted_;
127 ++generation_;
128 }
129 wake_.notify_all();
130 }

The documentation for this class was generated from the following file: