[PATCH] D101699: [Support/Parallel] Add a special case for 0/1 items to llvm::parallel_for_each.

Chris Lattner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 3 10:08:14 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG5fa9d4163421: [Support/Parallel] Add a special case for 0/1 items to llvm::parallel_for_each. (authored by lattner).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101699/new/

https://reviews.llvm.org/D101699

Files:
  llvm/include/llvm/Support/Parallel.h


Index: llvm/include/llvm/Support/Parallel.h
===================================================================
--- llvm/include/llvm/Support/Parallel.h
+++ llvm/include/llvm/Support/Parallel.h
@@ -129,9 +129,20 @@
 
 template <class IterTy, class FuncTy>
 void parallel_for_each(IterTy Begin, IterTy End, FuncTy Fn) {
+  // If we have zero or one items, then do not incur the overhead of spinning up
+  // a task group.  They are surprisingly expensive, and because they do not
+  // support nested parallelism, a single entry task group can block parallel
+  // execution underneath them.
+  auto NumItems = std::distance(Begin, End);
+  if (NumItems <= 1) {
+    if (NumItems)
+      Fn(*Begin);
+    return;
+  }
+
   // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling
   // overhead on large inputs.
-  ptrdiff_t TaskSize = std::distance(Begin, End) / MaxTasksPerGroup;
+  ptrdiff_t TaskSize = NumItems / MaxTasksPerGroup;
   if (TaskSize == 0)
     TaskSize = 1;
 
@@ -145,9 +156,20 @@
 
 template <class IndexTy, class FuncTy>
 void parallel_for_each_n(IndexTy Begin, IndexTy End, FuncTy Fn) {
+  // If we have zero or one items, then do not incur the overhead of spinning up
+  // a task group.  They are surprisingly expensive, and because they do not
+  // support nested parallelism, a single entry task group can block parallel
+  // execution underneath them.
+  auto NumItems = End - Begin;
+  if (NumItems <= 1) {
+    if (NumItems)
+      Fn(Begin);
+    return;
+  }
+
   // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling
   // overhead on large inputs.
-  ptrdiff_t TaskSize = (End - Begin) / MaxTasksPerGroup;
+  ptrdiff_t TaskSize = NumItems / MaxTasksPerGroup;
   if (TaskSize == 0)
     TaskSize = 1;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101699.342436.patch
Type: text/x-patch
Size: 1770 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210503/7d5fa2d3/attachment.bin>


More information about the llvm-commits mailing list