<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Incorrect dependence detection prevents vectorization in Analyzers</title>
    <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184688#M18757</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to get an approximated math function to auto-vectorize in C. The code uses an union to play with floats on the one hand and with the bit representation seen as an int on the other hand. This creates a spurious dependency and the compiler refuses to vectorize the loop despite everything else being safe.&lt;BR /&gt;&lt;BR /&gt;The code of the function looks like:&lt;/P&gt;
&lt;PRE class="brush:cpp; class-name:dark;"&gt;__attribute__((always_inline, const)) inline static float optimized_expf(const float x) {

  const float i = rintf(x * ((float)M_LOG2E));
  const float f = x - ((float)M_LN2) * i;

  float exp_f = 0.041944388f;
  exp_f = exp_f * f + 0.168006673f;
  exp_f = exp_f * f + 0.499999940f;
  exp_f = exp_f * f + 0.999956906f;
  exp_f = exp_f * f + 0.999999642f;

  union {
    int i;
    float f;
  } e;

  e.f = exp_f;
  e.i += ((int)i) &amp;lt;&amp;lt; 23;  // Spurious ANTI dependence here

  return e.f;
}

&lt;/PRE&gt;

&lt;P&gt;The compiler optimisation report tells me the function cannot be vectorized because of an ANTI dependence between e.i and e.f in the last line of the function. This is obviously true but in the same sense as any of the previous operation also need to be run in order.&lt;BR /&gt;The use of the union seems to put the compiler in some incorrect state not understanding that I am only playing with the same bits as I was on the line above.&lt;BR /&gt;This would be the C equivalent of a C++ reinterpret_cast&amp;lt;&amp;gt;, nothing more.&lt;BR /&gt;&lt;BR /&gt;Is there a way to tell the compiler that everything happening here is fine and that it needs not worry?&lt;BR /&gt;There are clearly vector instructions that exist for each of the lines of code here so (auto-)vectorization need not be an issue.&lt;/P&gt;
&lt;P&gt;Thanks in advance for your help and suggestions!&lt;/P&gt;</description>
    <pubDate>Mon, 18 May 2020 15:45:36 GMT</pubDate>
    <dc:creator>matthieuschaller</dc:creator>
    <dc:date>2020-05-18T15:45:36Z</dc:date>
    <item>
      <title>Incorrect dependence detection prevents vectorization</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184688#M18757</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to get an approximated math function to auto-vectorize in C. The code uses an union to play with floats on the one hand and with the bit representation seen as an int on the other hand. This creates a spurious dependency and the compiler refuses to vectorize the loop despite everything else being safe.&lt;BR /&gt;&lt;BR /&gt;The code of the function looks like:&lt;/P&gt;
&lt;PRE class="brush:cpp; class-name:dark;"&gt;__attribute__((always_inline, const)) inline static float optimized_expf(const float x) {

  const float i = rintf(x * ((float)M_LOG2E));
  const float f = x - ((float)M_LN2) * i;

  float exp_f = 0.041944388f;
  exp_f = exp_f * f + 0.168006673f;
  exp_f = exp_f * f + 0.499999940f;
  exp_f = exp_f * f + 0.999956906f;
  exp_f = exp_f * f + 0.999999642f;

  union {
    int i;
    float f;
  } e;

  e.f = exp_f;
  e.i += ((int)i) &amp;lt;&amp;lt; 23;  // Spurious ANTI dependence here

  return e.f;
}

&lt;/PRE&gt;

&lt;P&gt;The compiler optimisation report tells me the function cannot be vectorized because of an ANTI dependence between e.i and e.f in the last line of the function. This is obviously true but in the same sense as any of the previous operation also need to be run in order.&lt;BR /&gt;The use of the union seems to put the compiler in some incorrect state not understanding that I am only playing with the same bits as I was on the line above.&lt;BR /&gt;This would be the C equivalent of a C++ reinterpret_cast&amp;lt;&amp;gt;, nothing more.&lt;BR /&gt;&lt;BR /&gt;Is there a way to tell the compiler that everything happening here is fine and that it needs not worry?&lt;BR /&gt;There are clearly vector instructions that exist for each of the lines of code here so (auto-)vectorization need not be an issue.&lt;/P&gt;
&lt;P&gt;Thanks in advance for your help and suggestions!&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 15:45:36 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184688#M18757</guid>
      <dc:creator>matthieuschaller</dc:creator>
      <dc:date>2020-05-18T15:45:36Z</dc:date>
    </item>
    <item>
      <title>Hi Matthieu,</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184689#M18758</link>
      <description>&lt;P&gt;Hi Matthieu,&lt;/P&gt;&lt;P&gt;The formal answer to your question is to use&lt;/P&gt;
&lt;PRE class="brush:cpp; class-name:dark;"&gt;#pragma omp simd &lt;/PRE&gt;

&lt;P&gt;before your for-loop (the one invoking the function)&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;

&lt;PRE class="brush:cpp; class-name:dark;"&gt;#pragma omp declare simd&lt;/PRE&gt;

&lt;P&gt;before your function.&lt;/P&gt;
&lt;P&gt;Some explanation of OpenMP pragma declare simd (and how to combine it with #pragma omp simd) can be found at e.g.&amp;nbsp;https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/optimization-and-programming-guide/vectorization/explicit-vector-programming/simd-enabled-functions.html&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OpenMP "pragma-simd" will&amp;nbsp;override&amp;nbsp;all compiler's dependencies assumptions and will force&amp;nbsp;compiler to "vectorize at any cost".&lt;/P&gt;
&lt;P&gt;BTW you &lt;EM&gt;don't &lt;/EM&gt;need to use omp runtime (qopenmp) in order to active pragma simd. it is enough to use simd-specific&amp;nbsp;compile-time flag (qopenmp-simd), so no OMP runtime will be used unless you already use it by other reasons.&lt;/P&gt;
&lt;P&gt;That's being said; if compiler got confused with union usage - it might be little issue with final vector code generation anyway;so please double-check the output correctness after forcing vectorization with SIMD.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If questions remain (after using SIMD) - feel free to post the follow-up question in the same forum message thread.&lt;/P&gt;
&lt;P&gt;Zakhar&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 18:41:58 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184689#M18758</guid>
      <dc:creator>Zakhar_M_Intel1</dc:creator>
      <dc:date>2020-05-18T18:41:58Z</dc:date>
    </item>
    <item>
      <title>Hi Zakhar,</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184690#M18759</link>
      <description>&lt;P&gt;Hi Zakhar,&lt;/P&gt;&lt;P&gt;Thanks for the quick response.&lt;BR /&gt;&lt;BR /&gt;I am in general not a big fan of setting this pragma as (as you said) it will vectorize at all costs even if it is possibly problematic to do so. I do especially worry about having this pragma on if I am on a platform where the required instructions for my function either don't exist or are inefficient.&lt;BR /&gt;I worry that by forcing the compiler's hand I'll end up in a silly situation.&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;M.&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 19:16:25 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184690#M18759</guid>
      <dc:creator>matthieuschaller</dc:creator>
      <dc:date>2020-05-18T19:16:25Z</dc:date>
    </item>
    <item>
      <title>Mathieu,</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184691#M18760</link>
      <description>&lt;P&gt;Mathieu,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OpenMP is a standard and OpenMP5.0 is a standard long ago. It is supported by multiple&amp;nbsp;compilers to date: GCC, MSFT (from 2019), Intel, IBM, CLANG, even PGI and Cray&amp;nbsp;(afair), some others. So, first of all, it is not proprietary thing, it is a cross-industry standard, used for x86 and beyond.&lt;/P&gt;&lt;P&gt;Secondly, the huge advantage of OpenMP is portability from ISA to ISA from CPU to CPU. So if your CPU is SSE-only then OMP compilers will generate SSE code for pragma SIMD case.. and for AVX512 one - it will leverage AVX512 under the hood.&lt;/P&gt;&lt;P&gt;So OMP SIMD is not just yet another proprietary pragma trick, but pretty much TRUE cross-vendor and cross-industry standard.&lt;/P&gt;&lt;P&gt;BTW you probably don't need pragma omp declare simd for the very first experiment, because you have inlined function and for the most cases it could be enough (so only #pragma omp simd is required). Although for the full portability I'd follow OMP SIMD standard approach (ie would use both pragma-s in final version)&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 19:38:07 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184691#M18760</guid>
      <dc:creator>Zakhar_M_Intel1</dc:creator>
      <dc:date>2020-05-18T19:38:07Z</dc:date>
    </item>
    <item>
      <title>.. and for your last (perhaps</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184692#M18761</link>
      <description>&lt;P&gt;.. and for your last (perhaps most important) concern - regarding "profitability" of SIMD - in general can not imagine it to degrade in performance when moving from older to&amp;nbsp;newer architectures. So unless you "move back" (totally outside x86 or to SSE machine down from AVX) - you should expect better performance with newer hardware.&lt;/P&gt;&lt;P&gt;You can also double-check Advisor Vector Efficiency metric if you are worried about it.&lt;/P&gt;&lt;P&gt;And if you care of AVX512 "frequencies", then there are centralized flags in both Intel and GCC to control this behavior and those are totally orthogonal to omp simd anyway (so non-omp-simd codes equally suffer/or not suffer from those issues )&lt;/P&gt;&lt;P&gt;Finally, if nothing above can work for you, then we can try to re-post this question somewhere in Compiler forums to check how do they deal with union trick like yours.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 19:50:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184692#M18761</guid>
      <dc:creator>Zakhar_M_Intel1</dc:creator>
      <dc:date>2020-05-18T19:50:00Z</dc:date>
    </item>
    <item>
      <title>Hi,</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184693#M18762</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;Thanks! I wasn't worried about it not being standard but more about the second point you mention.&lt;BR /&gt;The main work horse machine for daily calculations we use only offers AVX (sadly!) so I am a bit worried that by "blindly" forcing vectorization (because it is all fine on AVX512 machines) I may create problems on these older systems.&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can confirm that in this specific case using this pragma works smoothly.&lt;BR /&gt;&lt;BR /&gt;Thanks again,&lt;BR /&gt;M.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 15:42:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184693#M18762</guid>
      <dc:creator>matthieuschaller</dc:creator>
      <dc:date>2020-05-19T15:42:44Z</dc:date>
    </item>
    <item>
      <title>Hi,</title>
      <link>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184694#M18763</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Glad it worked fine for you. I would not be worried about AVX vs AVX512 , esp for codes like YOURS.&amp;nbsp; The only issue (imaginary ) possible would be kind of opposite to what you said; i.e. with NON-DEFAULT compiler flags you may end up AVX512 performance to be &lt;EM&gt;lower &lt;/EM&gt;compared to AVX. But this is really orthogonal to OMP SIMD and this is &lt;EM&gt;not &lt;/EM&gt;happening with default compilation flags anyway..&lt;/P&gt;&lt;P&gt;Zakhar.&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 18:37:59 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/Incorrect-dependence-detection-prevents-vectorization/m-p/1184694#M18763</guid>
      <dc:creator>Zakhar_M_Intel1</dc:creator>
      <dc:date>2020-05-19T18:37:59Z</dc:date>
    </item>
  </channel>
</rss>

