From 6053c6a590c833536b719c73a91b3d8d84b61fb6 Mon Sep 17 00:00:00 2001 From: Neil Gershenfeld <gersh@cba.mit.edu> Date: Mon, 17 Dec 2018 21:41:23 -0500 Subject: [PATCH] wip --- C/threadpi.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 65 insertions(+) create mode 100644 C/threadpi.c diff --git a/C/threadpi.c b/C/threadpi.c new file mode 100644 index 0000000..7af8afa --- /dev/null +++ b/C/threadpi.c @@ -0,0 +1,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); + } diff --git a/README.md b/README.md index 997a1a4..996bc8d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ |---|---|---|---|---| |71.46|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 56 workers|Intel 2x E5-2680|Nov 19, 2018| |46.96|[mpipi.c](MPI/mpipi.c)|C, MPI<br>mpicc mpipi.c -o mpipi -O3 -ffast-math <br> mpirun -np 6 mpipi|Intel i7-8700T|Nov 17, 2018| +|44.61|[threadpi.c](C/threadpi.c)|C, 6 threads<br>gcc threadpi.c -o threadpi -O3 -ffast-math -pthread|Intel i7-8700T|Dec 17, 2018| |16.16|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 6 workers|Intel i7-8700T|Nov 17, 2018| |15.72|[clusterpi.js](Node/clusterpi.js)|Node, 6 workers|Intel i7-8700T|Dec 8, 2018| |9.371|[pi.c](C/pi.c)|C<br>gcc pi.c -o pi -lm -O3 -ffast-math|Intel i7-8700T|Nov 17, 2018| -- GitLab