<div dir="ltr"><div>Perhaps the docs should be updated to remove exp? Are there architectures with a SIMD machine instruction for exp()? Or a link to which architectures support which SIMD instructions would also be swell. It's hard to know a priori what will or will not get vectorized. Though clang does at least tell you when it won't be able to vectorize.<br>
<br></div>That's a nice idea with vecmathlib, but for my application I need something that is portable, compiles in gcc/clang/msvc for a wide range of compilers and doesn't require too much hacking. That said, I did look at it before, but the docs are *quite* sparse. The example shows what *not* to do, would be great if you could come up with an example of best practices. If you could, I well might use your library. What's the expected speedup for AVX for instance? How about this as an example: Take a std::vector<double> x, calculate exp(x). I think that would be a seductive application for not just me.<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Aug 19, 2014 at 9:28 PM, Erik Schnetter <span dir="ltr"><<a href="mailto:schnetter@cct.lsu.edu" target="_blank">schnetter@cct.lsu.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div class="">On Sun, Aug 17, 2014 at 9:43 AM, Ian Bell <span dir="ltr"><<a href="mailto:ian.h.bell@gmail.com" target="_blank">ian.h.bell@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>Hello all,<br>
<br></div>I am trying to optimize an inner loop in C++ in which nearly 100% of my runtime occurs. In this loop I have to do some exponential calls, and according to the docs at <a href="http://llvm.org/docs/Vectorizers.html#vectorization-of-function-calls" target="_blank">http://llvm.org/docs/Vectorizers.html#vectorization-of-function-calls</a> , it seems that the exp function should be able to be vectorized using SIMD instructions. I'm using the tippy SVN clang.<br>
</div></div></div></div></div></div></blockquote><div><br></div></div><div>To my knowledge, Clang does not vectorize arbitrary math functions; it only vectorizes those functions for which a SIMD machine instruction exists, such as floor.</div>
<div><br></div><div>There are libraries that provide vectorized exp() functions; you could try using one of these. However, these libraries require that you rewrite your code to use vectors instead of scalars. I maintain such a library "vecmathlib" <<a href="https://bitbucket.org/eschnett/vecmathlib/wiki/Home" target="_blank">https://bitbucket.org/eschnett/vecmathlib/wiki/Home</a>> that is used in pocl <<a href="http://pocl.sourceforge.net/" target="_blank">http://pocl.sourceforge.net/</a>>, an OpenCL implementation based on LLVM.</div>
<div><br></div><div>-erik</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div class="h5">
<div dir="ltr">
<div><div><div><div><div>The example here shows that the floor function is getting vectorized, but the exp function isn't. Is this is a bug or known behavior? I see the same problem if I use floats for what its worth.<br>
<br></div><div>
By the way, there are some problems with the auto-vectorization docs, vectorization is only enabled at -O3, it took me an hour to try to figure out why my -Rpass=loop-vectorize wasn't giving ANY output.<br></div><div>
<br></div>My code (loop_test2.cpp):<br><br>#include <vector><br>#include <cmath><br><br>typedef double numtype;<br><br>void foo(numtype *f) {<br> for (int i = 0; i != 1024; ++i)<br> f[i] = floor(f[i]);<br>
}<br><br>void foo2(numtype *f) {<br> for (int i = 0; i != 1024; ++i)<br> f[i] = exp(f[i]);<br>}<br><br>int main()<br>{<br> std::vector<numtype> f(1024, 1.3);<br> foo(&f[0]);<br> return 0;<br>}<br><br></div>
Compilation:<br>../../../build/Debug+Asserts/bin/clang++ -c -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize -O3 -gline-tables-only -gcolumn-info loop_test2.cpp<br><br></div>which yields<br><br>loop_test2.cpp:7:8: remark: unrolled with interleaving factor 2 (vectorization<br>
not beneficial) [-Rpass=loop-vectorize]<br> for (int i = 0; i != 1024; ++i)<br> ^<br>loop_test2.cpp:13:12: remark: loop not vectorized: call instruction cannot be<br> vectorized [-Rpass-analysis=loop-vectorize]<br>
f[i] = exp(f[i]);<br> ^<br>loop_test2.cpp:18:3: remark: vectorized loop (vectorization factor: 2, unrolling<br> interleave factor: 2) [-Rpass=loop-vectorize]<br> std::vector<numtype> f(1024, 1.3);<br>
^<br>loop_test2.cpp:8:12: remark: unrolled with interleaving factor 2 (vectorization<br> not beneficial) [-Rpass=loop-vectorize]<br> f[i] = floor(f[i]);<br> ^<br><br></div>Thanks,<br></div>Ian<br>
</div>
<br></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br>Erik Schnetter <<a href="mailto:schnetter@cct.lsu.edu" target="_blank">schnetter@cct.lsu.edu</a>><br><a href="http://www.perimeterinstitute.ca/personal/eschnetter/" target="_blank">http://www.perimeterinstitute.ca/personal/eschnetter/</a>
</font></span></div></div>
</blockquote></div><br></div>