| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H |
| | #define EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H |
| |
|
| | namespace Eigen { |
| |
|
| | template <typename Environment> |
| | class ThreadPoolTempl : public Eigen::ThreadPoolInterface { |
| | public: |
| | typedef typename Environment::Task Task; |
| | typedef RunQueue<Task, 1024> Queue; |
| |
|
| | ThreadPoolTempl(int num_threads, Environment env = Environment()) |
| | : ThreadPoolTempl(num_threads, true, env) {} |
| |
|
| | ThreadPoolTempl(int num_threads, bool allow_spinning, |
| | Environment env = Environment()) |
| | : env_(env), |
| | num_threads_(num_threads), |
| | allow_spinning_(allow_spinning), |
| | thread_data_(num_threads), |
| | all_coprimes_(num_threads), |
| | waiters_(num_threads), |
| | global_steal_partition_(EncodePartition(0, num_threads_)), |
| | blocked_(0), |
| | spinning_(0), |
| | done_(false), |
| | cancelled_(false), |
| | ec_(waiters_) { |
| | waiters_.resize(num_threads_); |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | eigen_plain_assert(num_threads_ < kMaxThreads); |
| | for (int i = 1; i <= num_threads_; ++i) { |
| | all_coprimes_.emplace_back(i); |
| | ComputeCoprimes(i, &all_coprimes_.back()); |
| | } |
| | #ifndef EIGEN_THREAD_LOCAL |
| | init_barrier_.reset(new Barrier(num_threads_)); |
| | #endif |
| | thread_data_.resize(num_threads_); |
| | for (int i = 0; i < num_threads_; i++) { |
| | SetStealPartition(i, EncodePartition(0, num_threads_)); |
| | thread_data_[i].thread.reset( |
| | env_.CreateThread([this, i]() { WorkerLoop(i); })); |
| | } |
| | #ifndef EIGEN_THREAD_LOCAL |
| | |
| | |
| | init_barrier_->Wait(); |
| | #endif |
| | } |
| |
|
| | ~ThreadPoolTempl() { |
| | done_ = true; |
| |
|
| | |
| | |
| | |
| | if (!cancelled_) { |
| | ec_.Notify(true); |
| | } else { |
| | |
| | |
| | for (size_t i = 0; i < thread_data_.size(); i++) { |
| | thread_data_[i].queue.Flush(); |
| | } |
| | } |
| | |
| | |
| | for (size_t i = 0; i < thread_data_.size(); ++i) |
| | thread_data_[i].thread.reset(); |
| | } |
| |
|
| | void SetStealPartitions(const std::vector<std::pair<unsigned, unsigned>>& partitions) { |
| | eigen_plain_assert(partitions.size() == static_cast<std::size_t>(num_threads_)); |
| |
|
| | |
| | for (int i = 0; i < num_threads_; i++) { |
| | const auto& pair = partitions[i]; |
| | unsigned start = pair.first, end = pair.second; |
| | AssertBounds(start, end); |
| | unsigned val = EncodePartition(start, end); |
| | SetStealPartition(i, val); |
| | } |
| | } |
| |
|
| | void Schedule(std::function<void()> fn) EIGEN_OVERRIDE { |
| | ScheduleWithHint(std::move(fn), 0, num_threads_); |
| | } |
| |
|
| | void ScheduleWithHint(std::function<void()> fn, int start, |
| | int limit) override { |
| | Task t = env_.CreateTask(std::move(fn)); |
| | PerThread* pt = GetPerThread(); |
| | if (pt->pool == this) { |
| | |
| | Queue& q = thread_data_[pt->thread_id].queue; |
| | t = q.PushFront(std::move(t)); |
| | } else { |
| | |
| | |
| | eigen_plain_assert(start < limit); |
| | eigen_plain_assert(limit <= num_threads_); |
| | int num_queues = limit - start; |
| | int rnd = Rand(&pt->rand) % num_queues; |
| | eigen_plain_assert(start + rnd < limit); |
| | Queue& q = thread_data_[start + rnd].queue; |
| | t = q.PushBack(std::move(t)); |
| | } |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if (!t.f) { |
| | ec_.Notify(false); |
| | } else { |
| | env_.ExecuteTask(t); |
| | } |
| | } |
| |
|
| | void Cancel() EIGEN_OVERRIDE { |
| | cancelled_ = true; |
| | done_ = true; |
| |
|
| | |
| | #ifdef EIGEN_THREAD_ENV_SUPPORTS_CANCELLATION |
| | for (size_t i = 0; i < thread_data_.size(); i++) { |
| | thread_data_[i].thread->OnCancel(); |
| | } |
| | #endif |
| |
|
| | |
| | ec_.Notify(true); |
| | } |
| |
|
| | int NumThreads() const EIGEN_FINAL { return num_threads_; } |
| |
|
| | int CurrentThreadId() const EIGEN_FINAL { |
| | const PerThread* pt = const_cast<ThreadPoolTempl*>(this)->GetPerThread(); |
| | if (pt->pool == this) { |
| | return pt->thread_id; |
| | } else { |
| | return -1; |
| | } |
| | } |
| |
|
| | private: |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | static const int kMaxPartitionBits = 16; |
| | static const int kMaxThreads = 1 << kMaxPartitionBits; |
| |
|
| | inline unsigned EncodePartition(unsigned start, unsigned limit) { |
| | return (start << kMaxPartitionBits) | limit; |
| | } |
| |
|
| | inline void DecodePartition(unsigned val, unsigned* start, unsigned* limit) { |
| | *limit = val & (kMaxThreads - 1); |
| | val >>= kMaxPartitionBits; |
| | *start = val; |
| | } |
| |
|
| | void AssertBounds(int start, int end) { |
| | eigen_plain_assert(start >= 0); |
| | eigen_plain_assert(start < end); |
| | eigen_plain_assert(end <= num_threads_); |
| | } |
| |
|
| | inline void SetStealPartition(size_t i, unsigned val) { |
| | thread_data_[i].steal_partition.store(val, std::memory_order_relaxed); |
| | } |
| |
|
| | inline unsigned GetStealPartition(int i) { |
| | return thread_data_[i].steal_partition.load(std::memory_order_relaxed); |
| | } |
| |
|
| | void ComputeCoprimes(int N, MaxSizeVector<unsigned>* coprimes) { |
| | for (int i = 1; i <= N; i++) { |
| | unsigned a = i; |
| | unsigned b = N; |
| | |
| | while (b != 0) { |
| | unsigned tmp = a; |
| | a = b; |
| | b = tmp % b; |
| | } |
| | if (a == 1) { |
| | coprimes->push_back(i); |
| | } |
| | } |
| | } |
| |
|
| | typedef typename Environment::EnvThread Thread; |
| |
|
| | struct PerThread { |
| | constexpr PerThread() : pool(NULL), rand(0), thread_id(-1) {} |
| | ThreadPoolTempl* pool; |
| | uint64_t rand; |
| | int thread_id; |
| | #ifndef EIGEN_THREAD_LOCAL |
| | |
| | char pad_[128]; |
| | #endif |
| | }; |
| |
|
| | struct ThreadData { |
| | constexpr ThreadData() : thread(), steal_partition(0), queue() {} |
| | std::unique_ptr<Thread> thread; |
| | std::atomic<unsigned> steal_partition; |
| | Queue queue; |
| | }; |
| |
|
| | Environment env_; |
| | const int num_threads_; |
| | const bool allow_spinning_; |
| | MaxSizeVector<ThreadData> thread_data_; |
| | MaxSizeVector<MaxSizeVector<unsigned>> all_coprimes_; |
| | MaxSizeVector<EventCount::Waiter> waiters_; |
| | unsigned global_steal_partition_; |
| | std::atomic<unsigned> blocked_; |
| | std::atomic<bool> spinning_; |
| | std::atomic<bool> done_; |
| | std::atomic<bool> cancelled_; |
| | EventCount ec_; |
| | #ifndef EIGEN_THREAD_LOCAL |
| | std::unique_ptr<Barrier> init_barrier_; |
| | std::mutex per_thread_map_mutex_; |
| | std::unordered_map<uint64_t, std::unique_ptr<PerThread>> per_thread_map_; |
| | #endif |
| |
|
| | |
| | void WorkerLoop(int thread_id) { |
| | #ifndef EIGEN_THREAD_LOCAL |
| | std::unique_ptr<PerThread> new_pt(new PerThread()); |
| | per_thread_map_mutex_.lock(); |
| | bool insertOK = per_thread_map_.emplace(GlobalThreadIdHash(), std::move(new_pt)).second; |
| | eigen_plain_assert(insertOK); |
| | EIGEN_UNUSED_VARIABLE(insertOK); |
| | per_thread_map_mutex_.unlock(); |
| | init_barrier_->Notify(); |
| | init_barrier_->Wait(); |
| | #endif |
| | PerThread* pt = GetPerThread(); |
| | pt->pool = this; |
| | pt->rand = GlobalThreadIdHash(); |
| | pt->thread_id = thread_id; |
| | Queue& q = thread_data_[thread_id].queue; |
| | EventCount::Waiter* waiter = &waiters_[thread_id]; |
| | |
| | |
| | |
| | |
| | const int spin_count = |
| | allow_spinning_ && num_threads_ > 0 ? 5000 / num_threads_ : 0; |
| | if (num_threads_ == 1) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | while (!cancelled_) { |
| | Task t = q.PopFront(); |
| | for (int i = 0; i < spin_count && !t.f; i++) { |
| | if (!cancelled_.load(std::memory_order_relaxed)) { |
| | t = q.PopFront(); |
| | } |
| | } |
| | if (!t.f) { |
| | if (!WaitForWork(waiter, &t)) { |
| | return; |
| | } |
| | } |
| | if (t.f) { |
| | env_.ExecuteTask(t); |
| | } |
| | } |
| | } else { |
| | while (!cancelled_) { |
| | Task t = q.PopFront(); |
| | if (!t.f) { |
| | t = LocalSteal(); |
| | if (!t.f) { |
| | t = GlobalSteal(); |
| | if (!t.f) { |
| | |
| | if (allow_spinning_ && !spinning_ && !spinning_.exchange(true)) { |
| | for (int i = 0; i < spin_count && !t.f; i++) { |
| | if (!cancelled_.load(std::memory_order_relaxed)) { |
| | t = GlobalSteal(); |
| | } else { |
| | return; |
| | } |
| | } |
| | spinning_ = false; |
| | } |
| | if (!t.f) { |
| | if (!WaitForWork(waiter, &t)) { |
| | return; |
| | } |
| | } |
| | } |
| | } |
| | } |
| | if (t.f) { |
| | env_.ExecuteTask(t); |
| | } |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | Task Steal(unsigned start, unsigned limit) { |
| | PerThread* pt = GetPerThread(); |
| | const size_t size = limit - start; |
| | unsigned r = Rand(&pt->rand); |
| | |
| | |
| | eigen_plain_assert(all_coprimes_[size - 1].size() < (1<<30)); |
| | unsigned victim = ((uint64_t)r * (uint64_t)size) >> 32; |
| | unsigned index = ((uint64_t) all_coprimes_[size - 1].size() * (uint64_t)r) >> 32; |
| | unsigned inc = all_coprimes_[size - 1][index]; |
| |
|
| | for (unsigned i = 0; i < size; i++) { |
| | eigen_plain_assert(start + victim < limit); |
| | Task t = thread_data_[start + victim].queue.PopBack(); |
| | if (t.f) { |
| | return t; |
| | } |
| | victim += inc; |
| | if (victim >= size) { |
| | victim -= size; |
| | } |
| | } |
| | return Task(); |
| | } |
| |
|
| | |
| | Task LocalSteal() { |
| | PerThread* pt = GetPerThread(); |
| | unsigned partition = GetStealPartition(pt->thread_id); |
| | |
| | |
| | if (global_steal_partition_ == partition) return Task(); |
| | unsigned start, limit; |
| | DecodePartition(partition, &start, &limit); |
| | AssertBounds(start, limit); |
| |
|
| | return Steal(start, limit); |
| | } |
| |
|
| | |
| | Task GlobalSteal() { |
| | return Steal(0, num_threads_); |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | bool WaitForWork(EventCount::Waiter* waiter, Task* t) { |
| | eigen_plain_assert(!t->f); |
| | |
| | |
| | ec_.Prewait(); |
| | |
| | int victim = NonEmptyQueueIndex(); |
| | if (victim != -1) { |
| | ec_.CancelWait(); |
| | if (cancelled_) { |
| | return false; |
| | } else { |
| | *t = thread_data_[victim].queue.PopBack(); |
| | return true; |
| | } |
| | } |
| | |
| | |
| | |
| | blocked_++; |
| | |
| | if (done_ && blocked_ == static_cast<unsigned>(num_threads_)) { |
| | ec_.CancelWait(); |
| | |
| | |
| | |
| | |
| | |
| | if (NonEmptyQueueIndex() != -1) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | blocked_--; |
| | return true; |
| | } |
| | |
| | ec_.Notify(true); |
| | return false; |
| | } |
| | ec_.CommitWait(waiter); |
| | blocked_--; |
| | return true; |
| | } |
| |
|
| | int NonEmptyQueueIndex() { |
| | PerThread* pt = GetPerThread(); |
| | |
| | |
| | |
| | const size_t size = thread_data_.size(); |
| | unsigned r = Rand(&pt->rand); |
| | unsigned inc = all_coprimes_[size - 1][r % all_coprimes_[size - 1].size()]; |
| | unsigned victim = r % size; |
| | for (unsigned i = 0; i < size; i++) { |
| | if (!thread_data_[victim].queue.Empty()) { |
| | return victim; |
| | } |
| | victim += inc; |
| | if (victim >= size) { |
| | victim -= size; |
| | } |
| | } |
| | return -1; |
| | } |
| |
|
| | static EIGEN_STRONG_INLINE uint64_t GlobalThreadIdHash() { |
| | return std::hash<std::thread::id>()(std::this_thread::get_id()); |
| | } |
| |
|
| | EIGEN_STRONG_INLINE PerThread* GetPerThread() { |
| | #ifndef EIGEN_THREAD_LOCAL |
| | static PerThread dummy; |
| | auto it = per_thread_map_.find(GlobalThreadIdHash()); |
| | if (it == per_thread_map_.end()) { |
| | return &dummy; |
| | } else { |
| | return it->second.get(); |
| | } |
| | #else |
| | EIGEN_THREAD_LOCAL PerThread per_thread_; |
| | PerThread* pt = &per_thread_; |
| | return pt; |
| | #endif |
| | } |
| |
|
| | static EIGEN_STRONG_INLINE unsigned Rand(uint64_t* state) { |
| | uint64_t current = *state; |
| | |
| | *state = current * 6364136223846793005ULL + 0xda3e39cb94b95bdbULL; |
| | |
| | return static_cast<unsigned>((current ^ (current >> 22)) >> |
| | (22 + (current >> 61))); |
| | } |
| | }; |
| |
|
| | typedef ThreadPoolTempl<StlThreadEnvironment> ThreadPool; |
| |
|
| | } |
| |
|
| | #endif |
| |
|