<?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 multiple reduction on stl::vector gets wrong results in Intel® Moderncode for Parallel Architectures</title>
    <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096878#M7272</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;In the following test code, I use omp declare reduction on stl::vector. &amp;nbsp;Using icpc 17.0, omp parallel reduction works correctly on a single stl::vector. &amp;nbsp;However, when I attempt to reduce to two separate vectors, v1 &amp;amp; v2, it returns v1=sum(v1)+sum(v2) and v2=0.0 (where sum is across threads). &amp;nbsp;By my reading of the standard, I would expect it to return v1=sum(v1) and v2=sum(v2). &amp;nbsp;This is what GCC 6.3 does.&lt;/P&gt;

&lt;P&gt;Can my omp directives be tweaked to work with icpc 17.0? &amp;nbsp;Or is there another problem?&lt;/P&gt;

&lt;P&gt;Thanks in advance,&lt;/P&gt;

&lt;P&gt;Sean&lt;/P&gt;

&lt;P&gt;
	&lt;STYLE type="text/css"&gt;p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; min-height: 14.0px}
span.s1 {font-variant-ligatures: no-common-ligatures}
span.Apple-tab-span {white-space:pre}
	&lt;/STYLE&gt;
&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;// &lt;A href="http://stackoverflow.com/questions/43168661/openmp-and-reduction-on-stdvector" target="_blank"&gt;http://stackoverflow.com/questions/43168661/openmp-and-reduction-on-stdvector&lt;/A&gt;
//
#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;functional&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;omp.h&amp;gt;

#pragma omp declare reduction(+ : std::vector&amp;lt;double&amp;gt; : std::transform(omp_in.begin(),omp_in.end(),omp_out.begin(),omp_out.begin(),std::plus&amp;lt;double&amp;gt;())) initializer (omp_priv=omp_orig)


int check1(int size){
  std::vector&amp;lt;double&amp;gt; result(size,0.0);

  int npart=100;
  int n;
  {
    int i;
// Works in GCC 6.3, Intel 17.0:
#pragma omp parallel for private(i,n) reduction(+:result)
    for (n=0; n&amp;lt;npart; n++){
      for (i=0; i&amp;lt;size; i++){
	result&lt;I&gt;  += i;
      }
    }
  }
  int fail=0;
  for (int i=0; i&amp;lt;size; i++) {
    if (result&lt;I&gt; != i*100){
      fail=1;
    } 
    std::cout &amp;lt;&amp;lt;"i="&amp;lt;&amp;lt; i&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result&lt;I&gt;&amp;lt;&amp;lt;std::endl;
  }
  return fail;
}

int check2(int size){
  std::vector&amp;lt;double&amp;gt; result(size,0.0);
  std::vector&amp;lt;double&amp;gt; result2(size,0.0);

  int npart=100;
  int n;
  {
    int i;
// Works in GCC 6.3, fails in Intel 17.0:
//#pragma omp parallel for private(i,n) reduction(+:result),reduction(+:result2)
// Works in GCC 6.3, fails in Intel 17.0:
#pragma omp parallel for private(i,n) reduction(+:result,result2)
    for (n=0; n&amp;lt;npart; n++){
      for (i=0; i&amp;lt;size; i++){
	result&lt;I&gt;  += i;
	result2&lt;I&gt; += i;
      }
    }
  }
  int fail=0;
  for (int i=0; i&amp;lt;size; i++) {
    if (result&lt;I&gt; != i*100 || result2&lt;I&gt; != i*100){
      fail=1;
    }
    std::cout &amp;lt;&amp;lt;"i="&amp;lt;&amp;lt; i&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result&lt;I&gt;&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result2&lt;I&gt;&amp;lt;&amp;lt;std::endl;
  }
  return fail;
}



int main(int argc, char *argv[]) {

  int size;

  if (argc &amp;lt; 2)
    size = 10;
  else
    size = atoi(argv[1]);

  int fail1=check1(size);
  int fail2=check2(size);

  return fail1+fail2;
}
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 May 2017 17:42:49 GMT</pubDate>
    <dc:creator>Sean_D_</dc:creator>
    <dc:date>2017-05-02T17:42:49Z</dc:date>
    <item>
      <title>multiple reduction on stl::vector gets wrong results</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096878#M7272</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;In the following test code, I use omp declare reduction on stl::vector. &amp;nbsp;Using icpc 17.0, omp parallel reduction works correctly on a single stl::vector. &amp;nbsp;However, when I attempt to reduce to two separate vectors, v1 &amp;amp; v2, it returns v1=sum(v1)+sum(v2) and v2=0.0 (where sum is across threads). &amp;nbsp;By my reading of the standard, I would expect it to return v1=sum(v1) and v2=sum(v2). &amp;nbsp;This is what GCC 6.3 does.&lt;/P&gt;

&lt;P&gt;Can my omp directives be tweaked to work with icpc 17.0? &amp;nbsp;Or is there another problem?&lt;/P&gt;

&lt;P&gt;Thanks in advance,&lt;/P&gt;

&lt;P&gt;Sean&lt;/P&gt;

&lt;P&gt;
	&lt;STYLE type="text/css"&gt;p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; min-height: 14.0px}
span.s1 {font-variant-ligatures: no-common-ligatures}
span.Apple-tab-span {white-space:pre}
	&lt;/STYLE&gt;
&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;// &lt;A href="http://stackoverflow.com/questions/43168661/openmp-and-reduction-on-stdvector" target="_blank"&gt;http://stackoverflow.com/questions/43168661/openmp-and-reduction-on-stdvector&lt;/A&gt;
//
#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;functional&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;omp.h&amp;gt;

#pragma omp declare reduction(+ : std::vector&amp;lt;double&amp;gt; : std::transform(omp_in.begin(),omp_in.end(),omp_out.begin(),omp_out.begin(),std::plus&amp;lt;double&amp;gt;())) initializer (omp_priv=omp_orig)


int check1(int size){
  std::vector&amp;lt;double&amp;gt; result(size,0.0);

  int npart=100;
  int n;
  {
    int i;
// Works in GCC 6.3, Intel 17.0:
#pragma omp parallel for private(i,n) reduction(+:result)
    for (n=0; n&amp;lt;npart; n++){
      for (i=0; i&amp;lt;size; i++){
	result&lt;I&gt;  += i;
      }
    }
  }
  int fail=0;
  for (int i=0; i&amp;lt;size; i++) {
    if (result&lt;I&gt; != i*100){
      fail=1;
    } 
    std::cout &amp;lt;&amp;lt;"i="&amp;lt;&amp;lt; i&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result&lt;I&gt;&amp;lt;&amp;lt;std::endl;
  }
  return fail;
}

int check2(int size){
  std::vector&amp;lt;double&amp;gt; result(size,0.0);
  std::vector&amp;lt;double&amp;gt; result2(size,0.0);

  int npart=100;
  int n;
  {
    int i;
// Works in GCC 6.3, fails in Intel 17.0:
//#pragma omp parallel for private(i,n) reduction(+:result),reduction(+:result2)
// Works in GCC 6.3, fails in Intel 17.0:
#pragma omp parallel for private(i,n) reduction(+:result,result2)
    for (n=0; n&amp;lt;npart; n++){
      for (i=0; i&amp;lt;size; i++){
	result&lt;I&gt;  += i;
	result2&lt;I&gt; += i;
      }
    }
  }
  int fail=0;
  for (int i=0; i&amp;lt;size; i++) {
    if (result&lt;I&gt; != i*100 || result2&lt;I&gt; != i*100){
      fail=1;
    }
    std::cout &amp;lt;&amp;lt;"i="&amp;lt;&amp;lt; i&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result&lt;I&gt;&amp;lt;&amp;lt;" "&amp;lt;&amp;lt;result2&lt;I&gt;&amp;lt;&amp;lt;std::endl;
  }
  return fail;
}



int main(int argc, char *argv[]) {

  int size;

  if (argc &amp;lt; 2)
    size = 10;
  else
    size = atoi(argv[1]);

  int fail1=check1(size);
  int fail2=check2(size);

  return fail1+fail2;
}
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 May 2017 17:42:49 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096878#M7272</guid>
      <dc:creator>Sean_D_</dc:creator>
      <dc:date>2017-05-02T17:42:49Z</dc:date>
    </item>
    <item>
      <title>I guess the question would</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096879#M7273</link>
      <description>&lt;P&gt;I guess the question would get more rapid attention on the C++ forum or with a submission to supporttickets.com&lt;/P&gt;

&lt;P&gt;I'll try this myself when I get a chance.&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 19:00:53 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096879#M7273</guid>
      <dc:creator>TimP</dc:creator>
      <dc:date>2017-05-02T19:00:53Z</dc:date>
    </item>
    <item>
      <title> </title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096880#M7274</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Thank you I just resubmitted to the C++ forum.&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 19:29:08 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/multiple-reduction-on-stl-vector-gets-wrong-results/m-p/1096880#M7274</guid>
      <dc:creator>Sean_D_</dc:creator>
      <dc:date>2017-05-02T19:29:08Z</dc:date>
    </item>
  </channel>
</rss>

