mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Thread pool
This commit is contained in:
committed by
Rasmus Munk Larsen
parent
9eb8e2afba
commit
94f57867fe
@@ -18,8 +18,8 @@
|
||||
#include "../../../Eigen/src/Core/util/DisableStupidWarnings.h"
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include "src/util/CXX11Meta.h"
|
||||
#include "src/util/MaxSizeVector.h"
|
||||
#include "src/Core/util/Meta.h"
|
||||
#include "src/Core/util/MaxSizeVector.h"
|
||||
// IWYU pragma: end_exports
|
||||
|
||||
/** \defgroup CXX11_Tensor_Module Tensor Module
|
||||
|
||||
@@ -1,73 +1 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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_MODULE_H
|
||||
#define EIGEN_CXX11_THREADPOOL_MODULE_H
|
||||
|
||||
#include "../../../Eigen/Core"
|
||||
|
||||
#include "../../../Eigen/src/Core/util/DisableStupidWarnings.h"
|
||||
|
||||
/** \defgroup CXX11_ThreadPool_Module C++11 ThreadPool Module
|
||||
*
|
||||
* This module provides 2 threadpool implementations
|
||||
* - a simple reference implementation
|
||||
* - a faster non blocking implementation
|
||||
*
|
||||
* This module requires C++11.
|
||||
*
|
||||
* \code
|
||||
* #include <Eigen/CXX11/ThreadPool>
|
||||
* \endcode
|
||||
*/
|
||||
|
||||
|
||||
// The code depends on CXX11, so only include the module if the
|
||||
// compiler supports it.
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <time.h>
|
||||
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
// There are non-parenthesized calls to "max" in the <unordered_map> header,
|
||||
// which trigger a check in test/main.h causing compilation to fail.
|
||||
// We work around the check here by removing the check for max in
|
||||
// the case where we have to emulate thread_local.
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
#include <unordered_map>
|
||||
|
||||
#include "src/util/CXX11Meta.h"
|
||||
#include "src/util/MaxSizeVector.h"
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include "src/ThreadPool/ThreadLocal.h"
|
||||
#include "src/ThreadPool/ThreadYield.h"
|
||||
#include "src/ThreadPool/ThreadCancel.h"
|
||||
#include "src/ThreadPool/EventCount.h"
|
||||
#include "src/ThreadPool/RunQueue.h"
|
||||
#include "src/ThreadPool/ThreadPoolInterface.h"
|
||||
#include "src/ThreadPool/ThreadEnvironment.h"
|
||||
#include "src/ThreadPool/Barrier.h"
|
||||
#include "src/ThreadPool/NonBlockingThreadPool.h"
|
||||
// IWYU pragma: end_exports
|
||||
|
||||
#include "../../../Eigen/src/Core/util/ReenableStupidWarnings.h"
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_MODULE_H
|
||||
#include "../../../Eigen/ThreadPool"
|
||||
@@ -1,69 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2018 Rasmus Munk Larsen <rmlarsen@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/.
|
||||
|
||||
// Barrier is an object that allows one or more threads to wait until
|
||||
// Notify has been called a specified number of times.
|
||||
|
||||
#ifndef EIGEN_CXX11_THREADPOOL_BARRIER_H
|
||||
#define EIGEN_CXX11_THREADPOOL_BARRIER_H
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
class Barrier {
|
||||
public:
|
||||
Barrier(unsigned int count) : state_(count << 1), notified_(false) {
|
||||
eigen_plain_assert(((count << 1) >> 1) == count);
|
||||
}
|
||||
~Barrier() { eigen_plain_assert((state_ >> 1) == 0); }
|
||||
|
||||
void Notify() {
|
||||
unsigned int v = state_.fetch_sub(2, std::memory_order_acq_rel) - 2;
|
||||
if (v != 1) {
|
||||
// Clear the lowest bit (waiter flag) and check that the original state
|
||||
// value was not zero. If it was zero, it means that notify was called
|
||||
// more times than the original count.
|
||||
eigen_plain_assert(((v + 2) & ~1) != 0);
|
||||
return; // either count has not dropped to 0, or waiter is not waiting
|
||||
}
|
||||
std::unique_lock<std::mutex> l(mu_);
|
||||
eigen_plain_assert(!notified_);
|
||||
notified_ = true;
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void Wait() {
|
||||
unsigned int v = state_.fetch_or(1, std::memory_order_acq_rel);
|
||||
if ((v >> 1) == 0) return;
|
||||
std::unique_lock<std::mutex> l(mu_);
|
||||
while (!notified_) {
|
||||
cv_.wait(l);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mu_;
|
||||
std::condition_variable cv_;
|
||||
std::atomic<unsigned int> state_; // low bit is waiter flag
|
||||
bool notified_;
|
||||
};
|
||||
|
||||
// Notification is an object that allows a user to to wait for another
|
||||
// thread to signal a notification that an event has occurred.
|
||||
//
|
||||
// Multiple threads can wait on the same Notification object,
|
||||
// but only one caller must call Notify() on the object.
|
||||
struct Notification : Barrier {
|
||||
Notification() : Barrier(1){};
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_BARRIER_H
|
||||
@@ -1,251 +0,0 @@
|
||||
// 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_EVENTCOUNT_H
|
||||
#define EIGEN_CXX11_THREADPOOL_EVENTCOUNT_H
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
// EventCount allows to wait for arbitrary predicates in non-blocking
|
||||
// algorithms. Think of condition variable, but wait predicate does not need to
|
||||
// be protected by a mutex. Usage:
|
||||
// Waiting thread does:
|
||||
//
|
||||
// if (predicate)
|
||||
// return act();
|
||||
// EventCount::Waiter& w = waiters[my_index];
|
||||
// ec.Prewait(&w);
|
||||
// if (predicate) {
|
||||
// ec.CancelWait(&w);
|
||||
// return act();
|
||||
// }
|
||||
// ec.CommitWait(&w);
|
||||
//
|
||||
// Notifying thread does:
|
||||
//
|
||||
// predicate = true;
|
||||
// ec.Notify(true);
|
||||
//
|
||||
// Notify is cheap if there are no waiting threads. Prewait/CommitWait are not
|
||||
// cheap, but they are executed only if the preceding predicate check has
|
||||
// failed.
|
||||
//
|
||||
// Algorithm outline:
|
||||
// There are two main variables: predicate (managed by user) and state_.
|
||||
// Operation closely resembles Dekker mutual algorithm:
|
||||
// https://en.wikipedia.org/wiki/Dekker%27s_algorithm
|
||||
// Waiting thread sets state_ then checks predicate, Notifying thread sets
|
||||
// predicate then checks state_. Due to seq_cst fences in between these
|
||||
// operations it is guaranteed than either waiter will see predicate change
|
||||
// and won't block, or notifying thread will see state_ change and will unblock
|
||||
// the waiter, or both. But it can't happen that both threads don't see each
|
||||
// other changes, which would lead to deadlock.
|
||||
class EventCount {
|
||||
public:
|
||||
class Waiter;
|
||||
|
||||
EventCount(MaxSizeVector<Waiter>& waiters)
|
||||
: state_(kStackMask), waiters_(waiters) {
|
||||
eigen_plain_assert(waiters.size() < (1 << kWaiterBits) - 1);
|
||||
}
|
||||
|
||||
~EventCount() {
|
||||
// Ensure there are no waiters.
|
||||
eigen_plain_assert(state_.load() == kStackMask);
|
||||
}
|
||||
|
||||
// Prewait prepares for waiting.
|
||||
// After calling Prewait, the thread must re-check the wait predicate
|
||||
// and then call either CancelWait or CommitWait.
|
||||
void Prewait() {
|
||||
uint64_t state = state_.load(std::memory_order_relaxed);
|
||||
for (;;) {
|
||||
CheckState(state);
|
||||
uint64_t newstate = state + kWaiterInc;
|
||||
CheckState(newstate);
|
||||
if (state_.compare_exchange_weak(state, newstate,
|
||||
std::memory_order_seq_cst))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// CommitWait commits waiting after Prewait.
|
||||
void CommitWait(Waiter* w) {
|
||||
eigen_plain_assert((w->epoch & ~kEpochMask) == 0);
|
||||
w->state = Waiter::kNotSignaled;
|
||||
const uint64_t me = (w - &waiters_[0]) | w->epoch;
|
||||
uint64_t state = state_.load(std::memory_order_seq_cst);
|
||||
for (;;) {
|
||||
CheckState(state, true);
|
||||
uint64_t newstate;
|
||||
if ((state & kSignalMask) != 0) {
|
||||
// Consume the signal and return immediately.
|
||||
newstate = state - kWaiterInc - kSignalInc;
|
||||
} else {
|
||||
// Remove this thread from pre-wait counter and add to the waiter stack.
|
||||
newstate = ((state & kWaiterMask) - kWaiterInc) | me;
|
||||
w->next.store(state & (kStackMask | kEpochMask),
|
||||
std::memory_order_relaxed);
|
||||
}
|
||||
CheckState(newstate);
|
||||
if (state_.compare_exchange_weak(state, newstate,
|
||||
std::memory_order_acq_rel)) {
|
||||
if ((state & kSignalMask) == 0) {
|
||||
w->epoch += kEpochInc;
|
||||
Park(w);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CancelWait cancels effects of the previous Prewait call.
|
||||
void CancelWait() {
|
||||
uint64_t state = state_.load(std::memory_order_relaxed);
|
||||
for (;;) {
|
||||
CheckState(state, true);
|
||||
uint64_t newstate = state - kWaiterInc;
|
||||
// We don't know if the thread was also notified or not,
|
||||
// so we should not consume a signal unconditionally.
|
||||
// Only if number of waiters is equal to number of signals,
|
||||
// we know that the thread was notified and we must take away the signal.
|
||||
if (((state & kWaiterMask) >> kWaiterShift) ==
|
||||
((state & kSignalMask) >> kSignalShift))
|
||||
newstate -= kSignalInc;
|
||||
CheckState(newstate);
|
||||
if (state_.compare_exchange_weak(state, newstate,
|
||||
std::memory_order_acq_rel))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Notify wakes one or all waiting threads.
|
||||
// Must be called after changing the associated wait predicate.
|
||||
void Notify(bool notifyAll) {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
uint64_t state = state_.load(std::memory_order_acquire);
|
||||
for (;;) {
|
||||
CheckState(state);
|
||||
const uint64_t waiters = (state & kWaiterMask) >> kWaiterShift;
|
||||
const uint64_t signals = (state & kSignalMask) >> kSignalShift;
|
||||
// Easy case: no waiters.
|
||||
if ((state & kStackMask) == kStackMask && waiters == signals) return;
|
||||
uint64_t newstate;
|
||||
if (notifyAll) {
|
||||
// Empty wait stack and set signal to number of pre-wait threads.
|
||||
newstate =
|
||||
(state & kWaiterMask) | (waiters << kSignalShift) | kStackMask;
|
||||
} else if (signals < waiters) {
|
||||
// There is a thread in pre-wait state, unblock it.
|
||||
newstate = state + kSignalInc;
|
||||
} else {
|
||||
// Pop a waiter from list and unpark it.
|
||||
Waiter* w = &waiters_[state & kStackMask];
|
||||
uint64_t next = w->next.load(std::memory_order_relaxed);
|
||||
newstate = (state & (kWaiterMask | kSignalMask)) | next;
|
||||
}
|
||||
CheckState(newstate);
|
||||
if (state_.compare_exchange_weak(state, newstate,
|
||||
std::memory_order_acq_rel)) {
|
||||
if (!notifyAll && (signals < waiters))
|
||||
return; // unblocked pre-wait thread
|
||||
if ((state & kStackMask) == kStackMask) return;
|
||||
Waiter* w = &waiters_[state & kStackMask];
|
||||
if (!notifyAll) w->next.store(kStackMask, std::memory_order_relaxed);
|
||||
Unpark(w);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Waiter {
|
||||
friend class EventCount;
|
||||
// Align to 128 byte boundary to prevent false sharing with other Waiter
|
||||
// objects in the same vector.
|
||||
EIGEN_ALIGN_TO_BOUNDARY(128) std::atomic<uint64_t> next;
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
uint64_t epoch = 0;
|
||||
unsigned state = kNotSignaled;
|
||||
enum {
|
||||
kNotSignaled,
|
||||
kWaiting,
|
||||
kSignaled,
|
||||
};
|
||||
};
|
||||
|
||||
private:
|
||||
// State_ layout:
|
||||
// - low kWaiterBits is a stack of waiters committed wait
|
||||
// (indexes in waiters_ array are used as stack elements,
|
||||
// kStackMask means empty stack).
|
||||
// - next kWaiterBits is count of waiters in prewait state.
|
||||
// - next kWaiterBits is count of pending signals.
|
||||
// - remaining bits are ABA counter for the stack.
|
||||
// (stored in Waiter node and incremented on push).
|
||||
static const uint64_t kWaiterBits = 14;
|
||||
static const uint64_t kStackMask = (1ull << kWaiterBits) - 1;
|
||||
static const uint64_t kWaiterShift = kWaiterBits;
|
||||
static const uint64_t kWaiterMask = ((1ull << kWaiterBits) - 1)
|
||||
<< kWaiterShift;
|
||||
static const uint64_t kWaiterInc = 1ull << kWaiterShift;
|
||||
static const uint64_t kSignalShift = 2 * kWaiterBits;
|
||||
static const uint64_t kSignalMask = ((1ull << kWaiterBits) - 1)
|
||||
<< kSignalShift;
|
||||
static const uint64_t kSignalInc = 1ull << kSignalShift;
|
||||
static const uint64_t kEpochShift = 3 * kWaiterBits;
|
||||
static const uint64_t kEpochBits = 64 - kEpochShift;
|
||||
static const uint64_t kEpochMask = ((1ull << kEpochBits) - 1) << kEpochShift;
|
||||
static const uint64_t kEpochInc = 1ull << kEpochShift;
|
||||
std::atomic<uint64_t> state_;
|
||||
MaxSizeVector<Waiter>& waiters_;
|
||||
|
||||
static void CheckState(uint64_t state, bool waiter = false) {
|
||||
static_assert(kEpochBits >= 20, "not enough bits to prevent ABA problem");
|
||||
const uint64_t waiters = (state & kWaiterMask) >> kWaiterShift;
|
||||
const uint64_t signals = (state & kSignalMask) >> kSignalShift;
|
||||
eigen_plain_assert(waiters >= signals);
|
||||
eigen_plain_assert(waiters < (1 << kWaiterBits) - 1);
|
||||
eigen_plain_assert(!waiter || waiters > 0);
|
||||
(void)waiters;
|
||||
(void)signals;
|
||||
}
|
||||
|
||||
void Park(Waiter* w) {
|
||||
std::unique_lock<std::mutex> lock(w->mu);
|
||||
while (w->state != Waiter::kSignaled) {
|
||||
w->state = Waiter::kWaiting;
|
||||
w->cv.wait(lock);
|
||||
}
|
||||
}
|
||||
|
||||
void Unpark(Waiter* w) {
|
||||
for (Waiter* next; w; w = next) {
|
||||
uint64_t wnext = w->next.load(std::memory_order_relaxed) & kStackMask;
|
||||
next = wnext == kStackMask ? nullptr : &waiters_[wnext];
|
||||
unsigned state;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(w->mu);
|
||||
state = w->state;
|
||||
w->state = Waiter::kSignaled;
|
||||
}
|
||||
// Avoid notifying if it wasn't waiting.
|
||||
if (state == Waiter::kWaiting) w->cv.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
EventCount(const EventCount&) = delete;
|
||||
void operator=(const EventCount&) = delete;
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_EVENTCOUNT_H
|
||||
@@ -1,3 +0,0 @@
|
||||
#ifndef EIGEN_CXX11_THREADPOOL_MODULE_H
|
||||
#error "Please include unsupported/Eigen/CXX11/ThreadPool instead of including headers inside the src directory directly."
|
||||
#endif
|
||||
@@ -1,488 +0,0 @@
|
||||
// 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
|
||||
|
||||
#include "./InternalHeaderCheck.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_);
|
||||
// Calculate coprimes of all numbers [1, num_threads].
|
||||
// Coprimes are used for random walks over all threads in Steal
|
||||
// and NonEmptyQueueIndex. Iteration is based on the fact that if we take
|
||||
// a random 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).
|
||||
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
|
||||
// Wait for workers to initialize per_thread_map_. Otherwise we might race
|
||||
// with them in Schedule or CurrentThreadId.
|
||||
init_barrier_->Wait();
|
||||
#endif
|
||||
}
|
||||
|
||||
~ThreadPoolTempl() {
|
||||
done_ = true;
|
||||
|
||||
// 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.
|
||||
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 < thread_data_.size(); i++) {
|
||||
thread_data_[i].queue.Flush();
|
||||
}
|
||||
}
|
||||
// Join threads explicitly (by destroying) to avoid destruction order within
|
||||
// this class.
|
||||
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_));
|
||||
|
||||
// Pass this information to each thread queue.
|
||||
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) {
|
||||
// Worker thread of this pool, push onto the thread's queue.
|
||||
Queue& q = thread_data_[pt->thread_id].queue;
|
||||
t = q.PushFront(std::move(t));
|
||||
} else {
|
||||
// A free-standing thread (or worker of another pool), push onto a random
|
||||
// queue.
|
||||
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));
|
||||
}
|
||||
// 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.
|
||||
if (!t.f) {
|
||||
ec_.Notify(false);
|
||||
} else {
|
||||
env_.ExecuteTask(t); // Push failed, execute directly.
|
||||
}
|
||||
}
|
||||
|
||||
void Cancel() EIGEN_OVERRIDE {
|
||||
cancelled_ = true;
|
||||
done_ = true;
|
||||
|
||||
// Let each thread know it's been cancelled.
|
||||
#ifdef EIGEN_THREAD_ENV_SUPPORTS_CANCELLATION
|
||||
for (size_t i = 0; i < thread_data_.size(); i++) {
|
||||
thread_data_[i].thread->OnCancel();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Wake up the threads without work to let them exit on their own.
|
||||
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:
|
||||
// Create a single atomic<int> that encodes start and limit information for
|
||||
// each thread.
|
||||
// We expect num_threads_ < 65536, so we can store them in a single
|
||||
// std::atomic<unsigned>.
|
||||
// Exposed publicly as static functions so that external callers can reuse
|
||||
// this encode/decode logic for maintaining their own thread-safe copies of
|
||||
// scheduling and steal domain(s).
|
||||
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); // non-zero sized partition
|
||||
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;
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename Environment::EnvThread Thread;
|
||||
|
||||
struct PerThread {
|
||||
constexpr PerThread() : pool(NULL), rand(0), thread_id(-1) {}
|
||||
ThreadPoolTempl* pool; // Parent pool, or null for normal threads.
|
||||
uint64_t rand; // Random generator state.
|
||||
int thread_id; // Worker thread index in pool.
|
||||
#ifndef EIGEN_THREAD_LOCAL
|
||||
// Prevent false sharing.
|
||||
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_; // Protects per_thread_map_.
|
||||
std::unordered_map<uint64_t, std::unique_ptr<PerThread>> per_thread_map_;
|
||||
#endif
|
||||
|
||||
// Main worker thread loop.
|
||||
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];
|
||||
// TODO(dvyukov,rmlarsen): The time spent in NonEmptyQueueIndex() 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 NonEmptyQueueIndex() 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_) {
|
||||
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) {
|
||||
// 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 = GlobalSteal();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
spinning_ = false;
|
||||
}
|
||||
if (!t.f) {
|
||||
if (!WaitForWork(waiter, &t)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (t.f) {
|
||||
env_.ExecuteTask(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Steal tries to steal work from other worker threads in the range [start,
|
||||
// limit) in best-effort manner.
|
||||
Task Steal(unsigned start, unsigned limit) {
|
||||
PerThread* pt = GetPerThread();
|
||||
const size_t size = limit - start;
|
||||
unsigned r = Rand(&pt->rand);
|
||||
// Reduce r into [0, size) range, this utilizes trick from
|
||||
// https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
|
||||
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();
|
||||
}
|
||||
|
||||
// Steals work within threads belonging to the partition.
|
||||
Task LocalSteal() {
|
||||
PerThread* pt = GetPerThread();
|
||||
unsigned partition = GetStealPartition(pt->thread_id);
|
||||
// If thread steal partition is the same as global partition, there is no
|
||||
// need to go through the steal loop twice.
|
||||
if (global_steal_partition_ == partition) return Task();
|
||||
unsigned start, limit;
|
||||
DecodePartition(partition, &start, &limit);
|
||||
AssertBounds(start, limit);
|
||||
|
||||
return Steal(start, limit);
|
||||
}
|
||||
|
||||
// Steals work from any other thread in the pool.
|
||||
Task GlobalSteal() {
|
||||
return Steal(0, num_threads_);
|
||||
}
|
||||
|
||||
|
||||
// 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_plain_assert(!t->f);
|
||||
// We already did best-effort emptiness check in Steal, so prepare for
|
||||
// blocking.
|
||||
ec_.Prewait();
|
||||
// Now do a reliable emptiness check.
|
||||
int victim = NonEmptyQueueIndex();
|
||||
if (victim != -1) {
|
||||
ec_.CancelWait();
|
||||
if (cancelled_) {
|
||||
return false;
|
||||
} else {
|
||||
*t = thread_data_[victim].queue.PopBack();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// 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_++;
|
||||
// TODO is blocked_ required to be unsigned?
|
||||
if (done_ && blocked_ == static_cast<unsigned>(num_threads_)) {
|
||||
ec_.CancelWait();
|
||||
// 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.
|
||||
if (NonEmptyQueueIndex() != -1) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
int NonEmptyQueueIndex() {
|
||||
PerThread* pt = GetPerThread();
|
||||
// We intentionally design NonEmptyQueueIndex to steal work from
|
||||
// anywhere in the queue so threads don't block in WaitForWork() forever
|
||||
// when all threads in their partition go to sleep. Steal is still local.
|
||||
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;
|
||||
// Update the internal state
|
||||
*state = current * 6364136223846793005ULL + 0xda3e39cb94b95bdbULL;
|
||||
// Generate the random output (using the PCG-XSH-RS scheme)
|
||||
return static_cast<unsigned>((current ^ (current >> 22)) >>
|
||||
(22 + (current >> 61)));
|
||||
}
|
||||
};
|
||||
|
||||
typedef ThreadPoolTempl<StlThreadEnvironment> ThreadPool;
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_NONBLOCKING_THREAD_POOL_H
|
||||
@@ -1,238 +0,0 @@
|
||||
// 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_RUNQUEUE_H
|
||||
#define EIGEN_CXX11_THREADPOOL_RUNQUEUE_H
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
// RunQueue is a fixed-size, partially non-blocking deque or Work items.
|
||||
// Operations on front of the queue must be done by a single thread (owner),
|
||||
// operations on back of the queue can be done by multiple threads concurrently.
|
||||
//
|
||||
// Algorithm outline:
|
||||
// All remote threads operating on the queue back are serialized by a mutex.
|
||||
// This ensures that at most two threads access state: owner and one remote
|
||||
// thread (Size aside). The algorithm ensures that the occupied region of the
|
||||
// underlying array is logically continuous (can wraparound, but no stray
|
||||
// occupied elements). Owner operates on one end of this region, remote thread
|
||||
// operates on the other end. Synchronization between these threads
|
||||
// (potential consumption of the last element and take up of the last empty
|
||||
// element) happens by means of state variable in each element. States are:
|
||||
// empty, busy (in process of insertion of removal) and ready. Threads claim
|
||||
// elements (empty->busy and ready->busy transitions) by means of a CAS
|
||||
// operation. The finishing transition (busy->empty and busy->ready) are done
|
||||
// with plain store as the element is exclusively owned by the current thread.
|
||||
//
|
||||
// Note: we could permit only pointers as elements, then we would not need
|
||||
// separate state variable as null/non-null pointer value would serve as state,
|
||||
// but that would require malloc/free per operation for large, complex values
|
||||
// (and this is designed to store std::function<()>).
|
||||
template <typename Work, unsigned kSize>
|
||||
class RunQueue {
|
||||
public:
|
||||
RunQueue() : front_(0), back_(0) {
|
||||
// require power-of-two for fast masking
|
||||
eigen_plain_assert((kSize & (kSize - 1)) == 0);
|
||||
eigen_plain_assert(kSize > 2); // why would you do this?
|
||||
eigen_plain_assert(kSize <= (64 << 10)); // leave enough space for counter
|
||||
for (unsigned i = 0; i < kSize; i++)
|
||||
array_[i].state.store(kEmpty, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
~RunQueue() { eigen_plain_assert(Size() == 0); }
|
||||
|
||||
// PushFront inserts w at the beginning of the queue.
|
||||
// If queue is full returns w, otherwise returns default-constructed Work.
|
||||
Work PushFront(Work w) {
|
||||
unsigned front = front_.load(std::memory_order_relaxed);
|
||||
Elem* e = &array_[front & kMask];
|
||||
uint8_t s = e->state.load(std::memory_order_relaxed);
|
||||
if (s != kEmpty ||
|
||||
!e->state.compare_exchange_strong(s, kBusy, std::memory_order_acquire))
|
||||
return w;
|
||||
front_.store(front + 1 + (kSize << 1), std::memory_order_relaxed);
|
||||
e->w = std::move(w);
|
||||
e->state.store(kReady, std::memory_order_release);
|
||||
return Work();
|
||||
}
|
||||
|
||||
// PopFront removes and returns the first element in the queue.
|
||||
// If the queue was empty returns default-constructed Work.
|
||||
Work PopFront() {
|
||||
unsigned front = front_.load(std::memory_order_relaxed);
|
||||
Elem* e = &array_[(front - 1) & kMask];
|
||||
uint8_t s = e->state.load(std::memory_order_relaxed);
|
||||
if (s != kReady ||
|
||||
!e->state.compare_exchange_strong(s, kBusy, std::memory_order_acquire))
|
||||
return Work();
|
||||
Work w = std::move(e->w);
|
||||
e->state.store(kEmpty, std::memory_order_release);
|
||||
front = ((front - 1) & kMask2) | (front & ~kMask2);
|
||||
front_.store(front, std::memory_order_relaxed);
|
||||
return w;
|
||||
}
|
||||
|
||||
// PushBack adds w at the end of the queue.
|
||||
// If queue is full returns w, otherwise returns default-constructed Work.
|
||||
Work PushBack(Work w) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
unsigned back = back_.load(std::memory_order_relaxed);
|
||||
Elem* e = &array_[(back - 1) & kMask];
|
||||
uint8_t s = e->state.load(std::memory_order_relaxed);
|
||||
if (s != kEmpty ||
|
||||
!e->state.compare_exchange_strong(s, kBusy, std::memory_order_acquire))
|
||||
return w;
|
||||
back = ((back - 1) & kMask2) | (back & ~kMask2);
|
||||
back_.store(back, std::memory_order_relaxed);
|
||||
e->w = std::move(w);
|
||||
e->state.store(kReady, std::memory_order_release);
|
||||
return Work();
|
||||
}
|
||||
|
||||
// PopBack removes and returns the last elements in the queue.
|
||||
Work PopBack() {
|
||||
if (Empty()) return Work();
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
unsigned back = back_.load(std::memory_order_relaxed);
|
||||
Elem* e = &array_[back & kMask];
|
||||
uint8_t s = e->state.load(std::memory_order_relaxed);
|
||||
if (s != kReady ||
|
||||
!e->state.compare_exchange_strong(s, kBusy, std::memory_order_acquire))
|
||||
return Work();
|
||||
Work w = std::move(e->w);
|
||||
e->state.store(kEmpty, std::memory_order_release);
|
||||
back_.store(back + 1 + (kSize << 1), std::memory_order_relaxed);
|
||||
return w;
|
||||
}
|
||||
|
||||
// PopBackHalf removes and returns half last elements in the queue.
|
||||
// Returns number of elements removed.
|
||||
unsigned PopBackHalf(std::vector<Work>* result) {
|
||||
if (Empty()) return 0;
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
unsigned back = back_.load(std::memory_order_relaxed);
|
||||
unsigned size = Size();
|
||||
unsigned mid = back;
|
||||
if (size > 1) mid = back + (size - 1) / 2;
|
||||
unsigned n = 0;
|
||||
unsigned start = 0;
|
||||
for (; static_cast<int>(mid - back) >= 0; mid--) {
|
||||
Elem* e = &array_[mid & kMask];
|
||||
uint8_t s = e->state.load(std::memory_order_relaxed);
|
||||
if (n == 0) {
|
||||
if (s != kReady || !e->state.compare_exchange_strong(
|
||||
s, kBusy, std::memory_order_acquire))
|
||||
continue;
|
||||
start = mid;
|
||||
} else {
|
||||
// Note: no need to store temporal kBusy, we exclusively own these
|
||||
// elements.
|
||||
eigen_plain_assert(s == kReady);
|
||||
}
|
||||
result->push_back(std::move(e->w));
|
||||
e->state.store(kEmpty, std::memory_order_release);
|
||||
n++;
|
||||
}
|
||||
if (n != 0)
|
||||
back_.store(start + 1 + (kSize << 1), std::memory_order_relaxed);
|
||||
return n;
|
||||
}
|
||||
|
||||
// Size returns current queue size.
|
||||
// Can be called by any thread at any time.
|
||||
unsigned Size() const { return SizeOrNotEmpty<true>(); }
|
||||
|
||||
// Empty tests whether container is empty.
|
||||
// Can be called by any thread at any time.
|
||||
bool Empty() const { return SizeOrNotEmpty<false>() == 0; }
|
||||
|
||||
// Delete all the elements from the queue.
|
||||
void Flush() {
|
||||
while (!Empty()) {
|
||||
PopFront();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static const unsigned kMask = kSize - 1;
|
||||
static const unsigned kMask2 = (kSize << 1) - 1;
|
||||
struct Elem {
|
||||
std::atomic<uint8_t> state;
|
||||
Work w;
|
||||
};
|
||||
enum {
|
||||
kEmpty,
|
||||
kBusy,
|
||||
kReady,
|
||||
};
|
||||
std::mutex mutex_;
|
||||
// Low log(kSize) + 1 bits in front_ and back_ contain rolling index of
|
||||
// front/back, respectively. The remaining bits contain modification counters
|
||||
// that are incremented on Push operations. This allows us to (1) distinguish
|
||||
// between empty and full conditions (if we would use log(kSize) bits for
|
||||
// position, these conditions would be indistinguishable); (2) obtain
|
||||
// consistent snapshot of front_/back_ for Size operation using the
|
||||
// modification counters.
|
||||
std::atomic<unsigned> front_;
|
||||
std::atomic<unsigned> back_;
|
||||
Elem array_[kSize];
|
||||
|
||||
// SizeOrNotEmpty returns current queue size; if NeedSizeEstimate is false,
|
||||
// only whether the size is 0 is guaranteed to be correct.
|
||||
// Can be called by any thread at any time.
|
||||
template<bool NeedSizeEstimate>
|
||||
unsigned SizeOrNotEmpty() const {
|
||||
// Emptiness plays critical role in thread pool blocking. So we go to great
|
||||
// effort to not produce false positives (claim non-empty queue as empty).
|
||||
unsigned front = front_.load(std::memory_order_acquire);
|
||||
for (;;) {
|
||||
// Capture a consistent snapshot of front/tail.
|
||||
unsigned back = back_.load(std::memory_order_acquire);
|
||||
unsigned front1 = front_.load(std::memory_order_relaxed);
|
||||
if (front != front1) {
|
||||
front = front1;
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
continue;
|
||||
}
|
||||
if (NeedSizeEstimate) {
|
||||
return CalculateSize(front, back);
|
||||
} else {
|
||||
// This value will be 0 if the queue is empty, and undefined otherwise.
|
||||
unsigned maybe_zero = ((front ^ back) & kMask2);
|
||||
// Queue size estimate must agree with maybe zero check on the queue
|
||||
// empty/non-empty state.
|
||||
eigen_assert((CalculateSize(front, back) == 0) == (maybe_zero == 0));
|
||||
return maybe_zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EIGEN_ALWAYS_INLINE
|
||||
unsigned CalculateSize(unsigned front, unsigned back) const {
|
||||
int size = (front & kMask2) - (back & kMask2);
|
||||
// Fix overflow.
|
||||
if (size < 0) size += 2 * kSize;
|
||||
// Order of modification in push/pop is crafted to make the queue look
|
||||
// larger than it is during concurrent modifications. E.g. push can
|
||||
// increment size before the corresponding pop has decremented it.
|
||||
// So the computed size can be up to kSize + 1, fix it.
|
||||
if (size > static_cast<int>(kSize)) size = kSize;
|
||||
return static_cast<unsigned>(size);
|
||||
}
|
||||
|
||||
RunQueue(const RunQueue&) = delete;
|
||||
void operator=(const RunQueue&) = delete;
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_RUNQUEUE_H
|
||||
@@ -1,23 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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_THREAD_CANCEL_H
|
||||
#define EIGEN_CXX11_THREADPOOL_THREAD_CANCEL_H
|
||||
|
||||
// Try to come up with a portable way to cancel a thread
|
||||
#if EIGEN_OS_GNULINUX
|
||||
#define EIGEN_THREAD_CANCEL(t) \
|
||||
pthread_cancel(t.native_handle());
|
||||
#define EIGEN_SUPPORTS_THREAD_CANCELLATION 1
|
||||
#else
|
||||
#define EIGEN_THREAD_CANCEL(t)
|
||||
#endif
|
||||
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_THREAD_CANCEL_H
|
||||
@@ -1,42 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.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_THREAD_ENVIRONMENT_H
|
||||
#define EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
struct StlThreadEnvironment {
|
||||
struct Task {
|
||||
std::function<void()> f;
|
||||
};
|
||||
|
||||
// EnvThread constructor must start the thread,
|
||||
// destructor must join the thread.
|
||||
class EnvThread {
|
||||
public:
|
||||
EnvThread(std::function<void()> f) : thr_(std::move(f)) {}
|
||||
~EnvThread() { thr_.join(); }
|
||||
// This function is called when the threadpool is cancelled.
|
||||
void OnCancel() { }
|
||||
|
||||
private:
|
||||
std::thread thr_;
|
||||
};
|
||||
|
||||
EnvThread* CreateThread(std::function<void()> f) { return new EnvThread(std::move(f)); }
|
||||
Task CreateTask(std::function<void()> f) { return Task{std::move(f)}; }
|
||||
void ExecuteTask(const Task& t) { t.f(); }
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_THREAD_ENVIRONMENT_H
|
||||
@@ -1,299 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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_THREAD_LOCAL_H
|
||||
#define EIGEN_CXX11_THREADPOOL_THREAD_LOCAL_H
|
||||
|
||||
#ifdef EIGEN_AVOID_THREAD_LOCAL
|
||||
|
||||
#ifdef EIGEN_THREAD_LOCAL
|
||||
#undef EIGEN_THREAD_LOCAL
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC )
|
||||
#define EIGEN_THREAD_LOCAL static thread_local
|
||||
#endif
|
||||
|
||||
// Disable TLS for Apple and Android builds with older toolchains.
|
||||
#if defined(__APPLE__)
|
||||
// Included for TARGET_OS_IPHONE, __IPHONE_OS_VERSION_MIN_REQUIRED,
|
||||
// __IPHONE_8_0.
|
||||
#include <Availability.h>
|
||||
#include <TargetConditionals.h>
|
||||
#endif
|
||||
// Checks whether C++11's `thread_local` storage duration specifier is
|
||||
// supported.
|
||||
#if EIGEN_COMP_CLANGAPPLE && ((EIGEN_COMP_CLANGAPPLE < 8000042) || \
|
||||
(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0))
|
||||
// Notes: Xcode's clang did not support `thread_local` until version
|
||||
// 8, and even then not for all iOS < 9.0.
|
||||
#undef EIGEN_THREAD_LOCAL
|
||||
|
||||
#elif defined(__ANDROID__) && EIGEN_COMP_CLANG
|
||||
// There are platforms for which TLS should not be used even though the compiler
|
||||
// makes it seem like it's supported (Android NDK < r12b for example).
|
||||
// This is primarily because of linker problems and toolchain misconfiguration:
|
||||
// TLS isn't supported until NDK r12b per
|
||||
// https://developer.android.com/ndk/downloads/revision_history.html
|
||||
// Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
|
||||
// <android/ndk-version.h>. For NDK < r16, users should define these macros,
|
||||
// e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11.
|
||||
#if __has_include(<android/ndk-version.h>)
|
||||
#include <android/ndk-version.h>
|
||||
#endif // __has_include(<android/ndk-version.h>)
|
||||
#if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && \
|
||||
defined(__NDK_MINOR__) && \
|
||||
((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
|
||||
#undef EIGEN_THREAD_LOCAL
|
||||
#endif
|
||||
#endif // defined(__ANDROID__) && defined(__clang__)
|
||||
|
||||
#endif // EIGEN_AVOID_THREAD_LOCAL
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
namespace internal {
|
||||
template <typename T>
|
||||
struct ThreadLocalNoOpInitialize {
|
||||
void operator()(T&) const {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct ThreadLocalNoOpRelease {
|
||||
void operator()(T&) const {}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Thread local container for elements of type T, that does not use thread local
|
||||
// storage. As long as the number of unique threads accessing this storage
|
||||
// is smaller than `capacity_`, it is lock-free and wait-free. Otherwise it will
|
||||
// use a mutex for synchronization.
|
||||
//
|
||||
// Type `T` has to be default constructible, and by default each thread will get
|
||||
// a default constructed value. It is possible to specify custom `initialize`
|
||||
// callable, that will be called lazily from each thread accessing this object,
|
||||
// and will be passed a default initialized object of type `T`. Also it's
|
||||
// possible to pass a custom `release` callable, that will be invoked before
|
||||
// calling ~T().
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// struct Counter {
|
||||
// int value = 0;
|
||||
// }
|
||||
//
|
||||
// Eigen::ThreadLocal<Counter> counter(10);
|
||||
//
|
||||
// // Each thread will have access to it's own counter object.
|
||||
// Counter& cnt = counter.local();
|
||||
// cnt++;
|
||||
//
|
||||
// WARNING: Eigen::ThreadLocal uses the OS-specific value returned by
|
||||
// std::this_thread::get_id() to identify threads. This value is not guaranteed
|
||||
// to be unique except for the life of the thread. A newly created thread may
|
||||
// get an OS-specific ID equal to that of an already destroyed thread.
|
||||
//
|
||||
// Somewhat similar to TBB thread local storage, with similar restrictions:
|
||||
// https://www.threadingbuildingblocks.org/docs/help/reference/thread_local_storage/enumerable_thread_specific_cls.html
|
||||
//
|
||||
template <typename T,
|
||||
typename Initialize = internal::ThreadLocalNoOpInitialize<T>,
|
||||
typename Release = internal::ThreadLocalNoOpRelease<T>>
|
||||
class ThreadLocal {
|
||||
// We preallocate default constructed elements in MaxSizedVector.
|
||||
static_assert(std::is_default_constructible<T>::value,
|
||||
"ThreadLocal data type must be default constructible");
|
||||
|
||||
public:
|
||||
explicit ThreadLocal(int capacity)
|
||||
: ThreadLocal(capacity, internal::ThreadLocalNoOpInitialize<T>(),
|
||||
internal::ThreadLocalNoOpRelease<T>()) {}
|
||||
|
||||
ThreadLocal(int capacity, Initialize initialize)
|
||||
: ThreadLocal(capacity, std::move(initialize),
|
||||
internal::ThreadLocalNoOpRelease<T>()) {}
|
||||
|
||||
ThreadLocal(int capacity, Initialize initialize, Release release)
|
||||
: initialize_(std::move(initialize)),
|
||||
release_(std::move(release)),
|
||||
capacity_(capacity),
|
||||
data_(capacity_),
|
||||
ptr_(capacity_),
|
||||
filled_records_(0) {
|
||||
eigen_assert(capacity_ >= 0);
|
||||
data_.resize(capacity_);
|
||||
for (int i = 0; i < capacity_; ++i) {
|
||||
ptr_.emplace_back(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
T& local() {
|
||||
std::thread::id this_thread = std::this_thread::get_id();
|
||||
if (capacity_ == 0) return SpilledLocal(this_thread);
|
||||
|
||||
std::size_t h = std::hash<std::thread::id>()(this_thread);
|
||||
const int start_idx = h % capacity_;
|
||||
|
||||
// NOTE: From the definition of `std::this_thread::get_id()` it is
|
||||
// guaranteed that we never can have concurrent insertions with the same key
|
||||
// to our hash-map like data structure. If we didn't find an element during
|
||||
// the initial traversal, it's guaranteed that no one else could have
|
||||
// inserted it while we are in this function. This allows to massively
|
||||
// simplify out lock-free insert-only hash map.
|
||||
|
||||
// Check if we already have an element for `this_thread`.
|
||||
int idx = start_idx;
|
||||
while (ptr_[idx].load() != nullptr) {
|
||||
ThreadIdAndValue& record = *(ptr_[idx].load());
|
||||
if (record.thread_id == this_thread) return record.value;
|
||||
|
||||
idx += 1;
|
||||
if (idx >= capacity_) idx -= capacity_;
|
||||
if (idx == start_idx) break;
|
||||
}
|
||||
|
||||
// If we are here, it means that we found an insertion point in lookup
|
||||
// table at `idx`, or we did a full traversal and table is full.
|
||||
|
||||
// If lock-free storage is full, fallback on mutex.
|
||||
if (filled_records_.load() >= capacity_) return SpilledLocal(this_thread);
|
||||
|
||||
// We double check that we still have space to insert an element into a lock
|
||||
// free storage. If old value in `filled_records_` is larger than the
|
||||
// records capacity, it means that some other thread added an element while
|
||||
// we were traversing lookup table.
|
||||
int insertion_index =
|
||||
filled_records_.fetch_add(1, std::memory_order_relaxed);
|
||||
if (insertion_index >= capacity_) return SpilledLocal(this_thread);
|
||||
|
||||
// At this point it's guaranteed that we can access to
|
||||
// data_[insertion_index_] without a data race.
|
||||
data_[insertion_index].thread_id = this_thread;
|
||||
initialize_(data_[insertion_index].value);
|
||||
|
||||
// That's the pointer we'll put into the lookup table.
|
||||
ThreadIdAndValue* inserted = &data_[insertion_index];
|
||||
|
||||
// We'll use nullptr pointer to ThreadIdAndValue in a compare-and-swap loop.
|
||||
ThreadIdAndValue* empty = nullptr;
|
||||
|
||||
// Now we have to find an insertion point into the lookup table. We start
|
||||
// from the `idx` that was identified as an insertion point above, it's
|
||||
// guaranteed that we will have an empty record somewhere in a lookup table
|
||||
// (because we created a record in the `data_`).
|
||||
const int insertion_idx = idx;
|
||||
|
||||
do {
|
||||
// Always start search from the original insertion candidate.
|
||||
idx = insertion_idx;
|
||||
while (ptr_[idx].load() != nullptr) {
|
||||
idx += 1;
|
||||
if (idx >= capacity_) idx -= capacity_;
|
||||
// If we did a full loop, it means that we don't have any free entries
|
||||
// in the lookup table, and this means that something is terribly wrong.
|
||||
eigen_assert(idx != insertion_idx);
|
||||
}
|
||||
// Atomic CAS of the pointer guarantees that any other thread, that will
|
||||
// follow this pointer will see all the mutations in the `data_`.
|
||||
} while (!ptr_[idx].compare_exchange_weak(empty, inserted));
|
||||
|
||||
return inserted->value;
|
||||
}
|
||||
|
||||
// WARN: It's not thread safe to call it concurrently with `local()`.
|
||||
void ForEach(std::function<void(std::thread::id, T&)> f) {
|
||||
// Reading directly from `data_` is unsafe, because only CAS to the
|
||||
// record in `ptr_` makes all changes visible to other threads.
|
||||
for (auto& ptr : ptr_) {
|
||||
ThreadIdAndValue* record = ptr.load();
|
||||
if (record == nullptr) continue;
|
||||
f(record->thread_id, record->value);
|
||||
}
|
||||
|
||||
// We did not spill into the map based storage.
|
||||
if (filled_records_.load(std::memory_order_relaxed) < capacity_) return;
|
||||
|
||||
// Adds a happens before edge from the last call to SpilledLocal().
|
||||
std::unique_lock<std::mutex> lock(mu_);
|
||||
for (auto& kv : per_thread_map_) {
|
||||
f(kv.first, kv.second);
|
||||
}
|
||||
}
|
||||
|
||||
// WARN: It's not thread safe to call it concurrently with `local()`.
|
||||
~ThreadLocal() {
|
||||
// Reading directly from `data_` is unsafe, because only CAS to the record
|
||||
// in `ptr_` makes all changes visible to other threads.
|
||||
for (auto& ptr : ptr_) {
|
||||
ThreadIdAndValue* record = ptr.load();
|
||||
if (record == nullptr) continue;
|
||||
release_(record->value);
|
||||
}
|
||||
|
||||
// We did not spill into the map based storage.
|
||||
if (filled_records_.load(std::memory_order_relaxed) < capacity_) return;
|
||||
|
||||
// Adds a happens before edge from the last call to SpilledLocal().
|
||||
std::unique_lock<std::mutex> lock(mu_);
|
||||
for (auto& kv : per_thread_map_) {
|
||||
release_(kv.second);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
struct ThreadIdAndValue {
|
||||
std::thread::id thread_id;
|
||||
T value;
|
||||
};
|
||||
|
||||
// Use unordered map guarded by a mutex when lock free storage is full.
|
||||
T& SpilledLocal(std::thread::id this_thread) {
|
||||
std::unique_lock<std::mutex> lock(mu_);
|
||||
|
||||
auto it = per_thread_map_.find(this_thread);
|
||||
if (it == per_thread_map_.end()) {
|
||||
auto result = per_thread_map_.emplace(this_thread, T());
|
||||
eigen_assert(result.second);
|
||||
initialize_((*result.first).second);
|
||||
return (*result.first).second;
|
||||
} else {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
Initialize initialize_;
|
||||
Release release_;
|
||||
const int capacity_;
|
||||
|
||||
// Storage that backs lock-free lookup table `ptr_`. Records stored in this
|
||||
// storage contiguously starting from index 0.
|
||||
MaxSizeVector<ThreadIdAndValue> data_;
|
||||
|
||||
// Atomic pointers to the data stored in `data_`. Used as a lookup table for
|
||||
// linear probing hash map (https://en.wikipedia.org/wiki/Linear_probing).
|
||||
MaxSizeVector<std::atomic<ThreadIdAndValue*>> ptr_;
|
||||
|
||||
// Number of records stored in the `data_`.
|
||||
std::atomic<int> filled_records_;
|
||||
|
||||
// We fallback on per thread map if lock-free storage is full. In practice
|
||||
// this should never happen, if `capacity_` is a reasonable estimate of the
|
||||
// number of threads running in a system.
|
||||
std::mutex mu_; // Protects per_thread_map_.
|
||||
std::unordered_map<std::thread::id, T> per_thread_map_;
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_THREAD_LOCAL_H
|
||||
@@ -1,50 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.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_THREAD_POOL_INTERFACE_H
|
||||
#define EIGEN_CXX11_THREADPOOL_THREAD_POOL_INTERFACE_H
|
||||
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
// This defines an interface that ThreadPoolDevice can take to use
|
||||
// custom thread pools underneath.
|
||||
class ThreadPoolInterface {
|
||||
public:
|
||||
// Submits a closure to be run by a thread in the pool.
|
||||
virtual void Schedule(std::function<void()> fn) = 0;
|
||||
|
||||
// Submits a closure to be run by threads in the range [start, end) in the
|
||||
// pool.
|
||||
virtual void ScheduleWithHint(std::function<void()> fn, int /*start*/,
|
||||
int /*end*/) {
|
||||
// Just defer to Schedule in case sub-classes aren't interested in
|
||||
// overriding this functionality.
|
||||
Schedule(fn);
|
||||
}
|
||||
|
||||
// If implemented, stop processing the closures that have been enqueued.
|
||||
// Currently running closures may still be processed.
|
||||
// If not implemented, does nothing.
|
||||
virtual void Cancel() {}
|
||||
|
||||
// Returns the number of threads in the pool.
|
||||
virtual int NumThreads() const = 0;
|
||||
|
||||
// Returns a logical thread index between 0 and NumThreads() - 1 if called
|
||||
// from one of the threads in the pool. Returns -1 otherwise.
|
||||
virtual int CurrentThreadId() const = 0;
|
||||
|
||||
virtual ~ThreadPoolInterface() {}
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_THREAD_POOL_INTERFACE_H
|
||||
@@ -1,16 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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_THREAD_YIELD_H
|
||||
#define EIGEN_CXX11_THREADPOOL_THREAD_YIELD_H
|
||||
|
||||
// Try to come up with a portable way to yield
|
||||
#define EIGEN_THREAD_YIELD() std::this_thread::yield()
|
||||
|
||||
#endif // EIGEN_CXX11_THREADPOOL_THREAD_YIELD_H
|
||||
@@ -1,286 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.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_EMULATE_ARRAY_H
|
||||
#define EIGEN_EMULATE_ARRAY_H
|
||||
|
||||
// CUDA doesn't support the STL containers, so we use our own instead.
|
||||
#if defined(EIGEN_GPUCC) || defined(EIGEN_AVOID_STL_ARRAY)
|
||||
|
||||
namespace Eigen {
|
||||
template <typename T, size_t n> class array {
|
||||
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE iterator begin() { return values; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const_iterator begin() const { return values; }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE iterator end() { return values + n; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const_iterator end() const { return values + n; }
|
||||
|
||||
|
||||
#if !defined(EIGEN_GPUCC)
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE reverse_iterator rbegin() { return reverse_iterator(end());}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE reverse_iterator rend() { return reverse_iterator(begin()); }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
|
||||
#endif
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& operator[] (size_t index) { eigen_internal_assert(index < size()); return values[index]; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& operator[] (size_t index) const { eigen_internal_assert(index < size()); return values[index]; }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& at(size_t index) { eigen_assert(index < size()); return values[index]; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& at(size_t index) const { eigen_assert(index < size()); return values[index]; }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& front() { return values[0]; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& front() const { return values[0]; }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& back() { return values[n-1]; }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& back() const { return values[n-1]; }
|
||||
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
|
||||
static std::size_t size() { return n; }
|
||||
|
||||
T values[n];
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array() { }
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v) {
|
||||
EIGEN_STATIC_ASSERT(n==1, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2) {
|
||||
EIGEN_STATIC_ASSERT(n==2, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3) {
|
||||
EIGEN_STATIC_ASSERT(n==3, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3,
|
||||
const T& v4) {
|
||||
EIGEN_STATIC_ASSERT(n==4, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
values[3] = v4;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
|
||||
const T& v5) {
|
||||
EIGEN_STATIC_ASSERT(n==5, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
values[3] = v4;
|
||||
values[4] = v5;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
|
||||
const T& v5, const T& v6) {
|
||||
EIGEN_STATIC_ASSERT(n==6, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
values[3] = v4;
|
||||
values[4] = v5;
|
||||
values[5] = v6;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(const T& v1, const T& v2, const T& v3, const T& v4,
|
||||
const T& v5, const T& v6, const T& v7) {
|
||||
EIGEN_STATIC_ASSERT(n==7, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
values[3] = v4;
|
||||
values[4] = v5;
|
||||
values[5] = v6;
|
||||
values[6] = v7;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(
|
||||
const T& v1, const T& v2, const T& v3, const T& v4,
|
||||
const T& v5, const T& v6, const T& v7, const T& v8) {
|
||||
EIGEN_STATIC_ASSERT(n==8, YOU_MADE_A_PROGRAMMING_MISTAKE)
|
||||
values[0] = v1;
|
||||
values[1] = v2;
|
||||
values[2] = v3;
|
||||
values[3] = v4;
|
||||
values[4] = v5;
|
||||
values[5] = v6;
|
||||
values[6] = v7;
|
||||
values[7] = v8;
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array(std::initializer_list<T> l) {
|
||||
eigen_assert(l.size() == n);
|
||||
internal::smart_copy(l.begin(), l.end(), values);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Specialize array for zero size
|
||||
template <typename T> class array<T, 0> {
|
||||
public:
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& operator[] (size_t) {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& operator[] (size_t) const {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& front() {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& front() const {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE T& back() {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE const T& back() const {
|
||||
eigen_assert(false && "Can't index a zero size array");
|
||||
return dummy;
|
||||
}
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE std::size_t size() { return 0; }
|
||||
|
||||
EIGEN_DEVICE_FUNC
|
||||
EIGEN_STRONG_INLINE array() : dummy() { }
|
||||
|
||||
EIGEN_DEVICE_FUNC array(std::initializer_list<T> l) : dummy() {
|
||||
EIGEN_UNUSED_VARIABLE(l);
|
||||
eigen_assert(l.size() == 0);
|
||||
}
|
||||
|
||||
private:
|
||||
T dummy;
|
||||
};
|
||||
|
||||
// Comparison operator
|
||||
// Todo: implement !=, <, <=, >, and >=
|
||||
template<class T, std::size_t N>
|
||||
EIGEN_DEVICE_FUNC bool operator==(const array<T,N>& lhs, const array<T,N>& rhs) {
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
if (lhs[i] != rhs[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
namespace internal {
|
||||
template<std::size_t I_, class T, std::size_t N>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T& array_get(array<T,N>& a) {
|
||||
return a[I_];
|
||||
}
|
||||
template<std::size_t I_, class T, std::size_t N>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T& array_get(const array<T,N>& a) {
|
||||
return a[I_];
|
||||
}
|
||||
|
||||
template<class T, std::size_t N> struct array_size<array<T,N> > {
|
||||
enum { value = N };
|
||||
};
|
||||
template<class T, std::size_t N> struct array_size<array<T,N>& > {
|
||||
enum { value = N };
|
||||
};
|
||||
template<class T, std::size_t N> struct array_size<const array<T,N> > {
|
||||
enum { value = N };
|
||||
};
|
||||
template<class T, std::size_t N> struct array_size<const array<T,N>& > {
|
||||
enum { value = N };
|
||||
};
|
||||
|
||||
} // end namespace internal
|
||||
} // end namespace Eigen
|
||||
|
||||
#else
|
||||
|
||||
// The compiler supports c++11, and we're not targeting cuda: use std::array as Eigen::array
|
||||
#include <array>
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
template <typename T, std::size_t N> using array = std::array<T, N>;
|
||||
|
||||
namespace internal {
|
||||
/* std::get is only constexpr in C++14, not yet in C++11
|
||||
* - libstdc++ from version 4.7 onwards has it nevertheless,
|
||||
* so use that
|
||||
* - libstdc++ older versions: use _M_instance directly
|
||||
* - libc++ all versions so far: use __elems_ directly
|
||||
* - all other libs: use std::get to be portable, but
|
||||
* this may not be constexpr
|
||||
*/
|
||||
#if defined(__GLIBCXX__) && __GLIBCXX__ < 20120322
|
||||
#define STD_GET_ARR_HACK a._M_instance[I_]
|
||||
#elif defined(_LIBCPP_VERSION)
|
||||
#define STD_GET_ARR_HACK a.__elems_[I_]
|
||||
#else
|
||||
#define STD_GET_ARR_HACK std::template get<I_, T, N>(a)
|
||||
#endif
|
||||
|
||||
template<std::size_t I_, class T, std::size_t N> constexpr inline T& array_get(std::array<T,N>& a) { return (T&) STD_GET_ARR_HACK; }
|
||||
template<std::size_t I_, class T, std::size_t N> constexpr inline T&& array_get(std::array<T,N>&& a) { return (T&&) STD_GET_ARR_HACK; }
|
||||
template<std::size_t I_, class T, std::size_t N> constexpr inline T const& array_get(std::array<T,N> const& a) { return (T const&) STD_GET_ARR_HACK; }
|
||||
|
||||
#undef STD_GET_ARR_HACK
|
||||
|
||||
} // end namespace internal
|
||||
} // end namespace Eigen
|
||||
|
||||
#endif
|
||||
|
||||
#endif // EIGEN_EMULATE_ARRAY_H
|
||||
@@ -1,158 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.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_FIXEDSIZEVECTOR_H
|
||||
#define EIGEN_FIXEDSIZEVECTOR_H
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
/** \class MaxSizeVector
|
||||
* \ingroup Core
|
||||
*
|
||||
* \brief The MaxSizeVector class.
|
||||
*
|
||||
* The %MaxSizeVector provides a subset of std::vector functionality.
|
||||
*
|
||||
* The goal is to provide basic std::vector operations when using
|
||||
* std::vector is not an option (e.g. on GPU or when compiling using
|
||||
* FMA/AVX, as this can cause either compilation failures or illegal
|
||||
* instruction failures).
|
||||
*
|
||||
* Beware: The constructors are not API compatible with these of
|
||||
* std::vector.
|
||||
*/
|
||||
template <typename T>
|
||||
class MaxSizeVector {
|
||||
static const size_t alignment = internal::plain_enum_max(EIGEN_ALIGNOF(T), sizeof(void*));
|
||||
public:
|
||||
// Construct a new MaxSizeVector, reserve n elements.
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
explicit MaxSizeVector(size_t n)
|
||||
: reserve_(n), size_(0),
|
||||
data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
|
||||
}
|
||||
|
||||
// Construct a new MaxSizeVector, reserve and resize to n.
|
||||
// Copy the init value to all elements.
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
MaxSizeVector(size_t n, const T& init)
|
||||
: reserve_(n), size_(n),
|
||||
data_(static_cast<T*>(internal::handmade_aligned_malloc(n * sizeof(T), alignment))) {
|
||||
size_t i = 0;
|
||||
EIGEN_TRY
|
||||
{
|
||||
for(; i < size_; ++i) { new (&data_[i]) T(init); }
|
||||
}
|
||||
EIGEN_CATCH(...)
|
||||
{
|
||||
// Construction failed, destruct in reverse order:
|
||||
for(; (i+1) > 0; --i) { data_[i-1].~T(); }
|
||||
internal::handmade_aligned_free(data_);
|
||||
EIGEN_THROW;
|
||||
}
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
~MaxSizeVector() {
|
||||
for (size_t i = size_; i > 0; --i) {
|
||||
data_[i-1].~T();
|
||||
}
|
||||
internal::handmade_aligned_free(data_);
|
||||
}
|
||||
|
||||
void resize(size_t n) {
|
||||
eigen_assert(n <= reserve_);
|
||||
for (; size_ < n; ++size_) {
|
||||
new (&data_[size_]) T;
|
||||
}
|
||||
for (; size_ > n; --size_) {
|
||||
data_[size_-1].~T();
|
||||
}
|
||||
eigen_assert(size_ == n);
|
||||
}
|
||||
|
||||
// Append new elements (up to reserved size).
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
void push_back(const T& t) {
|
||||
eigen_assert(size_ < reserve_);
|
||||
new (&data_[size_++]) T(t);
|
||||
}
|
||||
|
||||
// For C++03 compatibility this only takes one argument
|
||||
template<class X>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
void emplace_back(const X& x) {
|
||||
eigen_assert(size_ < reserve_);
|
||||
new (&data_[size_++]) T(x);
|
||||
}
|
||||
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const T& operator[] (size_t i) const {
|
||||
eigen_assert(i < size_);
|
||||
return data_[i];
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
T& operator[] (size_t i) {
|
||||
eigen_assert(i < size_);
|
||||
return data_[i];
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
T& back() {
|
||||
eigen_assert(size_ > 0);
|
||||
return data_[size_ - 1];
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const T& back() const {
|
||||
eigen_assert(size_ > 0);
|
||||
return data_[size_ - 1];
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
void pop_back() {
|
||||
eigen_assert(size_ > 0);
|
||||
data_[--size_].~T();
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
size_t size() const { return size_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
bool empty() const { return size_ == 0; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
T* data() { return data_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const T* data() const { return data_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
T* begin() { return data_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
T* end() { return data_ + size_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const T* begin() const { return data_; }
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const T* end() const { return data_ + size_; }
|
||||
|
||||
private:
|
||||
size_t reserve_;
|
||||
size_t size_;
|
||||
T* data_;
|
||||
};
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_FIXEDSIZEVECTOR_H
|
||||
@@ -223,12 +223,6 @@ if(EIGEN_TEST_SYCL)
|
||||
set(EIGEN_SYCL OFF)
|
||||
endif()
|
||||
|
||||
ei_add_test(cxx11_eventcount "-pthread" "${CMAKE_THREAD_LIBS_INIT}")
|
||||
ei_add_test(cxx11_runqueue "-pthread" "${CMAKE_THREAD_LIBS_INIT}")
|
||||
ei_add_test(cxx11_non_blocking_thread_pool "-pthread" "${CMAKE_THREAD_LIBS_INIT}")
|
||||
|
||||
ei_add_test(cxx11_meta)
|
||||
ei_add_test(cxx11_maxsizevector)
|
||||
ei_add_test(cxx11_tensor_argmax)
|
||||
ei_add_test(cxx11_tensor_assign)
|
||||
ei_add_test(cxx11_tensor_block_access)
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Dmitry Vyukov <dvyukov@google.com>
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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/.
|
||||
|
||||
#define EIGEN_USE_THREADS
|
||||
#include "main.h"
|
||||
#include <Eigen/CXX11/ThreadPool>
|
||||
|
||||
// Visual studio doesn't implement a rand_r() function since its
|
||||
// implementation of rand() is already thread safe
|
||||
int rand_reentrant(unsigned int* s) {
|
||||
#if EIGEN_COMP_MSVC_STRICT
|
||||
EIGEN_UNUSED_VARIABLE(s);
|
||||
return rand();
|
||||
#else
|
||||
return rand_r(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void test_basic_eventcount()
|
||||
{
|
||||
MaxSizeVector<EventCount::Waiter> waiters(1);
|
||||
waiters.resize(1);
|
||||
EventCount ec(waiters);
|
||||
EventCount::Waiter& w = waiters[0];
|
||||
ec.Notify(false);
|
||||
ec.Prewait();
|
||||
ec.Notify(true);
|
||||
ec.CommitWait(&w);
|
||||
ec.Prewait();
|
||||
ec.CancelWait();
|
||||
}
|
||||
|
||||
// Fake bounded counter-based queue.
|
||||
struct TestQueue {
|
||||
std::atomic<int> val_;
|
||||
static const int kQueueSize = 10;
|
||||
|
||||
TestQueue() : val_() {}
|
||||
|
||||
~TestQueue() { VERIFY_IS_EQUAL(val_.load(), 0); }
|
||||
|
||||
bool Push() {
|
||||
int val = val_.load(std::memory_order_relaxed);
|
||||
for (;;) {
|
||||
VERIFY_GE(val, 0);
|
||||
VERIFY_LE(val, kQueueSize);
|
||||
if (val == kQueueSize) return false;
|
||||
if (val_.compare_exchange_weak(val, val + 1, std::memory_order_relaxed))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Pop() {
|
||||
int val = val_.load(std::memory_order_relaxed);
|
||||
for (;;) {
|
||||
VERIFY_GE(val, 0);
|
||||
VERIFY_LE(val, kQueueSize);
|
||||
if (val == 0) return false;
|
||||
if (val_.compare_exchange_weak(val, val - 1, std::memory_order_relaxed))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Empty() { return val_.load(std::memory_order_relaxed) == 0; }
|
||||
};
|
||||
|
||||
const int TestQueue::kQueueSize;
|
||||
|
||||
// A number of producers send messages to a set of consumers using a set of
|
||||
// fake queues. Ensure that it does not crash, consumers don't deadlock and
|
||||
// number of blocked and unblocked threads match.
|
||||
static void test_stress_eventcount()
|
||||
{
|
||||
const int kThreads = std::thread::hardware_concurrency();
|
||||
static const int kEvents = 1 << 16;
|
||||
static const int kQueues = 10;
|
||||
|
||||
MaxSizeVector<EventCount::Waiter> waiters(kThreads);
|
||||
waiters.resize(kThreads);
|
||||
EventCount ec(waiters);
|
||||
TestQueue queues[kQueues];
|
||||
|
||||
std::vector<std::unique_ptr<std::thread>> producers;
|
||||
for (int i = 0; i < kThreads; i++) {
|
||||
producers.emplace_back(new std::thread([&ec, &queues]() {
|
||||
unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
||||
for (int j = 0; j < kEvents; j++) {
|
||||
unsigned idx = rand_reentrant(&rnd) % kQueues;
|
||||
if (queues[idx].Push()) {
|
||||
ec.Notify(false);
|
||||
continue;
|
||||
}
|
||||
EIGEN_THREAD_YIELD();
|
||||
j--;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<std::thread>> consumers;
|
||||
for (int i = 0; i < kThreads; i++) {
|
||||
consumers.emplace_back(new std::thread([&ec, &queues, &waiters, i]() {
|
||||
EventCount::Waiter& w = waiters[i];
|
||||
unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
|
||||
for (int j = 0; j < kEvents; j++) {
|
||||
unsigned idx = rand_reentrant(&rnd) % kQueues;
|
||||
if (queues[idx].Pop()) continue;
|
||||
j--;
|
||||
ec.Prewait();
|
||||
bool empty = true;
|
||||
for (int q = 0; q < kQueues; q++) {
|
||||
if (!queues[q].Empty()) {
|
||||
empty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty) {
|
||||
ec.CancelWait();
|
||||
continue;
|
||||
}
|
||||
ec.CommitWait(&w);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
for (int i = 0; i < kThreads; i++) {
|
||||
producers[i]->join();
|
||||
consumers[i]->join();
|
||||
}
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_eventcount)
|
||||
{
|
||||
CALL_SUBTEST(test_basic_eventcount());
|
||||
CALL_SUBTEST(test_stress_eventcount());
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#include "main.h"
|
||||
|
||||
#include <exception> // std::exception
|
||||
|
||||
#include <unsupported/Eigen/CXX11/Tensor>
|
||||
|
||||
struct Foo
|
||||
{
|
||||
static Index object_count;
|
||||
static Index object_limit;
|
||||
EIGEN_ALIGN_TO_BOUNDARY(128) int dummy;
|
||||
|
||||
Foo(int x=0) : dummy(x)
|
||||
{
|
||||
#ifdef EIGEN_EXCEPTIONS
|
||||
// TODO: Is this the correct way to handle this?
|
||||
if (Foo::object_count > Foo::object_limit) { std::cout << "\nThrow!\n"; throw Foo::Fail(); }
|
||||
#endif
|
||||
std::cout << '+';
|
||||
++Foo::object_count;
|
||||
eigen_assert((std::uintptr_t(this) & (127)) == 0);
|
||||
}
|
||||
Foo(const Foo&)
|
||||
{
|
||||
std::cout << 'c';
|
||||
++Foo::object_count;
|
||||
eigen_assert((std::uintptr_t(this) & (127)) == 0);
|
||||
}
|
||||
|
||||
~Foo()
|
||||
{
|
||||
std::cout << '~';
|
||||
--Foo::object_count;
|
||||
}
|
||||
|
||||
class Fail : public std::exception {};
|
||||
};
|
||||
|
||||
Index Foo::object_count = 0;
|
||||
Index Foo::object_limit = 0;
|
||||
|
||||
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_maxsizevector)
|
||||
{
|
||||
typedef MaxSizeVector<Foo> VectorX;
|
||||
Foo::object_count = 0;
|
||||
for(int r = 0; r < g_repeat; r++) {
|
||||
Index rows = internal::random<Index>(3,30);
|
||||
Foo::object_limit = internal::random<Index>(0, rows - 2);
|
||||
std::cout << "object_limit = " << Foo::object_limit << std::endl;
|
||||
bool exception_raised = false;
|
||||
#ifdef EIGEN_EXCEPTIONS
|
||||
try
|
||||
{
|
||||
#endif
|
||||
std::cout << "\nVectorX m(" << rows << ");\n";
|
||||
VectorX vect(rows);
|
||||
for(int i=0; i<rows; ++i)
|
||||
vect.push_back(Foo());
|
||||
#ifdef EIGEN_EXCEPTIONS
|
||||
VERIFY(false); // not reached if exceptions are enabled
|
||||
}
|
||||
catch (const Foo::Fail&) { exception_raised = true; }
|
||||
VERIFY(exception_raised);
|
||||
#endif
|
||||
VERIFY_IS_EQUAL(Index(0), Foo::object_count);
|
||||
|
||||
{
|
||||
Foo::object_limit = rows+1;
|
||||
VectorX vect2(rows, Foo());
|
||||
VERIFY_IS_EQUAL(Foo::object_count, rows);
|
||||
}
|
||||
VERIFY_IS_EQUAL(Index(0), Foo::object_count);
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
@@ -1,357 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
|
||||
//
|
||||
// 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/.
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#include <array>
|
||||
#include <Eigen/CXX11/src/util/CXX11Meta.h>
|
||||
|
||||
using Eigen::internal::is_same;
|
||||
using Eigen::internal::type_list;
|
||||
using Eigen::internal::numeric_list;
|
||||
using Eigen::internal::gen_numeric_list;
|
||||
using Eigen::internal::gen_numeric_list_reversed;
|
||||
using Eigen::internal::gen_numeric_list_swapped_pair;
|
||||
using Eigen::internal::gen_numeric_list_repeated;
|
||||
using Eigen::internal::concat;
|
||||
using Eigen::internal::mconcat;
|
||||
using Eigen::internal::take;
|
||||
using Eigen::internal::skip;
|
||||
using Eigen::internal::slice;
|
||||
using Eigen::internal::get;
|
||||
using Eigen::internal::id_numeric;
|
||||
using Eigen::internal::id_type;
|
||||
using Eigen::internal::is_same_gf;
|
||||
using Eigen::internal::apply_op_from_left;
|
||||
using Eigen::internal::apply_op_from_right;
|
||||
using Eigen::internal::contained_in_list;
|
||||
using Eigen::internal::contained_in_list_gf;
|
||||
using Eigen::internal::arg_prod;
|
||||
using Eigen::internal::arg_sum;
|
||||
using Eigen::internal::sum_op;
|
||||
using Eigen::internal::product_op;
|
||||
using Eigen::internal::array_reverse;
|
||||
using Eigen::internal::array_sum;
|
||||
using Eigen::internal::array_prod;
|
||||
using Eigen::internal::array_reduce;
|
||||
using Eigen::internal::array_zip;
|
||||
using Eigen::internal::array_zip_and_reduce;
|
||||
using Eigen::internal::array_apply;
|
||||
using Eigen::internal::array_apply_and_reduce;
|
||||
using Eigen::internal::repeat;
|
||||
using Eigen::internal::instantiate_by_c_array;
|
||||
|
||||
struct dummy_a {};
|
||||
struct dummy_b {};
|
||||
struct dummy_c {};
|
||||
struct dummy_d {};
|
||||
struct dummy_e {};
|
||||
|
||||
// dummy operation for testing apply
|
||||
template<typename A, typename B> struct dummy_op;
|
||||
template<> struct dummy_op<dummy_a, dummy_b> { typedef dummy_c type; };
|
||||
template<> struct dummy_op<dummy_b, dummy_a> { typedef dummy_d type; };
|
||||
template<> struct dummy_op<dummy_b, dummy_c> { typedef dummy_a type; };
|
||||
template<> struct dummy_op<dummy_c, dummy_b> { typedef dummy_d type; };
|
||||
template<> struct dummy_op<dummy_c, dummy_a> { typedef dummy_b type; };
|
||||
template<> struct dummy_op<dummy_a, dummy_c> { typedef dummy_d type; };
|
||||
template<> struct dummy_op<dummy_a, dummy_a> { typedef dummy_e type; };
|
||||
template<> struct dummy_op<dummy_b, dummy_b> { typedef dummy_e type; };
|
||||
template<> struct dummy_op<dummy_c, dummy_c> { typedef dummy_e type; };
|
||||
|
||||
template<typename A, typename B> struct dummy_test { constexpr static bool value = false; constexpr static int global_flags = 0; };
|
||||
template<> struct dummy_test<dummy_a, dummy_a> { constexpr static bool value = true; constexpr static int global_flags = 1; };
|
||||
template<> struct dummy_test<dummy_b, dummy_b> { constexpr static bool value = true; constexpr static int global_flags = 2; };
|
||||
template<> struct dummy_test<dummy_c, dummy_c> { constexpr static bool value = true; constexpr static int global_flags = 4; };
|
||||
|
||||
struct times2_op { template<typename A> static A run(A v) { return v * 2; } };
|
||||
|
||||
struct dummy_inst
|
||||
{
|
||||
int c;
|
||||
|
||||
dummy_inst() : c(0) {}
|
||||
explicit dummy_inst(int) : c(1) {}
|
||||
dummy_inst(int, int) : c(2) {}
|
||||
dummy_inst(int, int, int) : c(3) {}
|
||||
dummy_inst(int, int, int, int) : c(4) {}
|
||||
dummy_inst(int, int, int, int, int) : c(5) {}
|
||||
};
|
||||
|
||||
static void test_gen_numeric_list()
|
||||
{
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 0>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 1>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 2>::type, numeric_list<int, 0, 1>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 5>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 10>::type, numeric_list<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 0, 42>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 1, 42>::type, numeric_list<int, 42>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 2, 42>::type, numeric_list<int, 42, 43>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 5, 42>::type, numeric_list<int, 42, 43, 44, 45, 46>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list<int, 10, 42>::type, numeric_list<int, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 0>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 1>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 2>::type, numeric_list<int, 1, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 5>::type, numeric_list<int, 4, 3, 2, 1, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 10>::type, numeric_list<int, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 0, 42>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 1, 42>::type, numeric_list<int, 42>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 2, 42>::type, numeric_list<int, 43, 42>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 5, 42>::type, numeric_list<int, 46, 45, 44, 43, 42>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_reversed<int, 10, 42>::type, numeric_list<int, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 0, 2, 3>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 1, 2, 3>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 2, 3>::type, numeric_list<int, 0, 1>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4, 5, 6, 7, 8, 9>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 0, 44, 45, 42>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 1, 44, 45, 42>::type, numeric_list<int, 42>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 44, 45, 42>::type, numeric_list<int, 42, 43>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46, 47, 48, 49, 50, 51>>::value));
|
||||
|
||||
VERIFY((is_same<typename gen_numeric_list_repeated<int, 0, 0>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_repeated<int, 1, 0>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_repeated<int, 2, 0>::type, numeric_list<int, 0, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_repeated<int, 5, 0>::type, numeric_list<int, 0, 0, 0, 0, 0>>::value));
|
||||
VERIFY((is_same<typename gen_numeric_list_repeated<int, 10, 0>::type, numeric_list<int, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>::value));
|
||||
}
|
||||
|
||||
static void test_concat()
|
||||
{
|
||||
VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<>>::type, type_list<dummy_a, dummy_a>>::value));
|
||||
VERIFY((is_same<typename concat<type_list<>, type_list<dummy_a, dummy_a>>::type, type_list<dummy_a, dummy_a>>::value));
|
||||
VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<dummy_a, dummy_a>>::type, type_list<dummy_a, dummy_a, dummy_a, dummy_a>>::value));
|
||||
VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_c>>::value));
|
||||
VERIFY((is_same<typename concat<type_list<dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
|
||||
|
||||
VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int>>::type, numeric_list<int, 0, 0>>::value));
|
||||
VERIFY((is_same<typename concat<numeric_list<int>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0>>::value));
|
||||
VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0, 0, 0>>::value));
|
||||
VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 0, 1, 2>>::value));
|
||||
VERIFY((is_same<typename concat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
|
||||
|
||||
VERIFY((is_same<typename mconcat<type_list<dummy_a>>::type, type_list<dummy_a>>::value));
|
||||
VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b>>::type, type_list<dummy_a, dummy_b>>::value));
|
||||
VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b>, type_list<dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
|
||||
VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
|
||||
VERIFY((is_same<typename mconcat<type_list<dummy_a, dummy_b>, type_list<dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
|
||||
|
||||
VERIFY((is_same<typename mconcat<numeric_list<int, 0>>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>>::type, numeric_list<int, 0, 1>>::value));
|
||||
VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
|
||||
VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
|
||||
VERIFY((is_same<typename mconcat<numeric_list<int, 0, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
|
||||
}
|
||||
|
||||
static void test_slice()
|
||||
{
|
||||
typedef type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c> tl;
|
||||
typedef numeric_list<int, 0, 1, 2, 3, 4, 5> il;
|
||||
|
||||
VERIFY((is_same<typename take<0, tl>::type, type_list<>>::value));
|
||||
VERIFY((is_same<typename take<1, tl>::type, type_list<dummy_a>>::value));
|
||||
VERIFY((is_same<typename take<2, tl>::type, type_list<dummy_a, dummy_a>>::value));
|
||||
VERIFY((is_same<typename take<3, tl>::type, type_list<dummy_a, dummy_a, dummy_b>>::value));
|
||||
VERIFY((is_same<typename take<4, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b>>::value));
|
||||
VERIFY((is_same<typename take<5, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c>>::value));
|
||||
VERIFY((is_same<typename take<6, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));
|
||||
|
||||
VERIFY((is_same<typename take<0, il>::type, numeric_list<int>>::value));
|
||||
VERIFY((is_same<typename take<1, il>::type, numeric_list<int, 0>>::value));
|
||||
VERIFY((is_same<typename take<2, il>::type, numeric_list<int, 0, 1>>::value));
|
||||
VERIFY((is_same<typename take<3, il>::type, numeric_list<int, 0, 1, 2>>::value));
|
||||
VERIFY((is_same<typename take<4, il>::type, numeric_list<int, 0, 1, 2, 3>>::value));
|
||||
VERIFY((is_same<typename take<5, il>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
|
||||
VERIFY((is_same<typename take<6, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));
|
||||
|
||||
VERIFY((is_same<typename skip<0, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<1, tl>::type, type_list<dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<2, tl>::type, type_list<dummy_b, dummy_b, dummy_c, dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<3, tl>::type, type_list<dummy_b, dummy_c, dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<4, tl>::type, type_list<dummy_c, dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<5, tl>::type, type_list<dummy_c>>::value));
|
||||
VERIFY((is_same<typename skip<6, tl>::type, type_list<>>::value));
|
||||
|
||||
VERIFY((is_same<typename skip<0, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));
|
||||
VERIFY((is_same<typename skip<1, il>::type, numeric_list<int, 1, 2, 3, 4, 5>>::value));
|
||||
VERIFY((is_same<typename skip<2, il>::type, numeric_list<int, 2, 3, 4, 5>>::value));
|
||||
VERIFY((is_same<typename skip<3, il>::type, numeric_list<int, 3, 4, 5>>::value));
|
||||
VERIFY((is_same<typename skip<4, il>::type, numeric_list<int, 4, 5>>::value));
|
||||
VERIFY((is_same<typename skip<5, il>::type, numeric_list<int, 5>>::value));
|
||||
VERIFY((is_same<typename skip<6, il>::type, numeric_list<int>>::value));
|
||||
|
||||
VERIFY((is_same<typename slice<0, 3, tl>::type, typename take<3, tl>::type>::value));
|
||||
VERIFY((is_same<typename slice<0, 3, il>::type, typename take<3, il>::type>::value));
|
||||
VERIFY((is_same<typename slice<1, 3, tl>::type, type_list<dummy_a, dummy_b, dummy_b>>::value));
|
||||
VERIFY((is_same<typename slice<1, 3, il>::type, numeric_list<int, 1, 2, 3>>::value));
|
||||
}
|
||||
|
||||
static void test_get()
|
||||
{
|
||||
typedef type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c> tl;
|
||||
typedef numeric_list<int, 4, 8, 15, 16, 23, 42> il;
|
||||
|
||||
VERIFY((is_same<typename get<0, tl>::type, dummy_a>::value));
|
||||
VERIFY((is_same<typename get<1, tl>::type, dummy_a>::value));
|
||||
VERIFY((is_same<typename get<2, tl>::type, dummy_b>::value));
|
||||
VERIFY((is_same<typename get<3, tl>::type, dummy_b>::value));
|
||||
VERIFY((is_same<typename get<4, tl>::type, dummy_c>::value));
|
||||
VERIFY((is_same<typename get<5, tl>::type, dummy_c>::value));
|
||||
|
||||
VERIFY_IS_EQUAL(((int)get<0, il>::value), 4);
|
||||
VERIFY_IS_EQUAL(((int)get<1, il>::value), 8);
|
||||
VERIFY_IS_EQUAL(((int)get<2, il>::value), 15);
|
||||
VERIFY_IS_EQUAL(((int)get<3, il>::value), 16);
|
||||
VERIFY_IS_EQUAL(((int)get<4, il>::value), 23);
|
||||
VERIFY_IS_EQUAL(((int)get<5, il>::value), 42);
|
||||
}
|
||||
|
||||
static void test_id_helper(dummy_a a, dummy_a b, dummy_a c)
|
||||
{
|
||||
(void)a;
|
||||
(void)b;
|
||||
(void)c;
|
||||
}
|
||||
|
||||
template<int... ii>
|
||||
static void test_id_numeric()
|
||||
{
|
||||
test_id_helper(typename id_numeric<int, ii, dummy_a>::type()...);
|
||||
}
|
||||
|
||||
template<typename... tt>
|
||||
static void test_id_type()
|
||||
{
|
||||
test_id_helper(typename id_type<tt, dummy_a>::type()...);
|
||||
}
|
||||
|
||||
static void test_id()
|
||||
{
|
||||
// don't call VERIFY here, just assume it works if it compiles
|
||||
// (otherwise it will complain that it can't find the function)
|
||||
test_id_numeric<1, 4, 6>();
|
||||
test_id_type<dummy_a, dummy_b, dummy_c>();
|
||||
}
|
||||
|
||||
static void test_is_same_gf()
|
||||
{
|
||||
VERIFY((!is_same_gf<dummy_a, dummy_b>::value));
|
||||
VERIFY((!!is_same_gf<dummy_a, dummy_a>::value));
|
||||
VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_b>::global_flags), false);
|
||||
VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_a>::global_flags), false);
|
||||
}
|
||||
|
||||
static void test_apply_op()
|
||||
{
|
||||
typedef type_list<dummy_a, dummy_b, dummy_c> tl;
|
||||
VERIFY((!!is_same<typename apply_op_from_left<dummy_op, dummy_a, tl>::type, type_list<dummy_e, dummy_c, dummy_d>>::value));
|
||||
VERIFY((!!is_same<typename apply_op_from_right<dummy_op, dummy_a, tl>::type, type_list<dummy_e, dummy_d, dummy_b>>::value));
|
||||
}
|
||||
|
||||
static void test_contained_in_list()
|
||||
{
|
||||
typedef type_list<dummy_a, dummy_b, dummy_c> tl;
|
||||
|
||||
VERIFY((!!contained_in_list<is_same, dummy_a, tl>::value));
|
||||
VERIFY((!!contained_in_list<is_same, dummy_b, tl>::value));
|
||||
VERIFY((!!contained_in_list<is_same, dummy_c, tl>::value));
|
||||
VERIFY((!contained_in_list<is_same, dummy_d, tl>::value));
|
||||
VERIFY((!contained_in_list<is_same, dummy_e, tl>::value));
|
||||
|
||||
VERIFY((!!contained_in_list_gf<dummy_test, dummy_a, tl>::value));
|
||||
VERIFY((!!contained_in_list_gf<dummy_test, dummy_b, tl>::value));
|
||||
VERIFY((!!contained_in_list_gf<dummy_test, dummy_c, tl>::value));
|
||||
VERIFY((!contained_in_list_gf<dummy_test, dummy_d, tl>::value));
|
||||
VERIFY((!contained_in_list_gf<dummy_test, dummy_e, tl>::value));
|
||||
|
||||
VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_a, tl>::global_flags), 1);
|
||||
VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_b, tl>::global_flags), 2);
|
||||
VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_c, tl>::global_flags), 4);
|
||||
VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_d, tl>::global_flags), 0);
|
||||
VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_e, tl>::global_flags), 0);
|
||||
}
|
||||
|
||||
static void test_arg_reductions()
|
||||
{
|
||||
VERIFY_IS_EQUAL(arg_sum(1,2,3,4), 10);
|
||||
VERIFY_IS_EQUAL(arg_prod(1,2,3,4), 24);
|
||||
VERIFY_IS_APPROX(arg_sum(0.5, 2, 5), 7.5);
|
||||
VERIFY_IS_APPROX(arg_prod(0.5, 2, 5), 5.0);
|
||||
}
|
||||
|
||||
static void test_array_reverse_and_reduce()
|
||||
{
|
||||
array<int, 6> a{{4, 8, 15, 16, 23, 42}};
|
||||
array<int, 6> b{{42, 23, 16, 15, 8, 4}};
|
||||
|
||||
// there is no operator<< for std::array, so VERIFY_IS_EQUAL will
|
||||
// not compile
|
||||
VERIFY((array_reverse(a) == b));
|
||||
VERIFY((array_reverse(b) == a));
|
||||
VERIFY_IS_EQUAL((array_sum(a)), 108);
|
||||
VERIFY_IS_EQUAL((array_sum(b)), 108);
|
||||
VERIFY_IS_EQUAL((array_prod(a)), 7418880);
|
||||
VERIFY_IS_EQUAL((array_prod(b)), 7418880);
|
||||
}
|
||||
|
||||
static void test_array_zip_and_apply()
|
||||
{
|
||||
array<int, 6> a{{4, 8, 15, 16, 23, 42}};
|
||||
array<int, 6> b{{0, 1, 2, 3, 4, 5}};
|
||||
array<int, 6> c{{4, 9, 17, 19, 27, 47}};
|
||||
array<int, 6> d{{0, 8, 30, 48, 92, 210}};
|
||||
array<int, 6> e{{0, 2, 4, 6, 8, 10}};
|
||||
|
||||
VERIFY((array_zip<sum_op>(a, b) == c));
|
||||
VERIFY((array_zip<product_op>(a, b) == d));
|
||||
VERIFY((array_apply<times2_op>(b) == e));
|
||||
VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(a)), 216);
|
||||
VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(b)), 30);
|
||||
VERIFY_IS_EQUAL((array_zip_and_reduce<product_op, sum_op>(a, b)), 14755932);
|
||||
VERIFY_IS_EQUAL((array_zip_and_reduce<sum_op, product_op>(a, b)), 388);
|
||||
}
|
||||
|
||||
static void test_array_misc()
|
||||
{
|
||||
array<int, 3> a3{{1, 1, 1}};
|
||||
array<int, 6> a6{{2, 2, 2, 2, 2, 2}};
|
||||
VERIFY((repeat<3, int>(1) == a3));
|
||||
VERIFY((repeat<6, int>(2) == a6));
|
||||
|
||||
int data[5] = { 0, 1, 2, 3, 4 };
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 0>(data).c), 0);
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 1>(data).c), 1);
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 2>(data).c), 2);
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 3>(data).c), 3);
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 4>(data).c), 4);
|
||||
VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 5>(data).c), 5);
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_meta)
|
||||
{
|
||||
CALL_SUBTEST(test_gen_numeric_list());
|
||||
CALL_SUBTEST(test_concat());
|
||||
CALL_SUBTEST(test_slice());
|
||||
CALL_SUBTEST(test_get());
|
||||
CALL_SUBTEST(test_id());
|
||||
CALL_SUBTEST(test_is_same_gf());
|
||||
CALL_SUBTEST(test_apply_op());
|
||||
CALL_SUBTEST(test_contained_in_list());
|
||||
CALL_SUBTEST(test_arg_reductions());
|
||||
CALL_SUBTEST(test_array_reverse_and_reduce());
|
||||
CALL_SUBTEST(test_array_zip_and_apply());
|
||||
CALL_SUBTEST(test_array_misc());
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Dmitry Vyukov <dvyukov@google.com>
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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/.
|
||||
|
||||
#define EIGEN_USE_THREADS
|
||||
#include "main.h"
|
||||
#include "Eigen/CXX11/ThreadPool"
|
||||
#include "Eigen/CXX11/Tensor"
|
||||
|
||||
static void test_create_destroy_empty_pool()
|
||||
{
|
||||
// Just create and destroy the pool. This will wind up and tear down worker
|
||||
// threads. Ensure there are no issues in that logic.
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
ThreadPool tp(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void test_parallelism(bool allow_spinning)
|
||||
{
|
||||
// Test we never-ever fail to match available tasks with idle threads.
|
||||
const int kThreads = 16; // code below expects that this is a multiple of 4
|
||||
ThreadPool tp(kThreads, allow_spinning);
|
||||
VERIFY_IS_EQUAL(tp.NumThreads(), kThreads);
|
||||
VERIFY_IS_EQUAL(tp.CurrentThreadId(), -1);
|
||||
for (int iter = 0; iter < 100; ++iter) {
|
||||
std::atomic<int> running(0);
|
||||
std::atomic<int> done(0);
|
||||
std::atomic<int> phase(0);
|
||||
// Schedule kThreads tasks and ensure that they all are running.
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
tp.Schedule([&]() {
|
||||
const int thread_id = tp.CurrentThreadId();
|
||||
VERIFY_GE(thread_id, 0);
|
||||
VERIFY_LE(thread_id, kThreads - 1);
|
||||
running++;
|
||||
while (phase < 1) {
|
||||
}
|
||||
done++;
|
||||
});
|
||||
}
|
||||
while (running != kThreads) {
|
||||
}
|
||||
running = 0;
|
||||
phase = 1;
|
||||
// Now, while the previous tasks exit, schedule another kThreads tasks and
|
||||
// ensure that they are running.
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
tp.Schedule([&, i]() {
|
||||
running++;
|
||||
while (phase < 2) {
|
||||
}
|
||||
// When all tasks are running, half of tasks exit, quarter of tasks
|
||||
// continue running and quarter of tasks schedule another 2 tasks each.
|
||||
// Concurrently main thread schedules another quarter of tasks.
|
||||
// This gives us another kThreads tasks and we ensure that they all
|
||||
// are running.
|
||||
if (i < kThreads / 2) {
|
||||
} else if (i < 3 * kThreads / 4) {
|
||||
running++;
|
||||
while (phase < 3) {
|
||||
}
|
||||
done++;
|
||||
} else {
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
tp.Schedule([&]() {
|
||||
running++;
|
||||
while (phase < 3) {
|
||||
}
|
||||
done++;
|
||||
});
|
||||
}
|
||||
}
|
||||
done++;
|
||||
});
|
||||
}
|
||||
while (running != kThreads) {
|
||||
}
|
||||
running = 0;
|
||||
phase = 2;
|
||||
for (int i = 0; i < kThreads / 4; ++i) {
|
||||
tp.Schedule([&]() {
|
||||
running++;
|
||||
while (phase < 3) {
|
||||
}
|
||||
done++;
|
||||
});
|
||||
}
|
||||
while (running != kThreads) {
|
||||
}
|
||||
phase = 3;
|
||||
while (done != 3 * kThreads) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void test_cancel()
|
||||
{
|
||||
ThreadPool tp(2);
|
||||
|
||||
// Schedule a large number of closure that each sleeps for one second. This
|
||||
// will keep the thread pool busy for much longer than the default test timeout.
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
tp.Schedule([]() {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||
});
|
||||
}
|
||||
|
||||
// Cancel the processing of all the closures that are still pending.
|
||||
tp.Cancel();
|
||||
}
|
||||
|
||||
static void test_pool_partitions() {
|
||||
const int kThreads = 2;
|
||||
ThreadPool tp(kThreads);
|
||||
|
||||
// Assign each thread to its own partition, so that stealing other work only
|
||||
// occurs globally when a thread is idle.
|
||||
std::vector<std::pair<unsigned, unsigned>> steal_partitions(kThreads);
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
steal_partitions[i] = std::make_pair(i, i + 1);
|
||||
}
|
||||
tp.SetStealPartitions(steal_partitions);
|
||||
|
||||
std::atomic<int> running(0);
|
||||
std::atomic<int> done(0);
|
||||
std::atomic<int> phase(0);
|
||||
|
||||
// Schedule kThreads tasks and ensure that they all are running.
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
tp.Schedule([&]() {
|
||||
const int thread_id = tp.CurrentThreadId();
|
||||
VERIFY_GE(thread_id, 0);
|
||||
VERIFY_LE(thread_id, kThreads - 1);
|
||||
++running;
|
||||
while (phase < 1) {
|
||||
}
|
||||
++done;
|
||||
});
|
||||
}
|
||||
while (running != kThreads) {
|
||||
}
|
||||
// Schedule each closure to only run on thread 'i' and verify that it does.
|
||||
for (int i = 0; i < kThreads; ++i) {
|
||||
tp.ScheduleWithHint(
|
||||
[&, i]() {
|
||||
++running;
|
||||
const int thread_id = tp.CurrentThreadId();
|
||||
VERIFY_IS_EQUAL(thread_id, i);
|
||||
while (phase < 2) {
|
||||
}
|
||||
++done;
|
||||
},
|
||||
i, i + 1);
|
||||
}
|
||||
running = 0;
|
||||
phase = 1;
|
||||
while (running != kThreads) {
|
||||
}
|
||||
running = 0;
|
||||
phase = 2;
|
||||
}
|
||||
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_non_blocking_thread_pool)
|
||||
{
|
||||
CALL_SUBTEST(test_create_destroy_empty_pool());
|
||||
CALL_SUBTEST(test_parallelism(true));
|
||||
CALL_SUBTEST(test_parallelism(false));
|
||||
CALL_SUBTEST(test_cancel());
|
||||
CALL_SUBTEST(test_pool_partitions());
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Dmitry Vyukov <dvyukov@google.com>
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.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/.
|
||||
|
||||
#define EIGEN_USE_THREADS
|
||||
#include <cstdlib>
|
||||
#include "main.h"
|
||||
#include <Eigen/CXX11/ThreadPool>
|
||||
|
||||
|
||||
// Visual studio doesn't implement a rand_r() function since its
|
||||
// implementation of rand() is already thread safe
|
||||
int rand_reentrant(unsigned int* s) {
|
||||
#if EIGEN_COMP_MSVC_STRICT
|
||||
EIGEN_UNUSED_VARIABLE(s);
|
||||
return rand();
|
||||
#else
|
||||
return rand_r(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_basic_runqueue()
|
||||
{
|
||||
RunQueue<int, 4> q;
|
||||
// Check empty state.
|
||||
VERIFY(q.Empty());
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PopFront());
|
||||
std::vector<int> stolen;
|
||||
VERIFY_IS_EQUAL(0u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(0u, stolen.size());
|
||||
// Push one front, pop one front.
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(1));
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
VERIFY_IS_EQUAL(1, q.PopFront());
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
// Push front to overflow.
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(2));
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(3));
|
||||
VERIFY_IS_EQUAL(2u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(4));
|
||||
VERIFY_IS_EQUAL(3u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(5));
|
||||
VERIFY_IS_EQUAL(4u, q.Size());
|
||||
VERIFY_IS_EQUAL(6, q.PushFront(6));
|
||||
VERIFY_IS_EQUAL(4u, q.Size());
|
||||
VERIFY_IS_EQUAL(5, q.PopFront());
|
||||
VERIFY_IS_EQUAL(3u, q.Size());
|
||||
VERIFY_IS_EQUAL(4, q.PopFront());
|
||||
VERIFY_IS_EQUAL(2u, q.Size());
|
||||
VERIFY_IS_EQUAL(3, q.PopFront());
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
VERIFY_IS_EQUAL(2, q.PopFront());
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PopFront());
|
||||
// Push one back, pop one back.
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(7));
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
VERIFY_IS_EQUAL(1u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(1u, stolen.size());
|
||||
VERIFY_IS_EQUAL(7, stolen[0]);
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
stolen.clear();
|
||||
// Push back to overflow.
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(8));
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(9));
|
||||
VERIFY_IS_EQUAL(2u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(10));
|
||||
VERIFY_IS_EQUAL(3u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(11));
|
||||
VERIFY_IS_EQUAL(4u, q.Size());
|
||||
VERIFY_IS_EQUAL(12, q.PushBack(12));
|
||||
VERIFY_IS_EQUAL(4u, q.Size());
|
||||
// Pop back in halves.
|
||||
VERIFY_IS_EQUAL(2u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(2u, stolen.size());
|
||||
VERIFY_IS_EQUAL(10, stolen[0]);
|
||||
VERIFY_IS_EQUAL(11, stolen[1]);
|
||||
VERIFY_IS_EQUAL(2u, q.Size());
|
||||
stolen.clear();
|
||||
VERIFY_IS_EQUAL(1u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(1u, stolen.size());
|
||||
VERIFY_IS_EQUAL(9, stolen[0]);
|
||||
VERIFY_IS_EQUAL(1u, q.Size());
|
||||
stolen.clear();
|
||||
VERIFY_IS_EQUAL(1u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(1u, stolen.size());
|
||||
VERIFY_IS_EQUAL(8, stolen[0]);
|
||||
stolen.clear();
|
||||
VERIFY_IS_EQUAL(0u, q.PopBackHalf(&stolen));
|
||||
VERIFY_IS_EQUAL(0u, stolen.size());
|
||||
// Empty again.
|
||||
VERIFY(q.Empty());
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(1));
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(2));
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(3));
|
||||
VERIFY_IS_EQUAL(1, q.PopBack());
|
||||
VERIFY_IS_EQUAL(2, q.PopBack());
|
||||
VERIFY_IS_EQUAL(3, q.PopBack());
|
||||
VERIFY(q.Empty());
|
||||
VERIFY_IS_EQUAL(0u, q.Size());
|
||||
}
|
||||
|
||||
// Empty tests that the queue is not claimed to be empty when is is in fact not.
|
||||
// Emptiness property is crucial part of thread pool blocking scheme,
|
||||
// so we go to great effort to ensure this property. We create a queue with
|
||||
// 1 element and then push 1 element (either front or back at random) and pop
|
||||
// 1 element (either front or back at random). So queue always contains at least
|
||||
// 1 element, but otherwise changes chaotically. Another thread constantly tests
|
||||
// that the queue is not claimed to be empty.
|
||||
void test_empty_runqueue()
|
||||
{
|
||||
RunQueue<int, 4> q;
|
||||
q.PushFront(1);
|
||||
std::atomic<bool> done(false);
|
||||
std::thread mutator([&q, &done]() {
|
||||
unsigned rnd = 0;
|
||||
std::vector<int> stolen;
|
||||
for (int i = 0; i < 1 << 18; i++) {
|
||||
if (rand_reentrant(&rnd) % 2)
|
||||
VERIFY_IS_EQUAL(0, q.PushFront(1));
|
||||
else
|
||||
VERIFY_IS_EQUAL(0, q.PushBack(1));
|
||||
if (rand_reentrant(&rnd) % 2)
|
||||
VERIFY_IS_EQUAL(1, q.PopFront());
|
||||
else {
|
||||
for (;;) {
|
||||
if (q.PopBackHalf(&stolen) == 1) {
|
||||
stolen.clear();
|
||||
break;
|
||||
}
|
||||
VERIFY_IS_EQUAL(0u, stolen.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
});
|
||||
while (!done) {
|
||||
VERIFY(!q.Empty());
|
||||
int size = q.Size();
|
||||
VERIFY_GE(size, 1);
|
||||
VERIFY_LE(size, 2);
|
||||
}
|
||||
VERIFY_IS_EQUAL(1, q.PopFront());
|
||||
mutator.join();
|
||||
}
|
||||
|
||||
// Stress is a chaotic random test.
|
||||
// One thread (owner) calls PushFront/PopFront, other threads call PushBack/
|
||||
// PopBack. Ensure that we don't crash, deadlock, and all sanity checks pass.
|
||||
void test_stress_runqueue()
|
||||
{
|
||||
static const int kEvents = 1 << 18;
|
||||
RunQueue<int, 8> q;
|
||||
std::atomic<int> total(0);
|
||||
std::vector<std::unique_ptr<std::thread>> threads;
|
||||
threads.emplace_back(new std::thread([&q, &total]() {
|
||||
int sum = 0;
|
||||
int pushed = 1;
|
||||
int popped = 1;
|
||||
while (pushed < kEvents || popped < kEvents) {
|
||||
if (pushed < kEvents) {
|
||||
if (q.PushFront(pushed) == 0) {
|
||||
sum += pushed;
|
||||
pushed++;
|
||||
}
|
||||
}
|
||||
if (popped < kEvents) {
|
||||
int v = q.PopFront();
|
||||
if (v != 0) {
|
||||
sum -= v;
|
||||
popped++;
|
||||
}
|
||||
}
|
||||
}
|
||||
total += sum;
|
||||
}));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
threads.emplace_back(new std::thread([&q, &total]() {
|
||||
int sum = 0;
|
||||
for (int j = 1; j < kEvents; j++) {
|
||||
if (q.PushBack(j) == 0) {
|
||||
sum += j;
|
||||
continue;
|
||||
}
|
||||
EIGEN_THREAD_YIELD();
|
||||
j--;
|
||||
}
|
||||
total += sum;
|
||||
}));
|
||||
threads.emplace_back(new std::thread([&q, &total]() {
|
||||
int sum = 0;
|
||||
std::vector<int> stolen;
|
||||
for (int j = 1; j < kEvents;) {
|
||||
if (q.PopBackHalf(&stolen) == 0) {
|
||||
EIGEN_THREAD_YIELD();
|
||||
continue;
|
||||
}
|
||||
while (stolen.size() && j < kEvents) {
|
||||
int v = stolen.back();
|
||||
stolen.pop_back();
|
||||
VERIFY_IS_NOT_EQUAL(v, 0);
|
||||
sum += v;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
while (stolen.size()) {
|
||||
int v = stolen.back();
|
||||
stolen.pop_back();
|
||||
VERIFY_IS_NOT_EQUAL(v, 0);
|
||||
while ((v = q.PushBack(v)) != 0) EIGEN_THREAD_YIELD();
|
||||
}
|
||||
total -= sum;
|
||||
}));
|
||||
}
|
||||
for (size_t i = 0; i < threads.size(); i++) threads[i]->join();
|
||||
VERIFY(q.Empty());
|
||||
VERIFY(total.load() == 0);
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(cxx11_runqueue)
|
||||
{
|
||||
CALL_SUBTEST_1(test_basic_runqueue());
|
||||
CALL_SUBTEST_2(test_empty_runqueue());
|
||||
CALL_SUBTEST_3(test_stress_runqueue());
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <unordered_set>
|
||||
|
||||
#include "main.h"
|
||||
#include <Eigen/CXX11/ThreadPool>
|
||||
#include <Eigen/ThreadPool>
|
||||
|
||||
struct Counter {
|
||||
Counter() = default;
|
||||
|
||||
Reference in New Issue
Block a user