2016-04-14 15:23:10 -07:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2016 Dmitry Vyukov <dvyukov@google.com>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H
|
|
|
|
|
#define EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H
|
|
|
|
|
|
|
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
|
|
|
template <typename Environment>
|
2018-07-16 15:06:57 -07:00
|
|
|
class ThreadPoolTempl : public Eigen::ThreadPoolInterface {
|
2016-04-14 15:23:10 -07:00
|
|
|
public:
|
|
|
|
|
typedef typename Environment::Task Task;
|
|
|
|
|
typedef RunQueue<Task, 1024> Queue;
|
|
|
|
|
|
2018-07-16 15:06:57 -07:00
|
|
|
ThreadPoolTempl(int num_threads, Environment env = Environment())
|
|
|
|
|
: ThreadPoolTempl(num_threads, true, env) {}
|
2017-03-10 08:48:20 -08:00
|
|
|
|
2018-07-16 15:06:57 -07:00
|
|
|
ThreadPoolTempl(int num_threads, bool allow_spinning,
|
2018-08-13 15:31:23 -07:00
|
|
|
Environment env = Environment())
|
2017-07-06 21:11:15 -07:00
|
|
|
: env_(env),
|
|
|
|
|
num_threads_(num_threads),
|
2017-03-09 15:41:03 -08:00
|
|
|
allow_spinning_(allow_spinning),
|
|
|
|
|
threads_(num_threads),
|
|
|
|
|
queues_(num_threads),
|
|
|
|
|
coprimes_(num_threads),
|
|
|
|
|
waiters_(num_threads),
|
|
|
|
|
blocked_(0),
|
|
|
|
|
spinning_(0),
|
|
|
|
|
done_(false),
|
|
|
|
|
cancelled_(false),
|
|
|
|
|
ec_(waiters_) {
|
2017-03-10 08:48:20 -08:00
|
|
|
waiters_.resize(num_threads_);
|
2016-04-14 15:23:10 -07:00
|
|
|
|
2017-03-10 08:48:20 -08:00
|
|
|
// Calculate coprimes of num_threads_.
|
|
|
|
|
// Coprimes are used for a random walk over all threads in Steal
|
|
|
|
|
// and NonEmptyQueueIndex. Iteration is based on the fact that if we take
|
|
|
|
|
// a walk starting thread index t and calculate num_threads - 1 subsequent
|
|
|
|
|
// indices as (t + coprime) % num_threads, we will cover all threads without
|
|
|
|
|
// repetitions (effectively getting a presudo-random permutation of thread
|
|
|
|
|
// indices).
|
|
|
|
|
for (int i = 1; i <= num_threads_; i++) {
|
|
|
|
|
unsigned a = i;
|
|
|
|
|
unsigned b = num_threads_;
|
|
|
|
|
// If GCD(a, b) == 1, then a and b are coprimes.
|
|
|
|
|
while (b != 0) {
|
|
|
|
|
unsigned tmp = a;
|
|
|
|
|
a = b;
|
|
|
|
|
b = tmp % b;
|
|
|
|
|
}
|
|
|
|
|
if (a == 1) {
|
|
|
|
|
coprimes_.push_back(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-23 19:41:59 +02:00
|
|
|
queues_.resize(num_threads_);
|
2018-08-13 15:31:23 -07:00
|
|
|
#ifndef EIGEN_THREAD_LOCAL
|
|
|
|
|
init_barrier_.reset(new Barrier(num_threads_));
|
|
|
|
|
#endif
|
2017-03-10 08:48:20 -08:00
|
|
|
for (int i = 0; i < num_threads_; i++) {
|
2018-08-23 19:41:59 +02:00
|
|
|
threads_.emplace_back(env_.CreateThread([this, i]() { WorkerLoop(i); }));
|
2017-03-10 08:48:20 -08:00
|
|
|
}
|
2018-08-13 15:31:23 -07:00
|
|
|
#ifndef EIGEN_THREAD_LOCAL
|
|
|
|
|
// Wait for workers to initialize per_thread_map_. Otherwise we might race
|
|
|
|
|
// with them in Schedule or CurrentThreadId.
|
|
|
|
|
init_barrier_->Wait();
|
|
|
|
|
#endif
|
2017-03-10 08:48:20 -08:00
|
|
|
}
|
2017-03-10 08:30:22 -08:00
|
|
|
|
2018-07-16 15:06:57 -07:00
|
|
|
~ThreadPoolTempl() {
|
2016-05-12 11:45:48 -07:00
|
|
|
done_ = true;
|
2016-12-09 13:05:14 -08:00
|
|
|
|
2016-04-14 15:23:10 -07:00
|
|
|
// Now if all threads block without work, they will start exiting.
|
|
|
|
|
// But note that threads can continue to work arbitrary long,
|
|
|
|
|
// block, submit new work, unblock and otherwise live full life.
|
2016-12-09 13:05:14 -08:00
|
|
|
if (!cancelled_) {
|
|
|
|
|
ec_.Notify(true);
|
|
|
|
|
} else {
|
|
|
|
|
// Since we were cancelled, there might be entries in the queues.
|
|
|
|
|
// Empty them to prevent their destructor from asserting.
|
|
|
|
|
for (size_t i = 0; i < queues_.size(); i++) {
|
2018-08-23 19:41:59 +02:00
|
|
|
queues_[i].Flush();
|
2016-12-09 13:05:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-14 15:23:10 -07:00
|
|
|
|
|
|
|
|
// Join threads explicitly to avoid destruction order issues.
|
2018-08-23 19:41:59 +02:00
|
|
|
threads_.resize(0);
|
2018-08-23 11:36:49 -07:00
|
|
|
queues_.resize(0);
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Schedule(std::function<void()> fn) {
|
|
|
|
|
Task t = env_.CreateTask(std::move(fn));
|
|
|
|
|
PerThread* pt = GetPerThread();
|
|
|
|
|
if (pt->pool == this) {
|
|
|
|
|
// Worker thread of this pool, push onto the thread's queue.
|
2018-08-23 19:41:59 +02:00
|
|
|
Queue& q = queues_[pt->thread_id];
|
|
|
|
|
t = q.PushFront(std::move(t));
|
2016-04-14 15:23:10 -07:00
|
|
|
} else {
|
|
|
|
|
// A free-standing thread (or worker of another pool), push onto a random
|
|
|
|
|
// queue.
|
2018-08-23 19:41:59 +02:00
|
|
|
Queue& q = queues_[Rand(&pt->rand) % queues_.size()];
|
|
|
|
|
t = q.PushBack(std::move(t));
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
// Note: below we touch this after making w available to worker threads.
|
|
|
|
|
// Strictly speaking, this can lead to a racy-use-after-free. Consider that
|
|
|
|
|
// Schedule is called from a thread that is neither main thread nor a worker
|
|
|
|
|
// thread of this pool. Then, execution of w directly or indirectly
|
|
|
|
|
// completes overall computations, which in turn leads to destruction of
|
|
|
|
|
// this. We expect that such scenario is prevented by program, that is,
|
|
|
|
|
// this is kept alive while any threads can potentially be in Schedule.
|
2016-12-09 13:05:14 -08:00
|
|
|
if (!t.f) {
|
2016-04-14 15:23:10 -07:00
|
|
|
ec_.Notify(false);
|
2018-08-13 15:31:23 -07:00
|
|
|
} else {
|
2016-04-14 15:23:10 -07:00
|
|
|
env_.ExecuteTask(t); // Push failed, execute directly.
|
2016-12-09 13:05:14 -08:00
|
|
|
}
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-08 08:12:49 -08:00
|
|
|
void Cancel() {
|
2016-12-09 13:05:14 -08:00
|
|
|
cancelled_ = true;
|
|
|
|
|
done_ = true;
|
|
|
|
|
|
|
|
|
|
// Let each thread know it's been cancelled.
|
2016-12-14 18:33:39 -08:00
|
|
|
#ifdef EIGEN_THREAD_ENV_SUPPORTS_CANCELLATION
|
2016-12-08 08:12:49 -08:00
|
|
|
for (size_t i = 0; i < threads_.size(); i++) {
|
2016-12-09 13:05:14 -08:00
|
|
|
threads_[i]->OnCancel();
|
2016-12-08 08:12:49 -08:00
|
|
|
}
|
2016-12-14 18:33:39 -08:00
|
|
|
#endif
|
2016-12-09 13:05:14 -08:00
|
|
|
|
|
|
|
|
// Wake up the threads without work to let them exit on their own.
|
|
|
|
|
ec_.Notify(true);
|
2016-12-08 08:12:49 -08:00
|
|
|
}
|
|
|
|
|
|
2018-08-13 15:31:23 -07:00
|
|
|
int NumThreads() const final { return num_threads_; }
|
2016-06-03 16:28:58 -07:00
|
|
|
|
2016-06-23 16:40:07 -07:00
|
|
|
int CurrentThreadId() const final {
|
2018-08-13 15:31:23 -07:00
|
|
|
const PerThread* pt = const_cast<ThreadPoolTempl*>(this)->GetPerThread();
|
2016-06-03 16:28:58 -07:00
|
|
|
if (pt->pool == this) {
|
2016-06-03 18:06:37 -07:00
|
|
|
return pt->thread_id;
|
2016-06-03 16:28:58 -07:00
|
|
|
} else {
|
2016-06-23 16:40:07 -07:00
|
|
|
return -1;
|
2016-06-03 16:28:58 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 15:23:10 -07:00
|
|
|
private:
|
|
|
|
|
typedef typename Environment::EnvThread Thread;
|
|
|
|
|
|
|
|
|
|
struct PerThread {
|
2018-08-13 15:31:23 -07:00
|
|
|
constexpr PerThread() : pool(NULL), rand(0), thread_id(-1) {}
|
2018-07-16 15:06:57 -07:00
|
|
|
ThreadPoolTempl* pool; // Parent pool, or null for normal threads.
|
2018-08-13 15:31:23 -07:00
|
|
|
uint64_t rand; // Random generator state.
|
|
|
|
|
int thread_id; // Worker thread index in pool.
|
2018-08-23 12:54:33 -07:00
|
|
|
#ifndef EIGEN_THREAD_LOCAL
|
|
|
|
|
// Prevent false sharing.
|
|
|
|
|
char pad_[128];
|
|
|
|
|
#endif
|
2016-04-14 15:23:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Environment env_;
|
2017-03-09 15:41:03 -08:00
|
|
|
const int num_threads_;
|
|
|
|
|
const bool allow_spinning_;
|
2018-08-23 19:41:59 +02:00
|
|
|
MaxSizeVector<std::unique_ptr<Thread> > threads_;
|
|
|
|
|
MaxSizeVector<Queue> queues_;
|
2016-05-09 10:17:17 -07:00
|
|
|
MaxSizeVector<unsigned> coprimes_;
|
2016-09-02 19:25:47 -07:00
|
|
|
MaxSizeVector<EventCount::Waiter> waiters_;
|
2016-04-14 15:23:10 -07:00
|
|
|
std::atomic<unsigned> blocked_;
|
|
|
|
|
std::atomic<bool> spinning_;
|
|
|
|
|
std::atomic<bool> done_;
|
2016-12-09 13:05:14 -08:00
|
|
|
std::atomic<bool> cancelled_;
|
2016-04-14 15:23:10 -07:00
|
|
|
EventCount ec_;
|
2018-08-13 15:31:23 -07:00
|
|
|
#ifndef EIGEN_THREAD_LOCAL
|
|
|
|
|
std::unique_ptr<Barrier> init_barrier_;
|
2018-08-23 12:11:58 -07:00
|
|
|
std::mutex per_thread_map_mutex_; // Protects per_thread_map_.
|
2018-08-23 12:10:08 -07:00
|
|
|
std::unordered_map<uint64_t, std::unique_ptr<PerThread>> per_thread_map_;
|
2018-08-13 15:31:23 -07:00
|
|
|
#endif
|
2016-04-14 15:23:10 -07:00
|
|
|
|
|
|
|
|
// Main worker thread loop.
|
2016-06-03 18:06:37 -07:00
|
|
|
void WorkerLoop(int thread_id) {
|
2018-08-13 15:31:23 -07:00
|
|
|
#ifndef EIGEN_THREAD_LOCAL
|
2018-08-23 13:06:39 -07:00
|
|
|
std::unique_ptr<PerThread> new_pt(new PerThread());
|
2018-08-23 12:11:58 -07:00
|
|
|
per_thread_map_mutex_.lock();
|
2018-08-23 13:06:39 -07:00
|
|
|
eigen_assert(per_thread_map_.emplace(GlobalThreadIdHash(), std::move(new_pt)).second);
|
2018-08-23 12:11:58 -07:00
|
|
|
per_thread_map_mutex_.unlock();
|
2018-08-13 15:31:23 -07:00
|
|
|
init_barrier_->Notify();
|
|
|
|
|
init_barrier_->Wait();
|
|
|
|
|
#endif
|
2018-08-23 12:10:08 -07:00
|
|
|
PerThread* pt = GetPerThread();
|
2016-04-14 15:23:10 -07:00
|
|
|
pt->pool = this;
|
2018-08-13 15:31:23 -07:00
|
|
|
pt->rand = GlobalThreadIdHash();
|
2016-06-03 16:28:58 -07:00
|
|
|
pt->thread_id = thread_id;
|
2018-08-23 19:41:59 +02:00
|
|
|
Queue& q = queues_[thread_id];
|
2016-06-03 16:28:58 -07:00
|
|
|
EventCount::Waiter* waiter = &waiters_[thread_id];
|
2017-03-09 15:41:03 -08:00
|
|
|
// TODO(dvyukov,rmlarsen): The time spent in Steal() is proportional
|
|
|
|
|
// to num_threads_ and we assume that new work is scheduled at a
|
|
|
|
|
// constant rate, so we set spin_count to 5000 / num_threads_. The
|
|
|
|
|
// constant was picked based on a fair dice roll, tune it.
|
|
|
|
|
const int spin_count =
|
|
|
|
|
allow_spinning_ && num_threads_ > 0 ? 5000 / num_threads_ : 0;
|
|
|
|
|
if (num_threads_ == 1) {
|
|
|
|
|
// For num_threads_ == 1 there is no point in going through the expensive
|
|
|
|
|
// steal loop. Moreover, since Steal() calls PopBack() on the victim
|
|
|
|
|
// queues it might reverse the order in which ops are executed compared to
|
|
|
|
|
// the order in which they are scheduled, which tends to be
|
|
|
|
|
// counter-productive for the types of I/O workloads the single thread
|
|
|
|
|
// pools tend to be used for.
|
|
|
|
|
while (!cancelled_) {
|
2018-08-23 19:41:59 +02:00
|
|
|
Task t = q.PopFront();
|
2017-03-09 15:41:03 -08:00
|
|
|
for (int i = 0; i < spin_count && !t.f; i++) {
|
|
|
|
|
if (!cancelled_.load(std::memory_order_relaxed)) {
|
2018-08-23 19:41:59 +02:00
|
|
|
t = q.PopFront();
|
2017-03-09 15:41:03 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 10:17:17 -07:00
|
|
|
if (!t.f) {
|
2017-03-09 15:41:03 -08:00
|
|
|
if (!WaitForWork(waiter, &t)) {
|
|
|
|
|
return;
|
2016-05-09 10:17:17 -07:00
|
|
|
}
|
2017-03-09 15:41:03 -08:00
|
|
|
}
|
|
|
|
|
if (t.f) {
|
|
|
|
|
env_.ExecuteTask(t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
while (!cancelled_) {
|
2018-08-23 19:41:59 +02:00
|
|
|
Task t = q.PopFront();
|
2017-03-09 15:41:03 -08:00
|
|
|
if (!t.f) {
|
|
|
|
|
t = Steal();
|
2016-05-09 10:17:17 -07:00
|
|
|
if (!t.f) {
|
2017-03-09 15:41:03 -08:00
|
|
|
// Leave one thread spinning. This reduces latency.
|
|
|
|
|
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 = Steal();
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spinning_ = false;
|
|
|
|
|
}
|
|
|
|
|
if (!t.f) {
|
|
|
|
|
if (!WaitForWork(waiter, &t)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-09 15:41:03 -08:00
|
|
|
if (t.f) {
|
|
|
|
|
env_.ExecuteTask(t);
|
|
|
|
|
}
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Steal tries to steal work from other worker threads in best-effort manner.
|
2016-05-09 10:17:17 -07:00
|
|
|
Task Steal() {
|
2016-04-14 15:23:10 -07:00
|
|
|
PerThread* pt = GetPerThread();
|
2016-05-26 15:57:19 -07:00
|
|
|
const size_t size = queues_.size();
|
2016-05-09 10:17:17 -07:00
|
|
|
unsigned r = Rand(&pt->rand);
|
|
|
|
|
unsigned inc = coprimes_[r % coprimes_.size()];
|
|
|
|
|
unsigned victim = r % size;
|
|
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2018-08-23 19:41:59 +02:00
|
|
|
Task t = queues_[victim].PopBack();
|
2016-05-09 10:17:17 -07:00
|
|
|
if (t.f) {
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
victim += inc;
|
|
|
|
|
if (victim >= size) {
|
|
|
|
|
victim -= size;
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 10:17:17 -07:00
|
|
|
return Task();
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:17:17 -07:00
|
|
|
// WaitForWork blocks until new work is available (returns true), or if it is
|
|
|
|
|
// time to exit (returns false). Can optionally return a task to execute in t
|
|
|
|
|
// (in such case t.f != nullptr on return).
|
|
|
|
|
bool WaitForWork(EventCount::Waiter* waiter, Task* t) {
|
|
|
|
|
eigen_assert(!t->f);
|
|
|
|
|
// We already did best-effort emptiness check in Steal, so prepare for
|
|
|
|
|
// blocking.
|
2016-04-14 15:23:10 -07:00
|
|
|
ec_.Prewait(waiter);
|
2016-05-09 10:17:17 -07:00
|
|
|
// Now do a reliable emptiness check.
|
|
|
|
|
int victim = NonEmptyQueueIndex();
|
|
|
|
|
if (victim != -1) {
|
2016-04-14 15:23:10 -07:00
|
|
|
ec_.CancelWait(waiter);
|
2016-12-09 13:05:14 -08:00
|
|
|
if (cancelled_) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2018-08-23 19:41:59 +02:00
|
|
|
*t = queues_[victim].PopBack();
|
2016-12-09 13:05:14 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
// Number of blocked threads is used as termination condition.
|
|
|
|
|
// If we are shutting down and all worker threads blocked without work,
|
|
|
|
|
// that's we are done.
|
|
|
|
|
blocked_++;
|
2018-08-23 19:41:59 +02:00
|
|
|
// TODO is blocked_ required to be unsigned?
|
2018-08-14 12:17:46 -07:00
|
|
|
if (done_ && blocked_ == static_cast<unsigned>(num_threads_)) {
|
2016-04-14 15:23:10 -07:00
|
|
|
ec_.CancelWait(waiter);
|
|
|
|
|
// Almost done, but need to re-check queues.
|
|
|
|
|
// Consider that all queues are empty and all worker threads are preempted
|
|
|
|
|
// right after incrementing blocked_ above. Now a free-standing thread
|
|
|
|
|
// submits work and calls destructor (which sets done_). If we don't
|
|
|
|
|
// re-check queues, we will exit leaving the work unexecuted.
|
2016-05-09 10:17:17 -07:00
|
|
|
if (NonEmptyQueueIndex() != -1) {
|
2016-04-14 15:23:10 -07:00
|
|
|
// Note: we must not pop from queues before we decrement blocked_,
|
|
|
|
|
// otherwise the following scenario is possible. Consider that instead
|
|
|
|
|
// of checking for emptiness we popped the only element from queues.
|
|
|
|
|
// Now other worker threads can start exiting, which is bad if the
|
|
|
|
|
// work item submits other work. So we just check emptiness here,
|
|
|
|
|
// which ensures that all worker threads exit at the same time.
|
|
|
|
|
blocked_--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Reached stable termination state.
|
|
|
|
|
ec_.Notify(true);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
ec_.CommitWait(waiter);
|
|
|
|
|
blocked_--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:17:17 -07:00
|
|
|
int NonEmptyQueueIndex() {
|
|
|
|
|
PerThread* pt = GetPerThread();
|
2016-05-26 15:57:19 -07:00
|
|
|
const size_t size = queues_.size();
|
2016-05-09 10:17:17 -07:00
|
|
|
unsigned r = Rand(&pt->rand);
|
|
|
|
|
unsigned inc = coprimes_[r % coprimes_.size()];
|
|
|
|
|
unsigned victim = r % size;
|
|
|
|
|
for (unsigned i = 0; i < size; i++) {
|
2018-08-23 19:41:59 +02:00
|
|
|
if (!queues_[victim].Empty()) {
|
2016-05-09 10:17:17 -07:00
|
|
|
return victim;
|
|
|
|
|
}
|
|
|
|
|
victim += inc;
|
|
|
|
|
if (victim >= size) {
|
|
|
|
|
victim -= size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
2018-08-13 15:31:23 -07:00
|
|
|
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 {
|
2018-08-23 12:10:08 -07:00
|
|
|
return it->second.get();
|
2018-08-13 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
#else
|
2016-04-19 15:56:02 -07:00
|
|
|
EIGEN_THREAD_LOCAL PerThread per_thread_;
|
2016-04-14 15:23:10 -07:00
|
|
|
PerThread* pt = &per_thread_;
|
|
|
|
|
return pt;
|
2018-08-13 15:31:23 -07:00
|
|
|
#endif
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-14 17:51:47 -07:00
|
|
|
static EIGEN_STRONG_INLINE unsigned Rand(uint64_t* state) {
|
|
|
|
|
uint64_t current = *state;
|
|
|
|
|
// Update the internal state
|
|
|
|
|
*state = current * 6364136223846793005ULL + 0xda3e39cb94b95bdbULL;
|
|
|
|
|
// Generate the random output (using the PCG-XSH-RS scheme)
|
2018-08-13 15:31:23 -07:00
|
|
|
return static_cast<unsigned>((current ^ (current >> 22)) >>
|
|
|
|
|
(22 + (current >> 61)));
|
2016-04-14 15:23:10 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-16 15:06:57 -07:00
|
|
|
typedef ThreadPoolTempl<StlThreadEnvironment> ThreadPool;
|
2016-04-14 15:23:10 -07:00
|
|
|
|
|
|
|
|
} // namespace Eigen
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H
|