Skip to content
Snippets Groups Projects
Commit 6053c6a5 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

wip

parent c181be81
Branches
No related tags found
No related merge requests found
Pipeline #2889 passed
/*
* 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);
}
......@@ -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|
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment