title: OpenMP matrix multiplication nested loops
body: <p>This is a matrix multiplication code with one <code>i</code> loop parallelized and another with <code>j</code> loop parallelized. With both the versions the value of <code>C</code> array is correct (I have tested with small matrix sizes). There is also no performance improvement from one over other.</p>
  <p>Can anyone please tell me what is the difference in these 2 versions? Will the array <code>C</code> be accurate in both the versions regardless of the size of the matrix? Thanks in advance</p>
  
  <pre class="lang-c prettyprint-override"><code>void mat_multiply ( void )
  {
      int t;
      int i, j, k;    
      #pragma omp parallel for private(k) // parallelize i loop
      for(i = 0; i &lt; dimension; i++)
      {
          for(j = 0; j &lt; dimension; j++) 
          {
              for(k = 0; k &lt; dimension; k++)
              {
                  C[dimension*i+j] += A[dimension*i+k] *  B[dimension*k+j];       
              }
          }
      }
   }
  </code></pre>
  <hr />
  <pre class="lang-c prettyprint-override"><code> void mat_multiply ( void )
   {
       int t;
       int i, j, k;   
  
       for(i = 0; i &lt; dimension; i++)
       {
           #pragma omp parallel for private(k) // parallelize j loop
           for(j = 0; j &lt; dimension; j++) 
           {
               for(k = 0; k &lt; dimension; k++)
               {
                   C[dimension*i+j] += A[dimension*i+k] *  B[dimension*k+j];      
               }
           }
       }
   }
  </code></pre>