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.
This commit is contained in:
Changming Sun 2020-03-12 11:33:33 -07:00 committed by GitHub
parent f49912c42a
commit a02638eb46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 4 deletions

View file

@ -43,17 +43,16 @@ void ThreadPool::ParallelFor(int32_t total, std::function<void(int32_t)> 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<unsigned int>(total - 1));
Barrier barrier(static_cast<unsigned int>(total));
std::function<void(int32_t)> 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();
}

View file

@ -669,7 +669,7 @@ MlasGetMaximumThreadCount(
MLAS_UNREFERENCED_PARAMETER(ThreadPool);
#else
if (ThreadPool != nullptr) {
return ThreadPool->NumThreads() + 1;
return ThreadPool->NumThreads();
}
#endif