title: Any ways to speed up this C++ AMP code?
body: <p>Today I learned the basics of c++ amp for the purpose of maybe using it for a plugin I'm writing. The plugin needs to do a lot of edge vs triangle intersection tests so I wrote this test code to see if its a viable option.</p>
  
  <p>The function for processing and data quantities are real, the data itself just random stuff. </p>
  
  <p>To my disappointment, the AMP version runs ~3 times slower than even the unoptimized cpu version (i tried to keep the functions as similar as possible). The problem seems to be the part where I write to the result view (all though, i suspect that by commenting that bit out the entire shader gets snipped by the compiler).</p>
  
  <p>Anyway, I have an optimized cpu version that runs ~20 times faster than the cpu version in the test code and its still way too slow. Is there any hope for the AMP version to catch up and surpass that?</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;iostream&gt;
  #include &lt;sys/timeb.h&gt;
  #include &lt;math.h&gt;
  #include &lt;vector&gt;
  #include &lt;amp.h&gt;
  #include &lt;amp_math.h&gt;
  #include &lt;random&gt;
  using namespace concurrency;
  
  struct point3 {
      float a, b, c;
      point3() {
          a = rand() % 100;
          b = rand() % 100;
          c = rand() % 100;
      }
  };
  
  struct thing {
      point3 center;
      float radius;
  };
  
  int lastTick;
  timeb tb;
  int tick() {
      int hold = lastTick;
      ftime(&amp;tb);
      lastTick = tb.millitm + (tb.time &amp; 0xfffff) * 1000;
      return lastTick - hold;
  }
  
  int main()
  {
      system("PAUSE");
  
      std::vector&lt;thing&gt; faces(13500);
      std::vector&lt;thing&gt; edges(20000);
      std::vector&lt;int&gt; results(faces.size() * edges.size());
      array_view&lt;const thing&gt; v1(faces.size(), faces);
      array_view&lt;const thing&gt; v2(edges.size(), edges);
      array_view&lt;int&gt; vr(results.size(), results);
  
      int nFaces = faces.size();
      int nEdges = edges.size();
  
      tick();
      for (int x = 0; x &lt; nFaces; x++) {
          thing* face = &amp;faces[x];
  
          int a, b, c;
          thing* edge;
  
          for (int y = 0; y &lt; nEdges; y++) {
              edge = &amp;edges[y];
              a = edge-&gt;center.a - face-&gt;center.a;
              b = edge-&gt;center.b - face-&gt;center.b;
              c = edge-&gt;center.c - face-&gt;center.c;
              results[x * nFaces + y] = (sqrt(a * a + b * b + c * c) &lt; (face-&gt;radius + edge-&gt;radius)) ? 1 : 0;
          }
      }
      std::cout &lt;&lt; "cpu time: " &lt;&lt; tick() &lt;&lt; "\n";
  
      v1.refresh();
      v2.refresh();
  
      parallel_for_each(v1.extent, [=](index&lt;1&gt; x) restrict(amp) {
          const thing* face = &amp;v1[x];
  
          int a, b, c;
          const thing* edge;
  
          for (int y = 0; y &lt; nEdges; y++) {
              edge = &amp;v2[y];
              a = edge-&gt;center.a - face-&gt;center.a;
              b = edge-&gt;center.b - face-&gt;center.b;
              c = edge-&gt;center.c - face-&gt;center.c;
              vr[x * nFaces + y] = (fast_math::sqrt(a * a + b * b + c * c) &lt; (face-&gt;radius + edge-&gt;radius)) ? 1 : 0;
          }
      });
  
      vr.synchronize();
      std::cout &lt;&lt; "gpu time: " &lt;&lt; tick() &lt;&lt; "\n";
  
      system("PAUSE");
  }
  </code></pre>