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.
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;
109 caller_was_worker_ = in_worker();
110 async_ = threads > 1 && count > 0 && !caller_was_worker_;
111
112
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 }