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.
This commit is contained in:
Yufeng Li 2019-04-30 16:20:54 -07:00 committed by GitHub
parent 3b0dda0aca
commit 62b340608c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -78,32 +78,34 @@ class ThreadPool::Impl : public Eigen::ThreadPool {
void 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));
Barrier barrier(static_cast<unsigned int>(total - 1));
std::function<void(int32_t)> 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<void(int64_t, int64_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>(last - first + 1));
Barrier barrier(static_cast<unsigned int>(last - first));
std::function<void(int64_t, int64_t)> 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<unsigned int>(total));
Barrier barrier(static_cast<unsigned int>(total - 1));
std::function<void(int32_t)> 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<void()> 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<unsigned int>(last - first + 1));
Barrier barrier(static_cast<unsigned int>(last - first));
std::function<void(int64_t, int64_t)> 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<void()> task(std::bind(handle_iteration, id, id + 1));
RunTask(std::move(task));
}
fn(first, first + 1);
barrier.Wait();
#endif
}