Enable threadpool threads to greedily acquire new tasks if available. (#17808)
authorOwen Anderson <owen.anderson@oculus.com>
Wed, 13 Mar 2019 01:00:23 +0000 (18:00 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 13 Mar 2019 01:05:55 +0000 (18:05 -0700)
Summary:
This improves locality and affinity by keeping work on the same
threads preferentially to starting work on new ones, and reduces
contention on the threadpool lock more generally.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/17808

Differential Revision: D14391282

Pulled By: resistor

fbshipit-source-id: 3aec81656a50460a725aa4187c61864295d4f46e

c10/core/thread_pool.cpp

index b2ffffc..d1afaa1 100644 (file)
@@ -67,10 +67,10 @@ void ThreadPool::waitWorkComplete() {
 void ThreadPool::main_loop(std::size_t index) {
   init_thread();
 
+  std::unique_lock<std::mutex> lock(mutex_);
   while (running_) {
     // Wait on condition variable while the task is empty and
     // the pool is still running.
-    std::unique_lock<std::mutex> lock(mutex_);
     while (tasks_.empty() && running_) {
       condition_.wait(lock);
     }
@@ -112,6 +112,10 @@ void ThreadPool::main_loop(std::size_t index) {
         complete_ = true;
         completed_.notify_one();
       }
+
+      // Deliberately hold the lock on the backedge, so this thread has an
+      // opportunity to acquire a new task before another thread acquires
+      // the lock.
     }
   } // while running_
 }