title: Openmp do I have false sharing or Race condition?
body: <p>I'm learning Openmp, and I'm working on compressed sparse row multiplication (datatype <code>std::complex&lt;int&gt;</code>). And I'm getting different execution time each time I run the following function:</p>
  <pre><code>typedef std::vector &lt; std::vector &lt; std::complex &lt; int &gt;&gt;&gt; matrix;
  
  struct CSR {
      std::vector&lt;std::complex&lt;int&gt;&gt; values; //non-zero values
      std::vector&lt;int&gt; row_ptr; //pointers of rows
      std::vector&lt;int&gt; cols_index; //indices of columns
      int rows; //number of rows
      int cols; //number of columns
      int NNZ; //number of non_zero elements
  };
  
  const matrix multiply_omp (const CSR&amp; A,
      const CSR&amp; B) {
      if (A.cols != B.rows)
          throw &quot;Error&quot;;
      CSR B_t = sparse_transpose(B);
      matrix result(A.rows, std::vector &lt; std::complex &lt; int &gt;&gt;(B.rows, 0));
      #pragma omp parallel
      {
          #pragma omp for
          for (int i = 0; i &lt; A.rows; i++) {
              for (int j = A.row_ptr[i]; j &lt; A.row_ptr[i + 1]; j++) {
                  int Ai = A.cols_index[j];
                  std::complex&lt;int&gt; Avalue = A.values[j];
                  for (int k = 0; k &lt; B_t.rows; k++) {
                      std::complex &lt; int &gt; sum(0, 0);
                      for (int l = B_t.row_ptr[k]; l &lt; B_t.row_ptr[k + 1]; l++)
                          if (Ai == B_t.cols_index[l]) {
                              sum += Avalue * B_t.values[l];
                              break;
                          }
                      if (sum != std::complex &lt; int &gt;(0, 0)) {
                          result[i][k] += sum;
                      }
                  }
              }
  
          }
      }
  
      return result;
  }
  
  </code></pre>
  <p>I set a for loop to call the function 10 iterations giving it 1000*1000 matrices and used <code>omp_get_wtime()</code>, and here is the result:</p>
  <pre><code>iteration 1 : 0.751642 s
  iteration 2 : 0.911264 s
  iteration 3 : 1.553695 s
  iteration 4 : 0.761839 s
  iteration 5 : 0.603688 s
  iteration 6 : 0.423919 s
  iteration 7 : 0.423114 s
  iteration 8 : 0.445878 s
  iteration 9 : 0.892305 s
  iteration 10 : 0.918682 s
  </code></pre>
  <p>is that normal? or do I have false sharing or Race condition?</p>