title: How to vectorize my loop with g++?
body: <p>The introductory links I found while searching:</p>
  
  <ol>
  <li><a href="https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Loop-Specific-Pragmas.html" rel="noreferrer">6.59.14 Loop-Specific Pragmas</a></li>
  <li><a href="https://gcc.gnu.org/onlinedocs/gnat_rm/Pragma-Loop_005fOptimize.html" rel="noreferrer">2.100 Pragma Loop_Optimize</a></li>
  <li><a href="https://stackoverflow.com/questions/14256156/how-to-give-hint-to-gcc-about-loop-count">How to give hint to gcc about loop count</a></li>
  <li><a href="https://stackoverflow.com/questions/4071690/tell-gcc-to-specifically-unroll-a-loop">Tell gcc to specifically unroll a loop</a></li>
  <li><a href="http://goparallel.sourceforge.net/force-vectorization-c/" rel="noreferrer">How to Force Vectorization in C++</a></li>
  </ol>
  
  <p>As you can see most of them are for C, but I thought that they might work at C++ as well. Here is my code:</p>
  
  <pre><code>template&lt;typename T&gt;
  //__attribute__((optimize("unroll-loops")))
  //__attribute__ ((pure))
  void foo(std::vector&lt;T&gt; &amp;p1, size_t start,
              size_t end, const std::vector&lt;T&gt; &amp;p2) {
    typename std::vector&lt;T&gt;::const_iterator it2 = p2.begin();
    //#pragma simd
    //#pragma omp parallel for
    //#pragma GCC ivdep Unroll Vector
    for (size_t i = start; i &lt; end; ++i, ++it2) {
      p1[i] = p1[i] - *it2;
      p1[i] += 1;
    }
  }
  
  int main()
  {
      size_t n;
      double x,y;
      n = 12800000;
      vector&lt;double&gt; v,u;
      for(size_t i=0; i&lt;n; ++i) {
          x = i;
          y = i - 1;
          v.push_back(x);
          u.push_back(y);
      }
      using namespace std::chrono;
  
      high_resolution_clock::time_point t1 = high_resolution_clock::now();
      foo(v,0,n,u);
      high_resolution_clock::time_point t2 = high_resolution_clock::now();
  
      duration&lt;double&gt; time_span = duration_cast&lt;duration&lt;double&gt;&gt;(t2 - t1);
  
      std::cout &lt;&lt; "It took me " &lt;&lt; time_span.count() &lt;&lt; " seconds.";
      std::cout &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>I used al the hints one can see commented above, but I did not get any speedup, as a sample output shows (with the first run having uncommented this <code>#pragma GCC ivdep Unroll Vector</code>:</p>
  
  <pre><code>samaras@samaras-A15:~/Downloads$ g++ test.cpp -O3 -std=c++0x -funroll-loops -ftree-vectorize -o test
  samaras@samaras-A15:~/Downloads$ ./test
  It took me 0.026575 seconds.
  samaras@samaras-A15:~/Downloads$ g++ test.cpp -O3 -std=c++0x -o test
  samaras@samaras-A15:~/Downloads$ ./test
  It took me 0.0252697 seconds.
  </code></pre>
  
  <p>Is there any hope? Or the optimization flag <code>O3</code> just does the trick? Any suggestions to speedup this code (the <code>foo</code> function) are welcome!</p>
  
  <p>My version of g++:</p>
  
  <pre><code>samaras@samaras-A15:~/Downloads$ g++ --version
  g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
  </code></pre>
  
  <hr>
  
  <p>Notice that the body of the loop is random. I am not interesting in re-writing it in some other form.</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>An answer saying that there is nothing more that can be done is also acceptable!</p>