threadpi.c 1.53 KB
Newer Older
Neil Gershenfeld's avatar
wip  
Neil Gershenfeld committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
* threadpi.c
* Neil Gershenfeld 12/17/18
* pi calculation benchmark
* pi = 3.14159265358979323846
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

unsigned int npts = 1e9;
double pi = 0;
pthread_mutex_t mutex;

struct data {
   unsigned int index;
   unsigned int points;
   };

void *fn(void *arg) {
   struct data *vars;
   unsigned int i,start,end;
   double sum = 0;
   double a = 0.5;
   double b = 0.75;
   double c = 0.25;
   vars = (struct data *) arg;
   start = 1+(vars->points)*(vars->index);
   end = (vars->points)*(vars->index+1);
   for (i = start; i <= end; ++i)
      sum += a/((i-b)*(i-c));
   pthread_mutex_lock(&mutex);
   pi += sum;
   pthread_mutex_unlock(&mutex);
   pthread_exit(0);
   }

void main(int argc, char *argv[]) {
   int i,nthreads;
   nthreads = atoi(argv[1]);
   pthread_t threads[nthreads];
   struct data var[nthreads];
   double dt,mflops;
   void *status;
   struct timespec tstart,tend;
   clock_gettime(CLOCK_REALTIME,&tstart);
   for (i = 0; i < nthreads; ++i) {
      var[i].index = i;
      var[i].points = npts;
      pthread_create(&threads[i],NULL,fn,(void *) &var[i]);
      }
   for (i = 0; i < nthreads; ++i) {
      pthread_join(threads[i],&status);
      }
   clock_gettime(CLOCK_REALTIME,&tend);
   dt = (tend.tv_sec+tend.tv_nsec/1e9)-(tstart.tv_sec+tstart.tv_nsec/1e9);
   mflops = nthreads*(npts*5.0/(dt*1e6));
   printf("npts = %d, pi = %f\n",npts,pi);
   printf("time = %f, estimated MFlops = %f\n",dt,mflops);
   pthread_mutex_destroy(&mutex);
   pthread_exit(NULL);
   }