From a02638eb4660c70ed44d0439be6a6d31cb7530cd Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Thu, 12 Mar 2020 11:33:33 -0700 Subject: [PATCH] Adjust the threading logic in ThreadPool::ParallelFor (#3178) 1. Do not reuse the main thread. 2. Do not plus one when mlas calculate the number of tasks to schedule. (It was me put the plus one there) This is the second try of #1839 It's known that this change has negative performance impact on some of the models. --- onnxruntime/core/common/threadpool.cc | 5 ++--- onnxruntime/core/mlas/lib/mlasi.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/common/threadpool.cc b/onnxruntime/core/common/threadpool.cc index 9f08dc68bb..d4eba8a0ec 100644 --- a/onnxruntime/core/common/threadpool.cc +++ b/onnxruntime/core/common/threadpool.cc @@ -43,17 +43,16 @@ void ThreadPool::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 - 1)); + Barrier barrier(static_cast(total)); std::function handle_iteration = [&barrier, &fn](int iteration) { fn(iteration); barrier.Notify(); }; - for (int32_t id = 1; id < total; ++id) { + for (int32_t id = 0; id < total; ++id) { Schedule([=, &handle_iteration]() { handle_iteration(id); }); } - fn(0); barrier.Wait(); } diff --git a/onnxruntime/core/mlas/lib/mlasi.h b/onnxruntime/core/mlas/lib/mlasi.h index 12ed28672e..d11feb930c 100644 --- a/onnxruntime/core/mlas/lib/mlasi.h +++ b/onnxruntime/core/mlas/lib/mlasi.h @@ -669,7 +669,7 @@ MlasGetMaximumThreadCount( MLAS_UNREFERENCED_PARAMETER(ThreadPool); #else if (ThreadPool != nullptr) { - return ThreadPool->NumThreads() + 1; + return ThreadPool->NumThreads(); } #endif