I'm new to openMP and C, I tried the "Introduction to OpenMP - Tim Mattson (Intel)" Pi example but the outcome is not 3.14. I compare the code with teacher. They are same. But the result is different
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
//OpenMP example program: hello;
static long num_steps = 100000;
#define NUM_THREADS 2
double step;
int main()
{
int nnum,i,j=0;
step= 1.0/(double)num_steps;
double sum[NUM_THREADS];
double x,pi,result=0.0;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
int id=omp_get_thread_num();
int num=omp_get_num_threads();
if(id==0) nnum = num;
for(i=id,sum[id]=0.0;i<num_steps;i=i+num)
{
x=(i+0.5)*step;
sum[id]=sum[id]+(4.0/(1.0+x*x));
}
}
while(j<nnum)
{
printf(" is %2.4f \n",sum[j]);
result=result+sum[j];
j++;
}
pi = step*result;
printf("the result is %f \n",pi);
return 0;
}