/* Program to compute Pi using Monte Carlo methods */
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SEED 35

main(int argc, char* argv)
{
    int niter=0;
    double x,y;
    int i,tid,count=0; /* # of points in the 1st quadrant of unit circle */
    double z;
    double pi;
    time_t rawtime;
    struct tm * timeinfo;

    printf("Enter the number of iterations used to estimate pi: ");
    scanf("%d",&niter);
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "The current date/time is: %s", asctime (timeinfo) );

    /* initialize random numbers */
    srand(SEED);


#pragma omp parallel  for  private(x,y,z,tid) reduction(+:count) 
    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++;
        if (i==(niter/6)-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
        if (i==(niter/3)-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
        if (i==(niter/2)-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
        if (i==(2*niter/3)-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
        if (i==(5*niter/6)-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
        if (i==niter-1) {
            tid = omp_get_thread_num();
            printf(" thread %i just did iteration %i the count is %i\n",tid,i,count);
        }
    }
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "The current date/time is: %s", asctime (timeinfo) );
    printf(" the total count is %i\n",count);
    pi=(double)count/niter*4;
    printf("# of trials= %d , estimate of pi is %g \n",niter,pi);
    return 0;

}