// 2018/10/24 - created by Tsung-Wei Huang
//
// This program demonstrates how to share the executor among different
// taskflow objects to avoid over subcription of threads.
#include
#include
#include
#include
// Parameters
size_t MAX_TASKFLOW;
const size_t MAX_COUNT = 100;
const size_t MAX_THREAD = std::thread::hardware_concurrency();
// do some useful work
void matrix_multiplication() {
thread_local std::random_device r;
thread_local std::default_random_engine eng(r());
thread_local std::uniform_int_distribution dist(1, 100);
std::vector> a (MAX_COUNT);
std::vector> b (MAX_COUNT);
std::vector> c (MAX_COUNT);
// initialize the matrix
for(size_t i=0; i tfs;
for(size_t i=0; i> futures;
for(auto& tf : tfs) {
futures.emplace_back(tf.dispatch());
}
for(auto& fu : futures) {
fu.get();
}
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast(end - beg).count();
}
// Function: shared_executor
// The program creates an executor and share it with multiple
// taskflow objects. There is no over-subscription in this implementation.
auto shared_executor() {
auto beg = std::chrono::high_resolution_clock::now();
std::list tfs;
auto executor = std::make_shared(MAX_THREAD);
for(size_t i=0; i> futures;
for(auto& tf : tfs) {
futures.emplace_back(tf.dispatch());
}
for(auto& fu : futures) {
fu.get();
}
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast(end - beg).count();
}
// ----------------------------------------------------------------------------
// Function: main
int main(int argc, char* argv[]) {
const size_t width = 12;
std::cout << "====================================================\n"
<< "shared: all taskflow objects share the same executor\n"
<< "unique: each taskflow objects keep a unique executor\n"
<< "====================================================\n";
std::cout << std::setw(width) << "# taskflows"
<< std::setw(width) << "shared (ms)"
<< std::setw(width) << "unique (ms)"
<< std::endl;
for(MAX_TASKFLOW=1; MAX_TASKFLOW<=128; MAX_TASKFLOW *= 2) {
auto s = shared_executor();
auto u = unique_executor();
std::cout << std::setw(width) << MAX_TASKFLOW
<< std::setw(width) << s
<< std::setw(width) << u
<< std::endl;
}
return 0;
}