<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Segoe UI";
        panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi Christopher,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-autospace:none">Yes,  <span style="font-size:10.0pt;font-family:"Segoe UI",sans-serif;color:black">“is_partitioned” algo implementation is based on a reduction parallel pattern.  <o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Segoe UI",sans-serif;color:black">And it looks that a binary operation (combiner)  is not commutative.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Segoe UI",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Segoe UI",sans-serif;color:black">In general, “reduction” algorithm requires a commutative binary operation. And OpenMP reduction requires that.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Segoe UI",sans-serif;color:black">For TBB backend it works because TBB parallel reduction algorithm doesn’t require a commutative binary operation.
<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">We (me or Evgeniy) will check that hypo and inform you.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best regards,<o:p></o:p></p>
<p class="MsoNormal">Mikhail Dvorskiy<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b>From:</b> Christopher Nelson <nadiasvertex@gmail.com> <br>
<b>Sent:</b> Thursday, October 1, 2020 2:46 AM<br>
<b>To:</b> Kukanov, Alexey <Alexey.Kukanov@intel.com><br>
<b>Cc:</b> Dvorskiy, Mikhail <mikhail.dvorskiy@intel.com>; Pavlov, Evgeniy <evgeniy.pavlov@intel.com>; Louis Dionne <ldionne@apple.com>; Thomas Rodgers <trodgers@redhat.com>; Libc++ Dev <libcxx-dev@lists.llvm.org><br>
<b>Subject:</b> [libcxx-dev] OpenMP parallel reduce bugs<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Hello friends,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I have been working on the OpenMP backend for the parallel STL, and most of the tests are passing. However, among the failures is the "is_partitioned" test. I have rewritten the __parallel_reduce backend function to be simpler to understand
 in an attempt to understand what is failing (code is below.)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I also rewrote it as a serial function that splits the iteration range in two and then calls __reduction() on each half of the range being passed in. The result I get from the serial execution as compared to the result I get from the parallel
 execution is different.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I have verified that the parallel execution tasks are run, and that their results match what each serial execution would be if I ran them that way.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I am wondering if there is something wrong with the way OpenMP is running the reducer here? Perhaps it is injecting a value into the computation that is unexpected for this algorithm? Does anything jump out at anyone as being suspicious?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thank you again for your time and assistance!<o:p></o:p></p>
</div>
<div>
<pre><span style="font-size:11.5pt;color:#0033B3">template </span><span style="font-size:11.5pt;color:#080808"><</span><span style="font-size:11.5pt;color:#0033B3">class </span><span style="font-size:11.5pt;color:#371F80">_RandomAccessIterator</span><span style="font-size:11.5pt;color:#080808">, </span><span style="font-size:11.5pt;color:#0033B3">class </span><span style="font-size:11.5pt;color:#371F80">_Value</span><span style="font-size:11.5pt;color:#080808">, </span><span style="font-size:11.5pt;color:#0033B3">typename </span><span style="font-size:11.5pt;color:#371F80">_RealBody</span><span style="font-size:11.5pt;color:#080808">, </span><span style="font-size:11.5pt;color:#0033B3">typename </span><span style="font-size:11.5pt;color:#371F80">_Reduction</span><span style="font-size:11.5pt;color:#080808">><br></span><span style="font-size:11.5pt;color:#371F80">_Value<br></span><span style="font-size:11.5pt;color:#00627A">__parallel_reduce_body</span><span style="font-size:11.5pt;color:#080808">(</span><span style="font-size:11.5pt;color:#371F80">_RandomAccessIterator </span><span style="font-size:11.5pt;color:#080808">__first, </span><span style="font-size:11.5pt;color:#371F80">_RandomAccessIterator </span><span style="font-size:11.5pt;color:#080808">__last, </span><span style="font-size:11.5pt;color:#371F80">_Value </span><span style="font-size:11.5pt;color:#080808">__identity,<br>                       </span><span style="font-size:11.5pt;color:#371F80">_RealBody </span><span style="font-size:11.5pt;color:#080808">__real_body, </span><span style="font-size:11.5pt;color:#371F80">_Reduction </span><span style="font-size:11.5pt;color:#080808">__reduction)<br>{<br>    std::size_t </span><span style="font-size:11.5pt;color:black">__item_count </span><span style="font-size:11.5pt;color:#080808">= __last - __first;<br>    std::size_t </span><span style="font-size:11.5pt;color:black">__head_items </span><span style="font-size:11.5pt;color:#080808">= (__item_count / __default_chunk_size) * __default_chunk_size;<br><br>    </span><i><span style="font-size:11.5pt;color:#8C8C8C">// We should encapsulate a result value and a reduction operator since we<br>    // cannot use a lambda in OpenMP UDR.<br>    </span></i><span style="font-size:11.5pt;color:#0033B3">using </span><span style="font-size:11.5pt;color:#371F80">_CombinerType </span><span style="font-size:11.5pt;color:#080808">= </span><span style="font-size:11.5pt;color:teal">__pstl</span><span style="font-size:11.5pt;color:#080808">::</span><span style="font-size:11.5pt;color:teal">__internal</span><span style="font-size:11.5pt;color:#080808">::</span><span style="font-size:11.5pt;color:teal">_Combiner</span><span style="font-size:11.5pt;color:#080808"><</span><span style="font-size:11.5pt;color:#371F80">_Value</span><span style="font-size:11.5pt;color:#080808">, </span><span style="font-size:11.5pt;color:#371F80">_Reduction</span><span style="font-size:11.5pt;color:#080808">>;<br>    </span><span style="font-size:11.5pt;color:#371F80">_CombinerType </span><span style="font-size:11.5pt;color:black">__result</span><span style="font-size:11.5pt;color:#080808">{__identity, &__reduction};<br>    _PSTL_PRAGMA_DECLARE_REDUCTION(__combiner, _CombinerType)<br><br>    </span><i><span style="font-size:11.5pt;color:#8C8C8C">// To avoid over-subscription we use taskloop for the nested parallelism<br>    //_PSTL_PRAGMA(omp taskloop reduction(__combiner : __result))<br>    </span></i><span style="font-size:11.5pt;color:#0033B3">for </span><span style="font-size:11.5pt;color:#080808">(std::size_t __i = </span><span style="font-size:11.5pt;color:#1750EB">0</span><span style="font-size:11.5pt;color:#080808">; __i < __item_count; __i += __default_chunk_size)<br>    {<br>        </span><span style="font-size:11.5pt;color:#0033B3">auto </span><span style="font-size:11.5pt;color:#080808">__begin = __first + __i;<br>        </span><span style="font-size:11.5pt;color:#0033B3">auto </span><span style="font-size:11.5pt;color:#080808">__end = __i < __head_items ? __begin + __default_chunk_size : __last;<br>        __result.__value = __real_body(__begin, __end, __identity);<br>    }<br><br>    </span><span style="font-size:11.5pt;color:#0033B3">return </span><span style="font-size:11.5pt;color:black">__result</span><span style="font-size:11.5pt;color:#080808">.</span><span style="font-size:11.5pt;color:black">__value</span><span style="font-size:11.5pt;color:#080808">;<br>}    <o:p></o:p></span></pre>
</div>
</div>
</div>
</body>
</html>