<div dir="ltr">Thank you. I wondered if you had an update on this. I've done some further looking, and I think that is correct. I've tried to find example implementations of performing reductions with openmp that don't require a commutative operator. It seems like rewriting the is_partioned algorithm to provide a commutative operator might be a larger / undesirable change.<div><br></div><div>Do you have any guidance on manually writing a task loop in openmp that performs the reduction without requiring commutativity?</div><div><br></div><div>Thanks!</div><div><br></div><div>-={C}=-</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 1, 2020 at 9:11 AM Dvorskiy, Mikhail <<a href="mailto:mikhail.dvorskiy@intel.com">mikhail.dvorskiy@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-US">
<div class="gmail-m_9038091944821351282WordSection1">
<p class="MsoNormal">Hi Christopher,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Yes,  <span style="font-size:10pt;font-family:"Segoe UI",sans-serif;color:black">“is_partitioned” algo implementation is based on a reduction parallel pattern.  <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Segoe UI",sans-serif;color:black">And it looks that a binary operation (combiner)  is not commutative.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Segoe UI",sans-serif;color:black"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Segoe UI",sans-serif;color:black">In general, “reduction” algorithm requires a commutative binary operation. And OpenMP reduction requires that.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;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.
<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">We (me or Evgeniy) will check that hypo and inform you.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Best regards,<u></u><u></u></p>
<p class="MsoNormal">Mikhail Dvorskiy<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><b>From:</b> Christopher Nelson <<a href="mailto:nadiasvertex@gmail.com" target="_blank">nadiasvertex@gmail.com</a>> <br>
<b>Sent:</b> Thursday, October 1, 2020 2:46 AM<br>
<b>To:</b> Kukanov, Alexey <<a href="mailto:Alexey.Kukanov@intel.com" target="_blank">Alexey.Kukanov@intel.com</a>><br>
<b>Cc:</b> Dvorskiy, Mikhail <<a href="mailto:mikhail.dvorskiy@intel.com" target="_blank">mikhail.dvorskiy@intel.com</a>>; Pavlov, Evgeniy <<a href="mailto:evgeniy.pavlov@intel.com" target="_blank">evgeniy.pavlov@intel.com</a>>; Louis Dionne <<a href="mailto:ldionne@apple.com" target="_blank">ldionne@apple.com</a>>; Thomas Rodgers <<a href="mailto:trodgers@redhat.com" target="_blank">trodgers@redhat.com</a>>; Libc++ Dev <<a href="mailto:libcxx-dev@lists.llvm.org" target="_blank">libcxx-dev@lists.llvm.org</a>><br>
<b>Subject:</b> [libcxx-dev] OpenMP parallel reduce bugs<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Hello friends,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></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.)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></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.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></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.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></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?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thank you again for your time and assistance!<u></u><u></u></p>
</div>
<div>
<pre><span style="font-size:11.5pt;color:rgb(0,51,179)">template </span><span style="font-size:11.5pt;color:rgb(8,8,8)"><</span><span style="font-size:11.5pt;color:rgb(0,51,179)">class </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_RandomAccessIterator</span><span style="font-size:11.5pt;color:rgb(8,8,8)">, </span><span style="font-size:11.5pt;color:rgb(0,51,179)">class </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Value</span><span style="font-size:11.5pt;color:rgb(8,8,8)">, </span><span style="font-size:11.5pt;color:rgb(0,51,179)">typename </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_RealBody</span><span style="font-size:11.5pt;color:rgb(8,8,8)">, </span><span style="font-size:11.5pt;color:rgb(0,51,179)">typename </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Reduction</span><span style="font-size:11.5pt;color:rgb(8,8,8)">><br></span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Value<br></span><span style="font-size:11.5pt;color:rgb(0,98,122)">__parallel_reduce_body</span><span style="font-size:11.5pt;color:rgb(8,8,8)">(</span><span style="font-size:11.5pt;color:rgb(55,31,128)">_RandomAccessIterator </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__first, </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_RandomAccessIterator </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__last, </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Value </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__identity,<br>                       </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_RealBody </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__real_body, </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Reduction </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__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:rgb(8,8,8)">= __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:rgb(8,8,8)">= (__item_count / __default_chunk_size) * __default_chunk_size;<br><br>    </span><i><span style="font-size:11.5pt;color:rgb(140,140,140)">// 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:rgb(0,51,179)">using </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_CombinerType </span><span style="font-size:11.5pt;color:rgb(8,8,8)">= </span><span style="font-size:11.5pt;color:teal">__pstl</span><span style="font-size:11.5pt;color:rgb(8,8,8)">::</span><span style="font-size:11.5pt;color:teal">__internal</span><span style="font-size:11.5pt;color:rgb(8,8,8)">::</span><span style="font-size:11.5pt;color:teal">_Combiner</span><span style="font-size:11.5pt;color:rgb(8,8,8)"><</span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Value</span><span style="font-size:11.5pt;color:rgb(8,8,8)">, </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_Reduction</span><span style="font-size:11.5pt;color:rgb(8,8,8)">>;<br>    </span><span style="font-size:11.5pt;color:rgb(55,31,128)">_CombinerType </span><span style="font-size:11.5pt;color:black">__result</span><span style="font-size:11.5pt;color:rgb(8,8,8)">{__identity, &__reduction};<br>    _PSTL_PRAGMA_DECLARE_REDUCTION(__combiner, _CombinerType)<br><br>    </span><i><span style="font-size:11.5pt;color:rgb(140,140,140)">// 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:rgb(0,51,179)">for </span><span style="font-size:11.5pt;color:rgb(8,8,8)">(std::size_t __i = </span><span style="font-size:11.5pt;color:rgb(23,80,235)">0</span><span style="font-size:11.5pt;color:rgb(8,8,8)">; __i < __item_count; __i += __default_chunk_size)<br>    {<br>        </span><span style="font-size:11.5pt;color:rgb(0,51,179)">auto </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__begin = __first + __i;<br>        </span><span style="font-size:11.5pt;color:rgb(0,51,179)">auto </span><span style="font-size:11.5pt;color:rgb(8,8,8)">__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:rgb(0,51,179)">return </span><span style="font-size:11.5pt;color:black">__result</span><span style="font-size:11.5pt;color:rgb(8,8,8)">.</span><span style="font-size:11.5pt;color:black">__value</span><span style="font-size:11.5pt;color:rgb(8,8,8)">;<br>}    <u></u><u></u></span></pre>
</div>
</div>
</div>
</div>

</blockquote></div>