parallelizing loop and uninitialized variable

Hello,

I would like to parallelize the code. Currently it is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include <omp.h>

static long num_steps = 100000;
//double step;
void main()
{
    double x, sum;
    double step = 1.0 /( double ) num_steps;
    double pi;
    #pragma omp parallel private(x,sum) //private(x,sum, step)
    {
        int i;
        sum = 0.0;
       
        #pragma omp for schedule(static,1)
        for( i = 1; i <= num_steps; i++ )
        {
            x =( i - 0.5 ) * step;
            sum = sum + 4.0 /( 1.0 + x * x );
        }
        #pragma omp critical
        pi = step * sum;
    }
    printf( "Calculated: %f\n", pi );
    while( 1 ) { if( 'n' == getchar() ) break; }
}


The problem is such that every time I receive "Calculated: 1.570801" what means half of the value that I would like to receive. How could it be corrected?

I would like to try some methods of parallelizing, e.g.:
* SPMD program that uses only parallel region -> I have already done it, I have also added "Yes (/openmp)" in "OpenMP support" in Visual Studio and it displays correct result of calculations so I assume it works
* using work sharing construct (there are more of those) -> this is what I would like to do now (based on third example in http://en.wikipedia.org/wiki/OpenMP#Clauses_in_work-sharing_constructs_.28in_C.2FC.2B.2B.29)
* using reduction, private and work-sharing construct

Could you give me some guidelines about proceeding with the last approach, please?

Regards!
Last edited on
Shouldn't "step" be initialized after line 11, and not before ?
Topic archived. No new replies allowed.