I would like to implement a parallel version of the code below using threads in OpenMP,is there any better way to do this?
/* Program to compute Pi using Monte Carlo methods */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define SEED 35791246
int main(int argc, char* argv)
{
int niter=0;
double x,y;
int i,count=0; /* # of points in the 1st quadrant of unit circle */
double z;
double pi;
clock_t end_time, start_time;
printf("Enter the number of iterations used to estimate pi: ");
scanf("%d",&niter);
start_time = clock();
/* initialize random numbers */
srand(SEED);
count=0;
#pragma omp parallel for
for ( i=0; i<niter; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
#pragma omp task
pi=(double)count/niter*4;
#pragma omp barrier
end_time = clock();
printf("# of trials= %d , estimate of pi is %g, time= %f \n",niter,pi, difftime(end_time, start_time));
return 0;
}