[PATCH] [lld][Core] Implement parallel_for_each

Shankar Kalpathi Easwaran shankarke at gmail.com
Mon Mar 16 07:17:43 PDT 2015


The results are very similar to the previous patch that was posted. Regresses on performance with bigger links on my box.


http://reviews.llvm.org/D8348

Files:
  include/lld/Core/Parallel.h

Index: include/lld/Core/Parallel.h
===================================================================
--- include/lld/Core/Parallel.h
+++ include/lld/Core/Parallel.h
@@ -293,11 +293,36 @@
   concurrency::parallel_for_each(begin, end, func);
 }
 #else
+namespace detail {
+template <class Iterator, class Func>
+void parallel_for_each(Iterator begin, Iterator end, Func &func, TaskGroup &tg,
+                       unsigned concurrency, size_t taskSize) {
+
+  size_t len = std::distance(begin, end);
+
+  if (len <= taskSize || len <= concurrency) {
+    std::for_each(begin, end, func);
+    return;
+  }
+
+  auto pivot = begin + len / 2;
+
+  // Recurse.
+  tg.spawn([=, &func, &tg] {
+    parallel_for_each(begin, pivot, func, tg, concurrency, taskSize);
+  });
+  parallel_for_each(pivot, end, func, tg, concurrency, taskSize);
+}
+}
+
 template <class Iterator, class Func>
 void parallel_for_each(Iterator begin, Iterator end, Func func) {
-  // TODO: Make this parallel.
-  std::for_each(begin, end, func);
+  TaskGroup tg;
+  unsigned concurrency = std::thread::hardware_concurrency();
+  detail::parallel_for_each(begin, end, func, tg, concurrency,
+                            std::distance(begin, end) / concurrency);
 }
+
 #endif
 } // end namespace lld

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8348.22019.patch
Type: text/x-patch
Size: 1271 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150316/35f88fb8/attachment.bin>


More information about the llvm-commits mailing list