[PATCH] [lld][Core] Implement parallel_for_each

Shankar Kalpathi Easwaran shankarke at gmail.com
Sun Mar 15 11:01:06 PDT 2015


Hi davide, Bigcheese, rui314,

Remove TODO and implement parallel_for_each.

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,35 @@
   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,
+                       size_t depth) {
+
+  size_t len = std::distance(begin, end);
+
+  if (len < detail::minParallelSize || depth == 0) {
+    std::for_each(begin, end, func);
+    return;
+  }
+
+  auto pivot = begin + len / 2;
+
+  // Recurse.
+  tg.spawn([=, &func, &tg] {
+    parallel_for_each(begin, pivot, func, tg, depth - 1);
+  });
+  parallel_for_each(pivot + 1, end, func, tg, depth - 1);
+}
+}
+
 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;
+  detail::parallel_for_each(begin, end, func, tg,
+                            llvm::Log2_64(std::distance(begin, end)) + 1);
 }
+
 #endif
 } // end namespace lld

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


More information about the llvm-commits mailing list