[PATCH] [lld][Core] Implement parallel_for_each

Shankar Kalpathi Easwaran shankarke at gmail.com
Sun Mar 15 19:57:30 PDT 2015


Update code with comments from Rui,

Below are my notes from this patch :-

Your suggestion worked a lot better for simple applications (previously where lld was not parallelizing would become parallel now).

[with lld] clang -B`pwd` main.c
warning: ignoring unknown argument for -z: relro

real	0m0.068s
user	0m0.036s
sys	0m0.036s

[with binutils]$ time clang main.c

real	0m0.425s
user	0m0.036s
sys	0m0.020s

FYI, the above is in DEBUG mode of lld, just cant imagine release mode :)

But for self hosting lld/clang, the performance got degraded, takes 14 seconds to self host instead of 12(happens constantly). I will post the patch anyways for review for further discussion.


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 depth) {
+
+  size_t len = std::distance(begin, end);
+
+  if ((len / concurrency) < len || 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, concurrency, depth - 1);
+  });
+  parallel_for_each(pivot, end, func, tg, concurrency, 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;
+  unsigned concurrency = std::thread::hardware_concurrency();
+  detail::parallel_for_each(begin, end, func, tg, concurrency,
+                            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.22009.patch
Type: text/x-patch
Size: 1277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150316/52337e81/attachment.bin>


More information about the llvm-commits mailing list