[llvm] r311312 - [Support/Parallel] - Do not use a task group for a very small task.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 21 01:00:54 PDT 2017


Author: grimar
Date: Mon Aug 21 01:00:54 2017
New Revision: 311312

URL: http://llvm.org/viewvc/llvm-project?rev=311312&view=rev
Log:
[Support/Parallel] - Do not use a task group for a very small task.

parallel_for_each_n splits a given task into small pieces of tasks and then
passes them to background threads managed by a thread pool to process them
in parallel. TaskGroup then waits for all tasks to be done, which is done by
TaskGroup's destructor.

In the previous code, all tasks were passed to background threads, and the
main thread just waited for them to finish their jobs. This patch changes
the logic so that the main thread processes a task just like other
worker threads instead of just waiting for workers.

This patch improves the performance of parallel_for_each_n for a task which
is too small that we do not split it into multiple tasks. Previously, such task
was submitted to another thread and the main thread waited for its completion.
That involves multiple inter-thread synchronization which is not cheap for
small tasks. Now, such task is processed by the main thread, so no inter-thread
communication is necessary.

Differential revision: https://reviews.llvm.org/D36607

Modified:
    llvm/trunk/include/llvm/Support/Parallel.h

Modified: llvm/trunk/include/llvm/Support/Parallel.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Parallel.h?rev=311312&r1=311311&r2=311312&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Parallel.h (original)
+++ llvm/trunk/include/llvm/Support/Parallel.h Mon Aug 21 01:00:54 2017
@@ -158,11 +158,11 @@ void parallel_for_each(IterTy Begin, Ite
     TaskSize = 1;
 
   TaskGroup TG;
-  while (TaskSize <= std::distance(Begin, End)) {
+  while (TaskSize < std::distance(Begin, End)) {
     TG.spawn([=, &Fn] { std::for_each(Begin, Begin + TaskSize, Fn); });
     Begin += TaskSize;
   }
-  TG.spawn([=, &Fn] { std::for_each(Begin, End, Fn); });
+  std::for_each(Begin, End, Fn);
 }
 
 template <class IndexTy, class FuncTy>
@@ -179,10 +179,8 @@ void parallel_for_each_n(IndexTy Begin,
         Fn(J);
     });
   }
-  TG.spawn([=, &Fn] {
-    for (IndexTy J = I; J < End; ++J)
-      Fn(J);
-  });
+  for (IndexTy J = I; J < End; ++J)
+    Fn(J);
 }
 
 #endif




More information about the llvm-commits mailing list