From 62b340608c6284a068f2d23bbd181030455c5aa0 Mon Sep 17 00:00:00 2001 From: Yufeng Li Date: Tue, 30 Apr 2019 16:20:54 -0700 Subject: [PATCH] Execute one task in current thread (#947) Resnet50 takes 44.35 ms with the change, comparing to 50.94 ms without the change. The test environment is: Win 10, Intel i7-4790. --- onnxruntime/core/common/threadpool.cc | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/onnxruntime/core/common/threadpool.cc b/onnxruntime/core/common/threadpool.cc index 875b90c4ff..07305a41d0 100644 --- a/onnxruntime/core/common/threadpool.cc +++ b/onnxruntime/core/common/threadpool.cc @@ -78,32 +78,34 @@ class ThreadPool::Impl : public Eigen::ThreadPool { void ParallelFor(int32_t total, std::function fn) { // TODO: Eigen supports a more efficient ThreadPoolDevice mechanism // We will simply rely on the work queue and stealing in the short term. - Barrier barrier(static_cast(total)); + Barrier barrier(static_cast(total - 1)); std::function handle_iteration = [&barrier, &fn](int iteration) { fn(iteration); barrier.Notify(); }; - for (int32_t id = 0; id < total; ++id) { + for (int32_t id = 1; id < total; ++id) { Schedule([=, &handle_iteration]() { handle_iteration(id); }); } + fn(0); barrier.Wait(); } void ParallelForRange(int64_t first, int64_t last, std::function fn) { // TODO: Eigen supports a more efficient ThreadPoolDevice mechanism // We will simply rely on the work queue and stealing in the short term. - Barrier barrier(static_cast(last - first + 1)); + Barrier barrier(static_cast(last - first)); std::function handle_range = [&barrier, &fn](int64_t first, int64_t last) { fn(first, last); barrier.Notify(); }; - for (int64_t id = first; id <= last; ++id) { + for (int64_t id = first + 1; id <= last; ++id) { Schedule([=, &handle_range]() { handle_range(id, id + 1); }); } + fn(first, first + 1); barrier.Wait(); } }; @@ -127,15 +129,16 @@ class ThreadPool::Impl : public TaskThreadPool { fn(id); } #else - Barrier barrier(static_cast(total)); + Barrier barrier(static_cast(total - 1)); std::function handle_iteration = [&barrier, &fn](int iteration) { fn(iteration); barrier.Notify(); }; - for (int32_t id = 0; id < total; ++id) { + for (int32_t id = 1; id < total; ++id) { std::packaged_task task(std::bind(handle_iteration, id)); RunTask(std::move(task)); } + fn(0); barrier.Wait(); #endif } @@ -147,15 +150,16 @@ class ThreadPool::Impl : public TaskThreadPool { fn(id, id + 1); } #else - Barrier barrier(static_cast(last - first + 1)); + Barrier barrier(static_cast(last - first)); std::function handle_iteration = [&barrier, &fn](int64_t first, int64_t last) { fn(first, last); barrier.Notify(); }; - for (int64_t id = first; id < last; ++id) { + for (int64_t id = first + 1; id < last; ++id) { std::packaged_task task(std::bind(handle_iteration, id, id + 1)); RunTask(std::move(task)); } + fn(first, first + 1); barrier.Wait(); #endif }