pi_omp.c

#include <stdio.h>
#include <omp.h>

static long num_steps = 100000;
double step;    
int main (int argc, char *argv[])
{    
int i;
double x, pi, sum = 0.0;    
step = 1.0/(double) num_steps;

  int th_id, nthreads;

#pragma omp parallel private(th_id) 
{
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
      }

#pragma omp for private(x) reduction(+:sum) 
for (i=0;i< num_steps; i++){    
  x = (i+0.5)*step;
  sum = sum + 4.0/(1.0+x*x);    
  }
}
pi = step * sum;
printf(" pi= %10.8f\n", pi);

return 0;
}