title: SSE performance Eigen
body: <p>I'm trying to write some SSE code using Eigen, and some behavior eludes me.</p>
  
  <p>Given code:</p>
  
  <pre><code>#ifndef EIGEN_DONT_VECTORIZE // Not needed with Intel C++ Compiler XE 15.0
  #define EIGEN_VECTORIZE_SSE4_2
  #define EIGEN_VECTORIZE_SSE4_1
  #define EIGEN_VECTORIZE_SSSE3
  #define EIGEN_VECTORIZE_SSE3
  #endif
  
  #include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;unsupported/Eigen/AlignedVector3&gt;
  #include &lt;Eigen/StdVector&gt;
  #include &lt;chrono&gt;
  
  int _tmain(int argc, _TCHAR* argv[]) {
      static const int SIZE = 4000000;
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; A_SSE(1, 1, 1);
      //EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; B_SSE(2, 2, 2);
      //std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt; C_SSE(SIZE, Eigen::AlignedVector3&lt;float&gt;(0,0,0));
  
  
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; A_SSE1(1, 1, 1);
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; A_SSE2(1, 1, 1);
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; A_SSE3(1, 1, 1);
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; A_SSE4(1, 1, 1);
  
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; B_SSE(2, 2, 2);
      EIGEN_ALIGNED_VECTOR3 Eigen::AlignedVector3&lt;float&gt; B_SSE_increment_unroll(16, 16, 16);
  
      A_SSE2 += B_SSE;
      A_SSE3 = A_SSE2 + B_SSE;
      A_SSE4 = A_SSE3 + B_SSE;
  
  
      std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt; C_SSE(SIZE, Eigen::AlignedVector3&lt;float&gt;(0, 0, 0));
  
      auto start2 = std::chrono::system_clock::now();
  
      // no unroll
      for (int iteration = 0; iteration &lt; SIZE; ++iteration) {
          A_SSE += B_SSE;
          C_SSE[iteration] = A_SSE;
      }
  
      //// own unroll
      //for (int iteration = 0; iteration &lt; SIZE / 8; ++iteration){
      //  A_SSE1 += B_SSE_increment_unroll;
      //  A_SSE2 += B_SSE_increment_unroll;
      //  A_SSE3 += B_SSE_increment_unroll;
      //  A_SSE4 += B_SSE_increment_unroll;
  
      //  C_SSE[iteration * 2] = A_SSE1;
      //  C_SSE[iteration * 2 + 1] = A_SSE2;
      //  C_SSE[iteration * 2 + 2] = A_SSE3;
      //  C_SSE[iteration * 2 + 3] = A_SSE4;
  
      //}
  
      auto end2 = std::chrono::system_clock::now();
      auto elapsed2 = end2 - start2;
      std::cout &lt;&lt; "Eigen aligned vector " &lt;&lt; elapsed2.count() &lt;&lt; '\n';
  
      Eigen::Matrix3Xf A = Eigen::Matrix3Xf::Zero(3, SIZE);
      Eigen::Vector3f B(3, 3, 3);
      Eigen::Vector3f C(2, 2, 2);
  
      auto start1 = std::chrono::system_clock::now();
  
      for (int iteration = 0; iteration &lt; SIZE; ++iteration) {
          B += C;
          A.col(iteration) = B;
      }
      auto end1 = std::chrono::system_clock::now();
      auto elapsed1 = end1 - start1;
      std::cout &lt;&lt; "Eigen matrix " &lt;&lt; elapsed1.count() &lt;&lt; '\n';
  
  
      float *pResult = (float*)_aligned_malloc(SIZE * sizeof(float) * 4, 16); // align to 16-byte for SSE
      auto start3 = std::chrono::system_clock::now();
  
      __m128 x;
      __m128 xDelta = _mm_set1_ps(2.0f);      // Set the xDelta to (4,4,4,4)
      __m128 *pResultSSE = (__m128*) pResult;
  
      x = _mm_set_ps(1.0f, 1.0f, 1.0f, 1.0f); // Set the initial values of x to (4,3,2,1)
  
      for (int iteration = 0; iteration &lt; SIZE; ++iteration)
      {
          x = _mm_add_ps(x, xDelta);
          pResultSSE[iteration] = x;
      }
  
      auto end3 = std::chrono::system_clock::now();
      auto elapsed3 = end3 - start3;
      std::cout &lt;&lt; "Own sse " &lt;&lt; elapsed3.count() &lt;&lt; '\n';
  
  }
  </code></pre>
  
  <p>Timing seems odd, on my pc</p>
  
  <ul>
  <li>Eigen Aligned Vector Unroll: 20057</li>
  <li>Eigen Align Vector no unroll: ~120320</li>
  <li>Eigen Matrix: ~120207 ( same as Align no unroll)</li>
  <li>Own SSE: 160784</li>
  </ul>
  
  <p>When I examine assembly, aligned versions and Own SSE use addps movaps, but until i manually unroll loops I don't gain additional performance, and even if I do it not in all runs (50%) I don't get any boost. Version wit Eigen Matrix don't use sse, achieve same performance, inline assembly shows unrolling on 16 iterations. Does manual unrolling is that impactful? Should we manually do it for SSE, and If on with CPU properties it depends?</p>
  
  <p>Edit:
  So to sum up. SSE instruction do not perform better because of not able to prove that unrolling loop will hold same result as not unrolled, so it can not hide memory storage latency. But in assembly code "single" instructions are using only 1 register and incrementing it in unrolled loop. If the SSE addiction is performed vertically (single float in aligned vector accumulates same amount of operation of addition) compiler should be able to prove equality for unrolling. Are SSE operation by default non optimized by compiler? If unrolling loop preserve order of execution, so preserve non associative math, automatic unrolling should be possible so why it does not happen, and how to force compiler to do it?</p>
  
  <p>EDIT:
  As suggested I run test, but bench unit from eigen do not work under visual studio 2017 so it was replaced by</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;unsupported/Eigen/AlignedVector3&gt;
  #include &lt;chrono&gt;
  #include &lt;numeric&gt;
  
  EIGEN_DONT_INLINE
  void vector_no_unroll(std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt;&amp; out)
  {
      Eigen::AlignedVector3&lt;float&gt; A_SSE(1, 1, 1);
      Eigen::AlignedVector3&lt;float&gt; B_SSE(2, 2, 2);
      for (auto &amp;x : out)
      {
          A_SSE += B_SSE;
          x = A_SSE;
      }
  }
  
  EIGEN_DONT_INLINE
  void vector_unrolled(std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt;&amp; out)
  {
      Eigen::AlignedVector3&lt;float&gt; A_SSE1(1, 1, 1);
      Eigen::AlignedVector3&lt;float&gt; A_SSE2(1, 1, 1);
      Eigen::AlignedVector3&lt;float&gt; A_SSE3(1, 1, 1);
      Eigen::AlignedVector3&lt;float&gt; A_SSE4(1, 1, 1);
  
      Eigen::AlignedVector3&lt;float&gt; B_SSE(2, 2, 2);
      Eigen::AlignedVector3&lt;float&gt; B_SSE_increment_unroll(16, 16, 16);
  
      A_SSE2 += B_SSE;
      A_SSE3 = A_SSE2 + B_SSE;
      A_SSE4 = A_SSE3 + B_SSE;
      for (size_t i = 0; i&lt;out.size(); i += 4)
      {
          A_SSE1 += B_SSE_increment_unroll;
          A_SSE2 += B_SSE_increment_unroll;
          A_SSE3 += B_SSE_increment_unroll;
          A_SSE4 += B_SSE_increment_unroll;
          out[i + 0] = A_SSE1;
          out[i + 1] = A_SSE2;
          out[i + 2] = A_SSE3;
          out[i + 3] = A_SSE4;
      }
  }
  
  EIGEN_DONT_INLINE
  void eigen_matrix(Eigen::Matrix3Xf&amp; out)
  {
      Eigen::Vector3f B(1, 1, 1);
      Eigen::Vector3f C(2, 2, 2);
  
      for (int i = 0; i &lt; out.cols(); ++i) {
          B += C;
          out.col(i) = B;
      }
  }
  
  template&lt;int unrolling&gt; EIGEN_DONT_INLINE
  void eigen_matrix_unrolled(Eigen::Matrix3Xf&amp; out)
  {
      Eigen::Matrix&lt;float, 3, unrolling&gt; B = Eigen::Matrix&lt;float, 1, unrolling&gt;::LinSpaced(3.f, 1 + 2 * unrolling).template replicate&lt;3, 1&gt;();
  
      for (int i = 0; i &lt; out.cols(); i += unrolling) {
          out.middleCols&lt;unrolling&gt;(i) = B;
          B.array() += float(2 * unrolling);
      }
  }
  
  int main() {
      static const int SIZE = 4000000;
  
      int tries = 30;
      int rep = 10;
  
  
      std::vector&lt;int&gt; Timings(tries, 0);
      {
          Eigen::Matrix3Xf A(3, SIZE);
  #pragma loop( 1 )
          for (int iter = 0; iter &lt; tries; ++iter)
          {
              auto start1 = std::chrono::system_clock::now();
              eigen_matrix(A);
              Timings[iter] = (std::chrono::system_clock::now() - start1).count();
          }
      }
      std::cout &lt;&lt; "eigen matrix Min: " &lt;&lt; *std::min_element(Timings.begin(), Timings.end()) &lt;&lt; " ms\n";
      std::cout &lt;&lt; "eigen matrix Mean: " &lt;&lt; std::accumulate(Timings.begin(), Timings.end(), 0) / tries &lt;&lt; " ms\n";
  
      {
          Eigen::Matrix3Xf A(3, SIZE);
  #pragma loop( 1 )
          for (int iter = 0; iter &lt; tries; ++iter)
          {
              auto start1 = std::chrono::system_clock::now();
              eigen_matrix_unrolled&lt;4&gt;(A);
              Timings[iter] = (std::chrono::system_clock::now() - start1).count();
          }
      }
      std::cout &lt;&lt; "eigen matrix unrolled 4 min: " &lt;&lt; *std::min_element(Timings.begin(), Timings.end()) &lt;&lt; " ms\n";
      std::cout &lt;&lt; "eigen matrix unrolled 4 Mean: " &lt;&lt; std::accumulate(Timings.begin(), Timings.end(), 0) / tries &lt;&lt; " ms\n";
  
      {
          Eigen::Matrix3Xf A(3, SIZE);
  #pragma loop( 1 )
          for (int iter = 0; iter &lt; tries; ++iter)
          {
              auto start1 = std::chrono::system_clock::now();
              eigen_matrix_unrolled&lt;8&gt;(A);
              Timings[iter] = (std::chrono::system_clock::now() - start1).count();
          }
      }
      std::cout &lt;&lt; "eigen matrix unrolled 8 min: " &lt;&lt; *std::min_element(Timings.begin(), Timings.end()) &lt;&lt; " ms\n";
      std::cout &lt;&lt; "eigen matrix unrolled 8 Mean: " &lt;&lt; std::accumulate(Timings.begin(), Timings.end(), 0) / tries &lt;&lt; " ms\n";
  
      {
          std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt; A(SIZE, Eigen::AlignedVector3&lt;float&gt;(0, 0, 0));
  #pragma loop( 1 )
          for (int iter = 0; iter &lt; tries; ++iter)
          {
              auto start1 = std::chrono::system_clock::now();
              vector_no_unroll(A);
              Timings[iter] = (std::chrono::system_clock::now() - start1).count();
          }
      }
      std::cout &lt;&lt; "eigen vector min: " &lt;&lt; *std::min_element(Timings.begin(), Timings.end()) &lt;&lt; " ms\n";
      std::cout &lt;&lt; "eigen vector Mean: " &lt;&lt; std::accumulate(Timings.begin(), Timings.end(), 0) / tries &lt;&lt; " ms\n";
  
      {
          std::vector&lt;Eigen::AlignedVector3&lt;float&gt;&gt; A(SIZE, Eigen::AlignedVector3&lt;float&gt;(0, 0, 0));
  #pragma loop( 1 )
          for (int iter = 0; iter &lt; tries; ++iter)
          {
              auto start1 = std::chrono::system_clock::now();
              vector_unrolled(A);
              Timings[iter] = (std::chrono::system_clock::now() - start1).count();
          }
      }
      std::cout &lt;&lt; "eigen vector unrolled min: " &lt;&lt; *std::min_element(Timings.begin(), Timings.end()) &lt;&lt; " ms\n";
      std::cout &lt;&lt; "eigen vector unrolled Mean: " &lt;&lt; std::accumulate(Timings.begin(), Timings.end(), 0) / tries &lt;&lt; " ms\n";
  
  }
  </code></pre>
  
  <p>And checked the results on 8 diffrent machines (all windows) and get following results</p>
  
  <p>eigen matrix Min: 110477 ms </p>
  
  <p>eigen matrix Mean: 131691 ms</p>
  
  <p>eigen matrix unrolled 4 min: 40099 ms</p>
  
  <p>eigen matrix unrolled 4 Mean: 54812 ms</p>
  
  <p>eigen matrix unrolled 8 min: 40001 ms</p>
  
  <p>eigen matrix unrolled 8 Mean: 51482 ms</p>
  
  <p>eigen vector min: 100270 ms</p>
  
  <p>eigen vector Mean: 117316 ms</p>
  
  <p>eigen vector unrolled min: 59966 ms</p>
  
  <p>eigen vector unrolled Mean: 65847 ms</p>
  
  <p>On every machine I tested, exepted one with was the oldest. Looks like on new machines small unrolling can be quite beneficial ( results differs form 1.5 to 3.5 times speed up on 4x unrolled and do not incrise even if unrolling was for 8,16,32, or 256 time).</p>