Skip to content

Commit 06eab2f

Browse files
before merge
1 parent eb4448b commit 06eab2f

8 files changed

Lines changed: 198 additions & 114 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ target_compile_options(matrix PRIVATE ${OpenMP_CXX_FLAGS})
104104
target_link_libraries(matrix Threads::Threads OpenMP::OpenMP_CXX)
105105

106106
add_executable(parallel_for example/parallel_for.cpp)
107-
target_compile_options(parallel_for PRIVATE ${OpenMP_CXX_FLAGS})
108-
target_link_libraries(parallel_for Threads::Threads OpenMP::OpenMP_CXX)
107+
target_link_libraries(parallel_for Threads::Threads)
109108

110109
add_executable(threadpool_cxx14 example/threadpool_cxx14.cpp)
111110
set_property(TARGET threadpool_cxx14 PROPERTY CXX_STANDARD 14)
@@ -143,8 +142,8 @@ add_test(detached_subflow ${TF_UTEST_DIR}/taskflow -tc=Taskflow.DetachedSubflow)
143142
add_executable(threadpool_test_tmp unittest/threadpool.cpp)
144143
target_link_libraries(threadpool_test_tmp Threads::Threads)
145144
set_target_properties(threadpool_test_tmp PROPERTIES OUTPUT_NAME "threadpool")
146-
add_test(simple_threadpool ${TF_UTEST_DIR}/threadpool -tc=SimpleThreadpool)
147-
add_test(proactive_threadpool ${TF_UTEST_DIR}/threadpool -tc=ProactiveThreadpool)
145+
add_test(simple_threadpool ${TF_UTEST_DIR}/threadpool -tc=SimpleThreadpool)
146+
add_test(proactive_threadpool ${TF_UTEST_DIR}/threadpool -tc=ProactiveThreadpool)
148147
add_test(speculative_threadpool ${TF_UTEST_DIR}/threadpool -tc=SpeculativeThreadpool)
149148

150149
## threadpool_cxx14 unittest (contributed by Glen Fraser)

example/parallel_for.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,25 @@ void taskflow(int N) {
4141
<< " ms\n";
4242
}
4343

44-
// Procedure: openmp
45-
void openmp(int N) {
46-
47-
std::vector<int> range(N);
48-
std::iota(range.begin(), range.end(), 0);
49-
50-
auto tbeg = std::chrono::steady_clock::now();
51-
#pragma omp parallel for
52-
for(int i=0; i<N; ++i) {
53-
printf("fib[%d]=%d\n", range[i], fib(range[i]));
54-
}
55-
auto tend = std::chrono::steady_clock::now();
56-
std::cout << "openmp version takes "
57-
<< std::chrono::duration_cast<std::chrono::milliseconds>(tend-tbeg).count()
58-
<< " ms\n";
59-
}
60-
6144
// ------------------------------------------------------------------------------------------------
6245

6346
// Function: main
6447
int main(int argc, char* argv[]) {
6548

6649
if(argc != 3) {
67-
std::cerr << "usage: ./parallel_for [baseline|openmp|taskflow] N\n";
50+
std::cerr << "usage: ./parallel_for [baseline|taskflow] N\n";
6851
std::exit(EXIT_FAILURE);
6952
}
7053

7154
// Run methods
7255
if(std::string_view method(argv[1]); method == "baseline") {
7356
sequential(std::atoi(argv[2]));
7457
}
75-
else if(method == "openmp") {
76-
openmp(std::atoi(argv[2]));
77-
}
7858
else if(method == "taskflow") {
7959
taskflow(std::atoi(argv[2]));
8060
}
8161
else {
82-
std::cerr << "wrong method, shoud be [baseline|openmp|taskflow]\n";
62+
std::cerr << "wrong method, shoud be [baseline|taskflow]\n";
8363
}
8464

8565
return 0;

example/threadpool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ auto linear_insertions() {
4343
for(size_t i=0; i<num_threads; i++){
4444
insert(num_tasks / num_threads);
4545
}
46-
46+
4747
// synchronize until all tasks finish
4848
future.get();
4949
assert(sum == num_threads);
@@ -57,7 +57,7 @@ auto linear_insertions() {
5757
void benchmark_linear_insertions() {
5858

5959
std::cout << "==== Linear Insertions ====\n";
60-
60+
6161
std::cout << "Speculative threadpool elapsed time: "
6262
<< linear_insertions<tf::SpeculativeThreadpool>() << " ms\n";
6363

taskflow/taskflow.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
// ============================================================================
4646
// version
4747
#define TASKFLOW_VERSION_MAJOR 2
48-
#define TASKFLOW_VERSION_MINOR 1
48+
#define TASKFLOW_VERSION_MINOR 2
4949
#define TASKFLOW_VERSION_PATCH 0
5050
// ============================================================================
5151

@@ -207,7 +207,7 @@ struct MoC {
207207
};
208208

209209
// Forward declaration
210-
template <template<typename, typename...> class FuncType>
210+
template <template<typename...> class FuncType>
211211
class BasicNode;
212212

213213
template <typename NodeType>
@@ -228,7 +228,7 @@ class BasicTaskflow;
228228
// ----------------------------------------------------------------------------
229229

230230
// Class: BasicNode
231-
template <template<typename, typename...> class FuncType>
231+
template <template<typename...> class FuncType>
232232
class BasicNode {
233233

234234
template <typename U> friend class BasicTask;
@@ -268,46 +268,46 @@ class BasicNode {
268268
};
269269

270270
// Constructor
271-
template <template<typename, typename...> class FuncType>
271+
template <template<typename...> class FuncType>
272272
template <typename C>
273273
BasicNode<FuncType>::BasicNode(C&& c) : _work {std::forward<C>(c)} {
274274
}
275275

276276
// Procedure:
277-
template <template<typename, typename...> class FuncType>
277+
template <template<typename...> class FuncType>
278278
void BasicNode<FuncType>::precede(BasicNode& v) {
279279
_successors.push_back(&v);
280280
++v._dependents;
281281
}
282282

283283
// Function: num_successors
284-
template <template<typename, typename...> class FuncType>
284+
template <template<typename...> class FuncType>
285285
size_t BasicNode<FuncType>::num_successors() const {
286286
return _successors.size();
287287
}
288288

289289
// Function: dependents
290-
template <template<typename, typename...> class FuncType>
290+
template <template<typename...> class FuncType>
291291
size_t BasicNode<FuncType>::num_dependents() const {
292292
return _dependents.load();
293293
}
294294

295295
// Function: name
296-
template <template<typename, typename...> class FuncType>
296+
template <template<typename...> class FuncType>
297297
const std::string& BasicNode<FuncType>::name() const {
298298
return _name;
299299
}
300300

301301
// Function: dump
302-
template <template<typename, typename...> class FuncType>
302+
template <template<typename...> class FuncType>
303303
std::string BasicNode<FuncType>::dump() const {
304304
std::ostringstream os;
305305
_dump(os);
306306
return os.str();
307307
}
308308

309309
// Function: _dump
310-
template <template<typename, typename...> class FuncType>
310+
template <template<typename...> class FuncType>
311311
void BasicNode<FuncType>::_dump(std::ostream& os) const {
312312

313313
if(_name.empty()) os << '\"' << this << '\"';

taskflow/threadpool/proactive_threadpool.hpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,38 +92,38 @@ class BasicProactiveThreadpool {
9292
};
9393

9494
// Constructor
95-
template < template<typename...> class TaskType >
96-
BasicProactiveThreadpool<TaskType>::BasicProactiveThreadpool(unsigned N){
95+
template < template<typename...> class Func >
96+
BasicProactiveThreadpool<Func>::BasicProactiveThreadpool(unsigned N){
9797
spawn(N);
9898
}
9999

100100
// Destructor
101-
template < template<typename...> class TaskType >
102-
BasicProactiveThreadpool<TaskType>::~BasicProactiveThreadpool(){
101+
template < template<typename...> class Func >
102+
BasicProactiveThreadpool<Func>::~BasicProactiveThreadpool(){
103103
shutdown();
104104
}
105105

106106
// Function: is_owner
107-
template < template<typename...> class TaskType >
108-
bool BasicProactiveThreadpool<TaskType>::is_owner() const {
107+
template < template<typename...> class Func >
108+
bool BasicProactiveThreadpool<Func>::is_owner() const {
109109
return std::this_thread::get_id() == _owner;
110110
}
111111

112112
// Function: num_tasks
113-
template < template<typename...> class TaskType >
114-
size_t BasicProactiveThreadpool<TaskType>::num_tasks() const {
113+
template < template<typename...> class Func >
114+
size_t BasicProactiveThreadpool<Func>::num_tasks() const {
115115
return _task_queue.size();
116116
}
117117

118118
// Function: num_workers
119-
template < template<typename...> class TaskType >
120-
size_t BasicProactiveThreadpool<TaskType>::num_workers() const {
119+
template < template<typename...> class Func >
120+
size_t BasicProactiveThreadpool<Func>::num_workers() const {
121121
return _threads.size();
122122
}
123123

124124
// Procedure: shutdown
125-
template < template<typename...> class TaskType >
126-
void BasicProactiveThreadpool<TaskType>::shutdown() {
125+
template < template<typename...> class Func >
126+
void BasicProactiveThreadpool<Func>::shutdown() {
127127

128128
if(!is_owner()){
129129
throw std::runtime_error("Worker thread cannot shut down the pool");
@@ -170,8 +170,8 @@ void BasicProactiveThreadpool<TaskType>::shutdown() {
170170
}
171171

172172
// Procedure: spawn
173-
template < template<typename...> class TaskType >
174-
void BasicProactiveThreadpool<TaskType>::spawn(unsigned N) {
173+
template < template<typename...> class Func >
174+
void BasicProactiveThreadpool<Func>::spawn(unsigned N) {
175175

176176
if(!is_owner()){
177177
throw std::runtime_error("Worker thread cannot spawn threads");
@@ -221,9 +221,9 @@ void BasicProactiveThreadpool<TaskType>::spawn(unsigned N) {
221221
}
222222

223223
// Procedure: silent_async
224-
template < template<typename...> class TaskType >
224+
template < template<typename...> class Func >
225225
template <typename C>
226-
void BasicProactiveThreadpool<TaskType>::silent_async(C&& c){
226+
void BasicProactiveThreadpool<Func>::silent_async(C&& c){
227227

228228
TaskType t {std::forward<C>(c)};
229229

@@ -247,9 +247,9 @@ void BasicProactiveThreadpool<TaskType>::silent_async(C&& c){
247247
}
248248

249249
// Function: async
250-
template < template<typename...> class TaskType >
250+
template < template<typename...> class Func >
251251
template <typename C>
252-
auto BasicProactiveThreadpool<TaskType>::async(C&& c) {
252+
auto BasicProactiveThreadpool<Func>::async(C&& c) {
253253

254254
using R = std::invoke_result_t<C>;
255255

@@ -316,8 +316,8 @@ auto BasicProactiveThreadpool<TaskType>::async(C&& c) {
316316
}
317317

318318
// Function: wait_for_all
319-
template < template<typename...> class TaskType >
320-
void BasicProactiveThreadpool<TaskType>::wait_for_all() {
319+
template < template<typename...> class Func >
320+
void BasicProactiveThreadpool<Func>::wait_for_all() {
321321

322322
if(!is_owner()){
323323
throw std::runtime_error("Worker thread cannot wait for all");
@@ -333,11 +333,5 @@ void BasicProactiveThreadpool<TaskType>::wait_for_all() {
333333

334334
}; // namespace proactive_threadpool -----------------------------------------
335335

336-
namespace tf {
337-
338-
using ProactiveThreadpool = proactive_threadpool::BasicProactiveThreadpool<std::function>;
339-
340-
}; // namespace tf. ----------------------------------------------------------
341-
342336

343337

0 commit comments

Comments
 (0)