Skip to content

Instantly share code, notes, and snippets.

Created February 17, 2015 10:59
Show Gist options
  • Select an option

  • Save anonymous/7d3c15a875e786a79349 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/7d3c15a875e786a79349 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <omp.h>
static long num_steps = 1000;
double step,start = 0.0,stop = 0.0;
#define NUM_THREADS 1
int main ()
{
double pi;
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
int i, id,nthrds,nthreads = 0; double x, sum;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
if (id == 0) nthreads = nthrds;
start = omp_get_wtime();
for (i=id, sum=0.0;i< num_steps; i=i+nthreads){
x = (i+0.5)*step;
sum += 4.0/(1.0+x*x);
}
sum = sum*step;
#pragma omp atomic
pi += sum ;
}
stop = omp_get_wtime();
printf("pi = %f",pi);
printf("time = %f",stop-start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment