#include "stdafx.h"
#include <omp.h>
staticlong 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?