title: OpenMP: cannot change the value of reduction variable
body: <p>After I had read that the initial value of reduction variable is set according to the operator used for reduction, I decided that instead of remembering these default values it is better to initialize it explicitly. So I modified the code in <a href="https://stackoverflow.com/questions/978222/openmp-c-algorithms-for-min-max-median-average">question by Totonga</a> as follows</p>
  
  <pre><code>const int num_steps = 100000;
  double x, sum, dx = 1./num_steps;
  
  #pragma omp parallel private(x) reduction(+:sum)
  {
    sum = 0.;
    #pragma omp for schedule(static)
    for (int i=0; i&lt;num_steps; ++i)
    {
      x = (i+0.5)*dx;
      sum += 4./(1.+x*x);
    }
  }
  </code></pre>
  
  <p>But it turns out that no matter whether I write sum = 0. or sum = 123.456 the code produces the same result (used gcc-4.5.2 compiler). Can somebody, please, explain me why? (with a reference to openmp standard, if possible) Thanks in advance to everybody.</p>
  
  <p>P.S. since some people object initializing reduction variable, I think it makes sense to expand a question a little. The code below works as expected: I initialize reduction variable and obtain result, which DOES depend on MY initial value</p>
  
  <pre><code>int sum;
  #pragma omp parallel reduction(+:sum)
  {
    sum = 1;
  }
  printf("Reduction sum = %d\n",sum);
  </code></pre>
  
  <p>The printed result will be the number of cores, and not 0.</p>
  
  <p>P.P.S I have to update my question again. User Gilles gave an insightful comment: <em>And upon exit of the parallel region, these local values will be reduced using the + operator, and with the initial value of the variable, prior to entering the section.</em></p>
  
  <p>Well, the following code gives me the result <code>3.142592653598146</code>, which is badly calculated <code>pi</code> instead of expected <code>103.141592653598146</code> (the initial code was giving me excellent value of <code>pi=3.141592653598146</code>)</p>
  
  <pre><code>const int num_steps = 100000;
  double x, sum, dx = 1./num_steps;
  
  sum = 100.;
  #pragma omp parallel private(x) reduction(+:sum)
  {
    #pragma omp for schedule(static)
    for (int i=0; i&lt;num_steps; ++i)
    {
      x = (i+0.5)*dx;
      sum += 4./(1.+x*x);
    }
  }
  </code></pre>