mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-12 03:39:01 +08:00
Added a test to validate the new non blocking thread pool
This commit is contained in:
parent
4013b8feca
commit
6bf8273bc0
@ -139,6 +139,8 @@ if(EIGEN_TEST_CXX11)
|
|||||||
|
|
||||||
ei_add_test(cxx11_eventcount "-pthread" "${CMAKE_THREAD_LIBS_INIT}")
|
ei_add_test(cxx11_eventcount "-pthread" "${CMAKE_THREAD_LIBS_INIT}")
|
||||||
ei_add_test(cxx11_runqueue "-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_meta)
|
||||||
ei_add_test(cxx11_tensor_simple)
|
ei_add_test(cxx11_tensor_simple)
|
||||||
# ei_add_test(cxx11_tensor_symmetry)
|
# ei_add_test(cxx11_tensor_symmetry)
|
||||||
|
91
unsupported/test/cxx11_non_blocking_thread_pool.cpp
Normal file
91
unsupported/test/cxx11_non_blocking_thread_pool.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
static void test_parallelism()
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
NonBlockingThreadPool tp(kThreads);
|
||||||
|
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([&]() {
|
||||||
|
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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_cxx11_non_blocking_thread_pool()
|
||||||
|
{
|
||||||
|
CALL_SUBTEST(test_parallelism());
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user