| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cd4fb2f commit cfabb78
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -118,30 +118,32 @@ enable_testing() | |||
| 118 | 118 | ||
| 119 | 119 | message(STATUS "Building unit tests ...") | |
| 120 | 120 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/unittest) | |
| 121 | + set(TF_UTEST_DIR ${PROJECT_SOURCE_DIR}/unittest) | ||
| 121 | 122 | add_executable(taskflow unittest/taskflow.cpp) | |
| 122 | 123 | target_link_libraries(taskflow Threads::Threads) | |
| 123 | 124 | ||
| 124 | 125 | # unittest for taskflow | |
| 125 | - add_test(builder ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.Builder) | ||
| 126 | - add_test(dispatch ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.Dispatch) | ||
| 127 | - add_test(parallel_for ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.ParallelFor) | ||
| 128 | - add_test(reduce ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.Reduce) | ||
| 129 | - add_test(reduce_min ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.ReduceMin) | ||
| 130 | - add_test(reduce_max ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.ReduceMax) | ||
| 131 | - add_test(joined_subflow ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.JoinedSubflow) | ||
| 132 | - add_test(detached_subflow ${PROJECT_SOURCE_DIR}/unittest/taskflow -tc=Taskflow.DetachedSubflow) | ||
| 126 | + add_test(builder ${TF_UTEST_DIR}/taskflow -tc=Taskflow.Builder) | ||
| 127 | + add_test(dispatch ${TF_UTEST_DIR}/taskflow -tc=Taskflow.Dispatch) | ||
| 128 | + add_test(parallel_for ${TF_UTEST_DIR}/taskflow -tc=Taskflow.ParallelFor) | ||
| 129 | + add_test(reduce ${TF_UTEST_DIR}/taskflow -tc=Taskflow.Reduce) | ||
| 130 | + add_test(reduce_min ${TF_UTEST_DIR}/taskflow -tc=Taskflow.ReduceMin) | ||
| 131 | + add_test(reduce_max ${TF_UTEST_DIR}/taskflow -tc=Taskflow.ReduceMax) | ||
| 132 | + add_test(joined_subflow ${TF_UTEST_DIR}/taskflow -tc=Taskflow.JoinedSubflow) | ||
| 133 | + add_test(detached_subflow ${TF_UTEST_DIR}/taskflow -tc=Taskflow.DetachedSubflow) | ||
| 133 | 134 | ||
| 134 | 135 | # unittest for threadpool | |
| 135 | - add_executable(threadpool_test unittest/threadpool.cpp) | ||
| 136 | - target_link_libraries(threadpool_test Threads::Threads) | ||
| 137 | - | ||
| 138 | - add_test(proactivethreadpool ${PROJECT_SOURCE_DIR}/unittest/threadpool_test -tc=Threadpool.ProactiveThreadpool) | ||
| 136 | + add_executable(threadpool_test_tmp unittest/threadpool.cpp) | ||
| 137 | + target_link_libraries(threadpool_test_tmp Threads::Threads) | ||
| 138 | + set_target_properties(threadpool_test_tmp PROPERTIES OUTPUT_NAME "threadpool") | ||
| 139 | + add_test(simple_threadpool ${TF_UTEST_DIR}/threadpool -tc=SimpleThreadpool) | ||
| 140 | + add_test(proactive_threadpool ${TF_UTEST_DIR}/threadpool -tc=ProactiveThreadpool) | ||
| 139 | 141 | ||
| 140 | 142 | ## threadpool_cxx14 unittest (contributed by Glen Fraser) | |
| 141 | 143 | add_executable(threadpool_cxx14_tmp unittest/threadpool_cxx14.cpp) | |
| 142 | 144 | set_target_properties(threadpool_cxx14_tmp PROPERTIES CXX_STANDARD 14) | |
| 143 | 145 | target_link_libraries(threadpool_cxx14_tmp Threads::Threads) | |
| 144 | 146 | set_target_properties(threadpool_cxx14_tmp PROPERTIES OUTPUT_NAME "threadpool_cxx14") | |
| 145 | - add_test(threadpool_cxx14_basic ${PROJECT_SOURCE_DIR}/unittest/threadpool_cxx14 -tc=Threadpool.Basic) | ||
| 146 | - add_test(threadpool_cxx14_wait_for_all ${PROJECT_SOURCE_DIR}/unittest/threadpool_cxx14 -tc=Threadpool.WaitForAll) | ||
| 147 | + add_test(threadpool_cxx14_basic ${TF_UTEST_DIR}/threadpool_cxx14 -tc=Threadpool.Basic) | ||
| 148 | + add_test(threadpool_cxx14_wait_for_all ${TF_UTEST_DIR}/threadpool_cxx14 -tc=Threadpool.WaitForAll) | ||
| 147 | 149 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -150,7 +150,7 @@ void cppthread(const std::vector<size_t>& D) { | |||
| 150 | 150 | ||
| 151 | 151 | auto tbeg = std::chrono::steady_clock::now(); | |
| 152 | 152 | ||
| 153 | - tf::Threadpool tpl(std::thread::hardware_concurrency()); | ||
| 153 | + tf::SimpleThreadpool tpl(std::thread::hardware_concurrency()); | ||
| 154 | 154 | ||
| 155 | 155 | std::cout << "Generating matrix As ...\n"; | |
| 156 | 156 | std::vector<matrix_t> As(D.size()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,84 +1,86 @@ | |||
| 1 | - //2018/8/31 contributed by Guannan | ||
| 2 | - // Examples to test throughput of various theadpools | ||
| 1 | + // 2018/8/31 contributed by Guannan | ||
| 2 | + // | ||
| 3 | + // Examples to test different threadpool implementations: | ||
| 4 | + // - SimpleThreadpool | ||
| 5 | + // - ProactiveThreadpool | ||
| 3 | 6 | ||
| 4 | 7 | #include <taskflow/threadpool/threadpool.hpp> | |
| 5 | - #include <taskflow/threadpool/proactive_threadpool.hpp> | ||
| 6 | 8 | #include <chrono> | |
| 7 | - #include <atomic> | ||
| 8 | - #include <thread> | ||
| 9 | + #include <random> | ||
| 9 | 10 | ||
| 11 | + // Procedure: benchmark_empty_jobs | ||
| 10 | 12 | void benchmark_empty_jobs() { | |
| 11 | 13 | ||
| 12 | - std::cout << "Testing threadpool throughput on empty jobs..." << std::endl; | ||
| 14 | + std::cout << "Benchmarking threadpool throughput on empty jobs ...\n"; | ||
| 13 | 15 | ||
| 14 | 16 | unsigned thread_num = 4; | |
| 15 | 17 | unsigned int task_num = 10000000; | |
| 16 | 18 | ||
| 17 | 19 | auto start = std::chrono::high_resolution_clock::now(); | |
| 18 | 20 | ||
| 19 | - tf::ProactiveThreadpool pool(thread_num); | ||
| 21 | + tf::ProactiveThreadpool proactive(thread_num); | ||
| 20 | 22 | for(size_t i=0; i<task_num; i++){ | |
| 21 | - pool.silent_async([](){}); | ||
| 23 | + proactive.silent_async([](){}); | ||
| 22 | 24 | } | |
| 23 | - pool.shutdown(); | ||
| 25 | + proactive.shutdown(); | ||
| 24 | 26 | ||
| 25 | 27 | auto end = std::chrono::high_resolution_clock::now(); | |
| 26 | - std::chrono::duration<double> elapsed = end - start; | ||
| 27 | - std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << std::endl; | ||
| 28 | + auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | ||
| 29 | + std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << " ms\n"; | ||
| 28 | 30 | ||
| 29 | 31 | start = std::chrono::high_resolution_clock::now(); | |
| 30 | 32 | ||
| 31 | - tf::Threadpool tf_pool(thread_num); | ||
| 33 | + tf::SimpleThreadpool simple(thread_num); | ||
| 32 | 34 | for(size_t i=0; i<task_num; i++){ | |
| 33 | - tf_pool.silent_async([](){}); | ||
| 35 | + simple.silent_async([](){}); | ||
| 34 | 36 | } | |
| 35 | - tf_pool.shutdown(); | ||
| 37 | + simple.shutdown(); | ||
| 36 | 38 | ||
| 37 | 39 | end = std::chrono::high_resolution_clock::now(); | |
| 38 | - elapsed = end - start; | ||
| 39 | - std::cout << "Basic elapsed time: " << elapsed.count() << std::endl; | ||
| 40 | - std::cout << std::endl; | ||
| 40 | + elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | ||
| 41 | + std::cout << "SimpleThreadpool elapsed time: " << elapsed.count() << " ms\n"; | ||
| 41 | 42 | } | |
| 42 | 43 | ||
| 43 | - | ||
| 44 | + // Procedure: benchmark_atomic_add | ||
| 44 | 45 | void benchmark_atomic_add() { | |
| 45 | 46 | ||
| 46 | - std::cout << "Testing threadpool throughput on atomic add..." << std::endl; | ||
| 47 | + std::cout << "Benchmarking threadpool throughput on atomic add ...\n"; | ||
| 47 | 48 | ||
| 48 | 49 | unsigned thread_num = 4; | |
| 49 | 50 | unsigned int task_num = 10000000; | |
| 50 | 51 | ||
| 51 | 52 | std::atomic<int> counter(0); | |
| 52 | 53 | auto start = std::chrono::high_resolution_clock::now(); | |
| 53 | 54 | ||
| 54 | - tf::ProactiveThreadpool pool(thread_num); | ||
| 55 | + tf::ProactiveThreadpool proactive(thread_num); | ||
| 55 | 56 | for(size_t i=0; i<task_num; i++){ | |
| 56 | - pool.silent_async([&counter](){ counter++; }); | ||
| 57 | + proactive.silent_async([&counter](){ counter++; }); | ||
| 57 | 58 | } | |
| 58 | - pool.shutdown(); | ||
| 59 | + proactive.shutdown(); | ||
| 59 | 60 | ||
| 60 | 61 | auto end = std::chrono::high_resolution_clock::now(); | |
| 61 | - std::chrono::duration<double> elapsed = end - start; | ||
| 62 | - std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << std::endl; | ||
| 62 | + auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | ||
| 63 | + std::cout << "ProactiveThreadpool elapsed time: " << elapsed.count() << " ms\n"; | ||
| 63 | 64 | ||
| 64 | 65 | counter = 0; | |
| 65 | 66 | start = std::chrono::high_resolution_clock::now(); | |
| 66 | - tf::Threadpool tf_pool(thread_num); | ||
| 67 | + tf::SimpleThreadpool simple(thread_num); | ||
| 67 | 68 | ||
| 68 | 69 | for(size_t i=0; i<task_num; i++){ | |
| 69 | - tf_pool.silent_async([&counter](){ counter++; }); | ||
| 70 | + simple.silent_async([&counter](){ counter++; }); | ||
| 70 | 71 | } | |
| 71 | - tf_pool.shutdown(); | ||
| 72 | + simple.shutdown(); | ||
| 72 | 73 | ||
| 73 | 74 | end = std::chrono::high_resolution_clock::now(); | |
| 74 | - elapsed = end - start; | ||
| 75 | - std::cout << "Basic elapsed time: " << elapsed.count() << std::endl; | ||
| 76 | - std::cout << std::endl; | ||
| 75 | + elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | ||
| 76 | + std::cout << "SimpleThreadpool elapsed time: " << elapsed.count() << " ms\n"; | ||
| 77 | 77 | } | |
| 78 | 78 | ||
| 79 | + // Function: main | ||
| 79 | 80 | int main(int argc, char* argv[]) { | |
| 80 | 81 | ||
| 81 | 82 | benchmark_empty_jobs(); | |
| 82 | 83 | benchmark_atomic_add(); | |
| 83 | - | ||
| 84 | + | ||
| 85 | + return 0; | ||
| 84 | 86 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | // MIT License | |
| 2 | 2 | // | |
| 3 | - // Copyright (c) 2018 Tsung-Wei Huang, Chun-Xun Lin, and Martin Wong | ||
| 3 | + // Copyright (c) 2018 Tsung-Wei Huang, Chun-Xun Lin, Guannan Guo, and Martin Wong | ||
| 4 | 4 | // | |
| 5 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 6 | 6 | // of this software and associated documentation files (the "Software"), to deal | |
@@ -45,8 +45,8 @@ | |||
| 45 | 45 | // ============================================================================ | |
| 46 | 46 | // version | |
| 47 | 47 | #define TASKFLOW_VERSION_MAJOR 2 | |
| 48 | - #define TASKFLOW_VERSION_MINOR 0 | ||
| 49 | - #define TASKFLOW_VERSION_PATCH 2 | ||
| 48 | + #define TASKFLOW_VERSION_MINOR 1 | ||
| 49 | + #define TASKFLOW_VERSION_PATCH 0 | ||
| 50 | 50 | // ============================================================================ | |
| 51 | 51 | ||
| 52 | 52 | // Clang mis-interprets variant's get as a non-friend of variant and cannot | |
@@ -194,8 +194,19 @@ inline constexpr bool is_iterable_v = is_iterable<T>::value; | |||
| 194 | 194 | // Taskflow definition | |
| 195 | 195 | //----------------------------------------------------------------------------- | |
| 196 | 196 | ||
| 197 | - // Forward declaration | ||
| 197 | + // Struct: MoC | ||
| 198 | + template <typename T> | ||
| 199 | + struct MoC { | ||
| 198 | 200 | ||
| 201 | + MoC(T&& rhs) : object(std::move(rhs)) {} | ||
| 202 | + MoC(const MoC& other) : object(std::move(other.object)) {} | ||
| 203 | + | ||
| 204 | + T& get() { return object; } | ||
| 205 | + | ||
| 206 | + mutable T object; | ||
| 207 | + }; | ||
| 208 | + | ||
| 209 | + // Forward declaration | ||
| 199 | 210 | template <template<typename, typename...> class FuncType> | |
| 200 | 211 | class BasicNode; | |
| 201 | 212 | ||
@@ -1607,7 +1618,7 @@ std::string BasicTaskflow<Traits>::dump() const { | |||
| 1607 | 1618 | // Taskflow traits | |
| 1608 | 1619 | struct TaskflowTraits { | |
| 1609 | 1620 | using NodeType = BasicNode<std::function>; | |
| 1610 | - using ThreadpoolType = Threadpool; | ||
| 1621 | + using ThreadpoolType = SimpleThreadpool; | ||
| 1611 | 1622 | }; | |
| 1612 | 1623 | ||
| 1613 | 1624 | using Taskflow = BasicTaskflow<TaskflowTraits>; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,4 @@ | |||
| 1 | - // | ||
| 2 | - // contributed by Guannan | ||
| 1 | + // 2018/09/03 - contributed by Guannan Guo | ||
| 3 | 2 | // | |
| 4 | 3 | // ProactiveThreadpool schedules independent jobs in a greedy manner. | |
| 5 | 4 | // Whenever a job is inserted into the threadpool, the threadpool will check if there | |
@@ -23,12 +22,28 @@ | |||
| 23 | 22 | ||
| 24 | 23 | namespace tf { | |
| 25 | 24 | ||
| 26 | - | ||
| 27 | 25 | // template < template<typename...> class FuncType > | |
| 28 | - | ||
| 29 | 26 | class ProactiveThreadpool { | |
| 30 | 27 | ||
| 31 | - using UnitTask = std::function<void()>; | ||
| 28 | + using TaskType = std::function<void()>; | ||
| 29 | + | ||
| 30 | + template <typename T> | ||
| 31 | + struct MoC { | ||
| 32 | + | ||
| 33 | + MoC(T&& rhs): object(std::move(rhs)) {} | ||
| 34 | + MoC(const MoC& other) : object(std::move(other.object)) {} | ||
| 35 | + | ||
| 36 | + T& get() {return object; } | ||
| 37 | + | ||
| 38 | + mutable T object; | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + struct Worker{ | ||
| 43 | + std::condition_variable cv; | ||
| 44 | + TaskType task; | ||
| 45 | + bool ready; | ||
| 46 | + }; | ||
| 32 | 47 | ||
| 33 | 48 | public: | |
| 34 | 49 | ||
@@ -89,7 +104,7 @@ class ProactiveThreadpool { | |||
| 89 | 104 | } | |
| 90 | 105 | ||
| 91 | 106 | Worker w; | |
| 92 | - UnitTask t; | ||
| 107 | + TaskType t; | ||
| 93 | 108 | std::unique_lock<std::mutex> lock(_mutex); | |
| 94 | 109 | while(!_exiting){ | |
| 95 | 110 | if(_task_queue.empty()){ | |
@@ -128,7 +143,7 @@ class ProactiveThreadpool { | |||
| 128 | 143 | template <typename C> | |
| 129 | 144 | void silent_async(C&& c){ | |
| 130 | 145 | ||
| 131 | - UnitTask t {std::forward<C>(c)}; | ||
| 146 | + TaskType t {std::forward<C>(c)}; | ||
| 132 | 147 | ||
| 133 | 148 | //no worker thread available | |
| 134 | 149 | if(num_workers() == 0){ | |
@@ -175,7 +190,7 @@ class ProactiveThreadpool { | |||
| 175 | 190 | // all workers are busy. | |
| 176 | 191 | if(_workers.empty()){ | |
| 177 | 192 | _task_queue.emplace_back( | |
| 178 | - [p = MoveOnCopy(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 193 | + [p = MoC(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 179 | 194 | c(); | |
| 180 | 195 | p.get().set_value(); | |
| 181 | 196 | } | |
@@ -186,7 +201,7 @@ class ProactiveThreadpool { | |||
| 186 | 201 | Worker* w = _workers.back(); | |
| 187 | 202 | _workers.pop_back(); | |
| 188 | 203 | w->ready = true; | |
| 189 | - w->task = [p = MoveOnCopy(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 204 | + w->task = [p = MoC(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 190 | 205 | c(); | |
| 191 | 206 | p.get().set_value(); | |
| 192 | 207 | }; | |
@@ -197,7 +212,7 @@ class ProactiveThreadpool { | |||
| 197 | 212 | ||
| 198 | 213 | if(_workers.empty()){ | |
| 199 | 214 | _task_queue.emplace_back( | |
| 200 | - [p = MoveOnCopy(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 215 | + [p = MoC(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 201 | 216 | p.get().set_value(c()); | |
| 202 | 217 | return; | |
| 203 | 218 | } | |
@@ -207,7 +222,7 @@ class ProactiveThreadpool { | |||
| 207 | 222 | Worker* w = _workers.back(); | |
| 208 | 223 | _workers.pop_back(); | |
| 209 | 224 | w->ready = true; | |
| 210 | - w->task = [p = MoveOnCopy(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 225 | + w->task = [p = MoC(std::move(p)), c = std::forward<C>(c)]() mutable { | ||
| 211 | 226 | p.get().set_value(c()); | |
| 212 | 227 | return; | |
| 213 | 228 | }; | |
@@ -234,31 +249,13 @@ class ProactiveThreadpool { | |||
| 234 | 249 | ||
| 235 | 250 | ||
| 236 | 251 | private: | |
| 237 | - | ||
| 238 | - template <typename T> | ||
| 239 | - struct MoveOnCopy{ | ||
| 240 | - | ||
| 241 | - MoveOnCopy(T&& rhs): object(std::move(rhs)) {} | ||
| 242 | - MoveOnCopy(const MoveOnCopy& other) : object(std::move(other.object)) {} | ||
| 243 | - | ||
| 244 | - T& get() {return object; } | ||
| 245 | - | ||
| 246 | - mutable T object; | ||
| 247 | - }; | ||
| 248 | - | ||
| 249 | - | ||
| 250 | - struct Worker{ | ||
| 251 | - std::condition_variable cv; | ||
| 252 | - UnitTask task; | ||
| 253 | - bool ready; | ||
| 254 | - }; | ||
| 255 | 252 | ||
| 256 | 253 | mutable std::mutex _mutex; | |
| 257 | 254 | ||
| 258 | 255 | std::condition_variable _empty; | |
| 259 | 256 | std::condition_variable _complete; | |
| 260 | 257 | ||
| 261 | - std::deque<UnitTask> _task_queue; | ||
| 258 | + std::deque<TaskType> _task_queue; | ||
| 262 | 259 | std::vector<std::thread> _threads; | |
| 263 | 260 | std::vector<Worker*> _workers; | |
| 264 | 261 | std::unordered_set<std::thread::id> _worker_ids; | |
@@ -270,8 +267,7 @@ class ProactiveThreadpool { | |||
| 270 | 267 | }; | |
| 271 | 268 | ||
| 272 | 269 | ||
| 273 | - | ||
| 274 | - }; | ||
| 270 | + }; // namespace tf. ---------------------------------------------------------- | ||
| 275 | 271 | ||
| 276 | 272 | ||
| 277 | 273 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments