Skip to content

Instantly share code, notes, and snippets.

@oodler577
Forked from bbengfort/pi.c
Created May 14, 2021 04:52
Show Gist options
  • Save oodler577/556e3f15920ac747145f52e9153a6251 to your computer and use it in GitHub Desktop.
Save oodler577/556e3f15920ac747145f52e9153a6251 to your computer and use it in GitHub Desktop.
OpenMP parallel integration to compute Pi.
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_THREADS 8
static long steps = 1000000000;
double step;
int main (int argc, const char *argv[]) {
int i,j;
double x;
double pi, sum = 0.0;
double start, delta;
step = 1.0/(double) steps;
// Compute parallel compute times for 1-MAX_THREADS
for (j=1; j<= MAX_THREADS; j++) {
printf(" running on %d threads: ", j);
// This is the beginning of a single PI computation
omp_set_num_threads(j);
sum = 0.0;
double start = omp_get_wtime();
#pragma omp parallel for reduction(+:sum) private(x)
for (i=0; i < steps; i++) {
x = (i+0.5)*step;
sum += 4.0 / (1.0+x*x);
}
// Out of the parallel region, finialize computation
pi = step * sum;
delta = omp_get_wtime() - start;
printf("PI = %.16g computed in %.4g seconds\n", pi, delta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment