threads_fork_join.cpp File Reference
#include "main.h"
#include "Eigen/ThreadPool"

Classes

struct  TestData
 

Macros

#define EIGEN_USE_THREADS
 

Functions

TestData make_test_data (int num_threads, int num_shards)
 
static void test_unary_parallel_for (int granularity)
 
static void test_binary_parallel_for (int granularity)
 
static void test_async_parallel_for ()
 
 EIGEN_DECLARE_TEST (fork_join)
 

Macro Definition Documentation

◆ EIGEN_USE_THREADS

#define EIGEN_USE_THREADS

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( fork_join  )
73  {
79 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382
static void test_async_parallel_for()
Definition: threads_fork_join.cpp:50
static void test_unary_parallel_for(int granularity)
Definition: threads_fork_join.cpp:23
static void test_binary_parallel_for(int granularity)
Definition: threads_fork_join.cpp:36

References CALL_SUBTEST, test_async_parallel_for(), test_binary_parallel_for(), and test_unary_parallel_for().

◆ make_test_data()

TestData make_test_data ( int  num_threads,
int  num_shards 
)
19  {
20  return {ThreadPool(num_threads), std::vector<double>(num_shards, 1.0)};
21 }
ThreadPoolTempl< StlThreadEnvironment > ThreadPool
Definition: NonBlockingThreadPool.h:580

Referenced by test_async_parallel_for(), test_binary_parallel_for(), and test_unary_parallel_for().

◆ test_async_parallel_for()

static void test_async_parallel_for ( )
static
50  {
51  // Test correctness.
52  // NOTE: Granularity and type of `do_func` are checked in the synchronous tests.
53  const int kNumThreads = 4;
54  const int kNumTasks = 100;
55  const int kNumAsyncCalls = kNumThreads * 4;
56  TestData test_data = make_test_data(kNumThreads, kNumTasks);
57  std::atomic<double> sum = 0.0;
58  std::function<void(int)> unary_do_fn = [&](int i) {
59  for (double new_sum = sum; !sum.compare_exchange_weak(new_sum, new_sum + test_data.data[i]);) {
60  };
61  };
62  Barrier barrier(kNumTasks * kNumAsyncCalls);
63  std::function<void()> done = [&]() { barrier.Notify(); };
64  for (int k = 0; k < kNumAsyncCalls; ++k) {
65  test_data.tp.Schedule([&]() {
66  ForkJoinScheduler::ParallelForAsync(0, kNumTasks, /*granularity=*/1, unary_do_fn, done, &test_data.tp);
67  });
68  }
69  barrier.Wait();
70  VERIFY_IS_EQUAL(sum, kNumTasks * kNumAsyncCalls);
71 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Definition: Barrier.h:21
void Schedule(std::function< void()> fn) EIGEN_OVERRIDE
Definition: NonBlockingThreadPool.h:120
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
Definition: threads_fork_join.cpp:14
ThreadPool tp
Definition: threads_fork_join.cpp:15
std::vector< double > data
Definition: threads_fork_join.cpp:16
TestData make_test_data(int num_threads, int num_shards)
Definition: threads_fork_join.cpp:19

References TestData::data, i, k, make_test_data(), Eigen::Barrier::Notify(), Eigen::ThreadPoolTempl< Environment >::Schedule(), TestData::tp, VERIFY_IS_EQUAL, and Eigen::Barrier::Wait().

Referenced by EIGEN_DECLARE_TEST().

◆ test_binary_parallel_for()

static void test_binary_parallel_for ( int  granularity)
static
36  {
37  // Test correctness.
38  const int kNumTasks = 100000;
39  TestData test_data = make_test_data(/*num_threads=*/4, kNumTasks);
40  std::atomic<double> sum = 0.0;
41  std::function<void(int, int)> binary_do_fn = [&](int i, int j) {
42  for (int k = i; k < j; ++k)
43  for (double new_sum = sum; !sum.compare_exchange_weak(new_sum, new_sum + test_data.data[k]);) {
44  };
45  };
46  ForkJoinScheduler::ParallelFor(0, kNumTasks, granularity, std::move(binary_do_fn), &test_data.tp);
47  VERIFY_IS_EQUAL(sum, kNumTasks);
48 }
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References TestData::data, i, j, k, make_test_data(), TestData::tp, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_unary_parallel_for()

static void test_unary_parallel_for ( int  granularity)
static
23  {
24  // Test correctness.
25  const int kNumTasks = 100000;
26  TestData test_data = make_test_data(/*num_threads=*/4, kNumTasks);
27  std::atomic<double> sum = 0.0;
28  std::function<void(int)> unary_do_fn = [&](int i) {
29  for (double new_sum = sum; !sum.compare_exchange_weak(new_sum, new_sum + test_data.data[i]);) {
30  };
31  };
32  ForkJoinScheduler::ParallelFor(0, kNumTasks, granularity, std::move(unary_do_fn), &test_data.tp);
33  VERIFY_IS_EQUAL(sum, kNumTasks);
34 }

References TestData::data, i, make_test_data(), TestData::tp, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().