title: Different results between Debug and Release
body: <p>I have the problem that my code returns different results when comparing debug to release. I checked that both modes use /fp:precise, so that should not be the problem. The main issue I have with this is that the complete image analysis (its an image understanding project) is completely deterministic, there's absolutely nothing random in it.</p>
  
  <p>Another issue with this is the fact that my release build actually <em>always</em> returns the same result (23.014 for the image), while debug returns some random value between 22 and 23, which just should not be. I've already checked whether it may be thread related, but the only part in the algorithm which is multi-threaded returns the precisely same result for both debug and release.</p>
  
  <p>What else may be happening here?</p>
  
  <p><strong>Update1:</strong> The code I now found responsible for this behaviour:</p>
  
  <pre><code>float PatternMatcher::GetSADFloatRel(float* sample, float* compared, int sampleX, int compX, int offX)
  {
      if (sampleX != compX)
      {
          return 50000.0f;
      }
      float result = 0;
  
      float* pTemp1 = sample;
      float* pTemp2 = compared + offX;
  
      float w1 = 0.0f;
      float w2 = 0.0f;
      float w3 = 0.0f;
  
      for(int j = 0; j &lt; sampleX; j ++)
      {
          w1 += pTemp1[j] * pTemp1[j];
          w2 += pTemp1[j] * pTemp2[j];
          w3 += pTemp2[j] * pTemp2[j];
      }               
      float a = w2 / w3;
      result = w3 * a * a - 2 * w2 * a + w1;
      return result / sampleX;
  }
  </code></pre>
  
  <p><strong>Update2:</strong>
  This is not reproducible with 32bit code. While debug and release code will always result in the same value for 32bit, it still is different from the 64bit release version, and the 64bit debug still returns some completely random values.</p>
  
  <p><strong>Update3:</strong>
  Okay, I found it to certainly be caused by OpenMP. When I disable it, it works fine. (both Debug and Release use the same code, and both have OpenMP activated). </p>
  
  <p>Following is the code giving me trouble:</p>
  
  <pre><code>#pragma omp parallel for shared(last, bestHit, cVal, rad, veneOffset)
  for(int r = 0; r &lt; 53; ++r)
  {
      for(int k = 0; k &lt; 3; ++k)
      {
          for(int c = 0; c &lt; 30; ++c)
          {
              for(int o = -1; o &lt;= 1; ++o)
              {
                  /*
                  r: 2.0f - 15.0f, in 53 steps, representing the radius of blood vessel
                  c: 0-29, in steps of 1, representing the absorption value (collagene)
                  iO: 0-2, depending on current radius. Signifies a subpixel offset (-1/3, 0, 1/3)
                  o: since we are not sure we hit the middle, move -1 to 1 pixels along the samples
                  */
  
                  int offset = r * 3 * 61 * 30 + k * 30 * 61 + c * 61 + o + (61 - (4*w+1))/2;
  
                  if(offset &lt; 0 || offset == fSamples.size())
                  {
                      continue;
                  }
                  last = GetSADFloatRel(adapted, &amp;fSamples.at(offset), 4*w+1, 4*w+1, 0);
                  if(bestHit &gt; last)
                  {
                      bestHit = last;
                      rad = (r+8)*0.25f;
                      cVal = c * 2;
                      veneOffset =(-0.5f + (1.0f / 3.0f) * k + (1.0f / 3.0f) / 2.0f);
                      if(fabs(veneOffset) &lt; 0.001)
                          veneOffset = 0.0f;
                  }
                  last = GetSADFloatRel(input, &amp;fSamples.at(offset), w * 4 + 1, w * 4 + 1, 0);
                  if(bestHit &gt; last)
                  {
                      bestHit = last;
                      rad = (r+8)*0.25f;
                      cVal = c * 2;
                      veneOffset = (-0.5f + (1.0f / 3.0f) * k + (1.0f / 3.0f) / 2.0f);
                      if(fabs(veneOffset) &lt; 0.001)
                          veneOffset = 0.0f;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Note: with Release mode and OpenMP activated I get the same result as with deactivating OpenMP. Debug mode and OpenMP activated gets a different result, OpenMP deactivated gets the same result as with Release.</p>