title: openmp for loop wrong results for more than one thread
body: <pre><code>double compute_inner_contraction_0( double* X, double* T, int n, int f, int m, int e, int No, int Nv )
  {
      double contraction_value = 0.0;
      int l_begin = 0;
      int l_end   = m;
      for ( int l = l_begin; l &lt; l_end; l++ )
      {
          int k_begin = 0;
          int k_end   = l;
          for ( int k = k_begin; k &lt; k_end; k++ )
          {
              contraction_value += (-1)*X[n * Nv * No * No * No * No + e * No * No * No * No + m * No * No * No + l * No * No + l * No + k]*T[k * Nv + f];
          }
      }
      return contraction_value;
  }
  
  void compute_contraction_0( double* X, double* T, int No, int Nv, double* ExEx_block , int nthd )
  {
      omp_set_dynamic(0);
      omp_set_num_threads(nthd);
      #pragma omp parallel for
      for ( int n = 0; n &lt; No; n++ )
      {
          int f_begin = 0;
          int f_end   = Nv;
          for ( int f = f_begin; f &lt; f_end; f++ )
          {
              int m_begin = 0;
              int m_end   = n;
              for ( int m = m_begin; m &lt; m_end; m++ )
              {
                  int e_begin = 0;
                  int e_end   = f;
                  for ( int e = e_begin; e &lt; e_end; e++ )
                  {
                      ExEx_block[n * Nv * No * Nv + f * No * Nv + m * Nv + e] += compute_inner_contraction_0( X, T, n, f, m, e, No, Nv);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Hi Guys, sorry for the long functions. Please don't worry about the indexing for memory leaks as they've been extensively tested. Basically it is taking numbers in array X and T, and multiplies them, then adds the result to another array ExEx_block. This code would run on multi threads but will give very wrong computation results compared to the result of running on one thread(which is correct). Could you please help? It's built as a shared library (with many other similar functions ) and being loaded from Python wrappers. The OS is CentOS 6 and the compiler is gcc-4.4.7 
  It's a quad Xeon E5-4620 (Sandy-EP) (in total 32 cores) server with 480 GB of DDR3. No, Nv are just integers needed in the calculation. nthd (number of thread) is usually just 10 or 20. The size of these arrays can go up to 17 GB. Thank you very much for your time.</p>