threads_eventcount.cpp File Reference
#include "main.h"
#include <Eigen/ThreadPool>

Classes

struct  TestQueue
 

Macros

#define EIGEN_USE_THREADS
 

Functions

int rand_reentrant (unsigned int *s)
 
static void test_basic_eventcount ()
 
static void test_stress_eventcount ()
 
 EIGEN_DECLARE_TEST (cxx11_eventcount)
 

Macro Definition Documentation

◆ EIGEN_USE_THREADS

#define EIGEN_USE_THREADS

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_eventcount  )
134  {
137 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
static void test_basic_eventcount()
Definition: threads_eventcount.cpp:26
static void test_stress_eventcount()
Definition: threads_eventcount.cpp:76

References CALL_SUBTEST, test_basic_eventcount(), and test_stress_eventcount().

◆ rand_reentrant()

int rand_reentrant ( unsigned int s)
17  {
18 #if EIGEN_COMP_MSVC_STRICT
20  return rand();
21 #else
22  return rand_r(s);
23 #endif
24 }
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
RealScalar s
Definition: level1_cplx_impl.h:130

References EIGEN_UNUSED_VARIABLE, and s.

Referenced by test_stress_eventcount().

◆ test_basic_eventcount()

static void test_basic_eventcount ( )
static
26  {
28  waiters.resize(1);
29  EventCount ec(waiters);
30  EventCount::Waiter& w = waiters[0];
31  ec.Notify(false);
32  ec.Prewait();
33  ec.Notify(true);
34  ec.CommitWait(&w);
35  ec.Prewait();
36  ec.CancelWait();
37 }
RowVector3d w
Definition: Matrix_resize_int.cpp:3
Definition: EventCount.h:184
Definition: EventCount.h:52
The MaxSizeVector class.
Definition: MaxSizeVector.h:31

References Eigen::EventCount::CancelWait(), Eigen::EventCount::CommitWait(), Eigen::EventCount::Notify(), Eigen::EventCount::Prewait(), Eigen::MaxSizeVector< T >::resize(), and w.

Referenced by EIGEN_DECLARE_TEST().

◆ test_stress_eventcount()

static void test_stress_eventcount ( )
static
76  {
77  const int kThreads = std::thread::hardware_concurrency();
78  static const int kEvents = 1 << 16;
79  static const int kQueues = 10;
80 
81  MaxSizeVector<EventCount::Waiter> waiters(kThreads);
82  waiters.resize(kThreads);
83  EventCount ec(waiters);
84  TestQueue queues[kQueues];
85 
86  std::vector<std::unique_ptr<std::thread>> producers;
87  for (int i = 0; i < kThreads; i++) {
88  producers.emplace_back(new std::thread([&ec, &queues]() {
89  unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
90  for (int j = 0; j < kEvents; j++) {
91  unsigned idx = rand_reentrant(&rnd) % kQueues;
92  if (queues[idx].Push()) {
93  ec.Notify(false);
94  continue;
95  }
97  j--;
98  }
99  }));
100  }
101 
102  std::vector<std::unique_ptr<std::thread>> consumers;
103  for (int i = 0; i < kThreads; i++) {
104  consumers.emplace_back(new std::thread([&ec, &queues, &waiters, i]() {
105  EventCount::Waiter& w = waiters[i];
106  unsigned int rnd = static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
107  for (int j = 0; j < kEvents; j++) {
108  unsigned idx = rand_reentrant(&rnd) % kQueues;
109  if (queues[idx].Pop()) continue;
110  j--;
111  ec.Prewait();
112  bool empty = true;
113  for (int q = 0; q < kQueues; q++) {
114  if (!queues[q].Empty()) {
115  empty = false;
116  break;
117  }
118  }
119  if (!empty) {
120  ec.CancelWait();
121  continue;
122  }
123  ec.CommitWait(&w);
124  }
125  }));
126  }
127 
128  for (int i = 0; i < kThreads; i++) {
129  producers[i]->join();
130  consumers[i]->join();
131  }
132 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_THREAD_YIELD()
Definition: ThreadYield.h:14
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:2019
const int Empty
Definition: Eigen_Colamd.h:114
Definition: threads_eventcount.cpp:40
int rand_reentrant(unsigned int *s)
Definition: threads_eventcount.cpp:17
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::EventCount::CancelWait(), Eigen::EventCount::CommitWait(), EIGEN_THREAD_YIELD, internal::Colamd::Empty, i, j, Eigen::EventCount::Notify(), Eigen::EventCount::Prewait(), Eigen::numext::q, rand_reentrant(), Eigen::MaxSizeVector< T >::resize(), and w.

Referenced by EIGEN_DECLARE_TEST().