mppi.c 796 Bytes
Newer Older
Neil Gershenfeld's avatar
Neil Gershenfeld committed
1
2
3
/*
* mppi.c
* Neil Gershenfeld 6/21/19
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
4
* OpenMP pi calculation benchmark
Neil Gershenfeld's avatar
Neil Gershenfeld committed
5
6
7
8
9
10
* pi = 3.14159265358979323846
*/

#include <stdio.h>
#include <time.h>
#include <omp.h>
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
11
#include <stdint.h>
Neil Gershenfeld's avatar
Neil Gershenfeld committed
12

Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
13
#define NPTS 100000000000
Neil Gershenfeld's avatar
Neil Gershenfeld committed
14
15

void main() {
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
16
   uint64_t i;
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
17
   double a,b,c,pi,dt,mflops;
Neil Gershenfeld's avatar
Neil Gershenfeld committed
18
19
20
21
22
23
24
   struct timespec tstart,tend;
   clock_gettime(CLOCK_REALTIME,&tstart);
   a = 0.5;
   b = 0.75;
   c = 0.25;
   pi = 0;
   pi = 0;
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
25
   #pragma omp parallel for reduction(+:pi)
Neil Gershenfeld's avatar
Neil Gershenfeld committed
26
27
28
29
30
   for (i = 1; i <= NPTS; ++i)
      pi += a/((i-b)*(i-c));
   clock_gettime(CLOCK_REALTIME,&tend);
   dt = (tend.tv_sec+tend.tv_nsec/1e9)-(tstart.tv_sec+tstart.tv_nsec/1e9);
   mflops = NPTS*5.0/(dt*1e6);
Neil Gershenfeld's avatar
wip    
Neil Gershenfeld committed
31
   printf("NPTS = %ld, pi = %f, threads = %d\n",NPTS,pi,omp_get_max_threads());
Neil Gershenfeld's avatar
Neil Gershenfeld committed
32
33
   printf("time = %f, estimated MFlops = %f\n",dt,mflops);
   }