Skip to content

Commit

Permalink
Rewrite the handling of threads==1, so it's a little more readable, a…
Browse files Browse the repository at this point in the history
…nd gets compiled with -O3 in a way that puts this case at the start of the function instead of at the end, which for a mysterious reason results in more stable performance.

PiperOrigin-RevId: 262565366
  • Loading branch information
bjacob authored and tensorflower-gardener committed Aug 9, 2019
1 parent 977f213 commit c94c683
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions tensorflow/lite/experimental/ruy/thread_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,26 @@ class Thread {

void ThreadPool::ExecuteImpl(int task_count, int stride, Task* tasks) {
RUY_DCHECK_GE(task_count, 1);
if (task_count > 1) {
// Task #0 will be run on the current thread.
CreateThreads(task_count - 1);
counter_to_decrement_when_ready_.Reset(task_count - 1);
for (int i = 1; i < task_count; i++) {
auto task_address = reinterpret_cast<std::uintptr_t>(tasks) + i * stride;
threads_[i - 1]->StartWork(reinterpret_cast<Task*>(task_address));
}

// Case of 1 thread: just run the single task on the current thread.
if (task_count == 1) {
(tasks + 0)->Run();
return;
}

// Task #0 will be run on the current thread.
CreateThreads(task_count - 1);
counter_to_decrement_when_ready_.Reset(task_count - 1);
for (int i = 1; i < task_count; i++) {
auto task_address = reinterpret_cast<std::uintptr_t>(tasks) + i * stride;
threads_[i - 1]->StartWork(reinterpret_cast<Task*>(task_address));
}

// Execute task #0 immediately on the current thread.
(tasks + 0)->Run();

if (task_count > 1) {
// Wait for the threads submitted above to finish.
counter_to_decrement_when_ready_.Wait();
}
// Wait for the threads submitted above to finish.
counter_to_decrement_when_ready_.Wait();
}

// Ensures that the pool has at least the given count of threads.
Expand Down

0 comments on commit c94c683

Please sign in to comment.