From c28495fc7dd1b010ffd171ec452b5ed2127f0008 Mon Sep 17 00:00:00 2001 From: Neil Gershenfeld <gersh@cba.mit.edu> Date: Sun, 1 Mar 2020 19:58:19 -0500 Subject: [PATCH] wip --- C++/threadpi.cpp | 38 ++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 39 insertions(+) create mode 100755 C++/threadpi.cpp diff --git a/C++/threadpi.cpp b/C++/threadpi.cpp new file mode 100755 index 0000000..0b6e285 --- /dev/null +++ b/C++/threadpi.cpp @@ -0,0 +1,38 @@ +// +// threadpi.cpp +// Neil Gershenfeld 3/1/20 +// calculation of pi by a C++ thread sum +// pi = 3.14159265358979323846 +// +#include <iostream> +#include <chrono> +#include <thread> +#include <vector> +unsigned int npts = 2e7; +unsigned int nthreads = std::thread::hardware_concurrency(); +std::vector<double> results(nthreads); +void sum(int index) { + unsigned int start = npts*index+1; + unsigned int end = npts*(index+1)+1; + double result = 0; + for (unsigned int i = start; i < end; ++i) + result += 0.5/((i-0.75)*(i-0.25)); + results[index] = result; + } +int main(void) { + double pi = 0; + std::thread threads[nthreads]; + auto tstart = std::chrono::high_resolution_clock::now(); + for (int i = 0; i < nthreads; ++i) + threads[i] = std::thread(sum,i); + for (int i = 0; i < nthreads; ++i) { + threads[i].join(); + pi += results[i]; + } + auto tend = std::chrono::high_resolution_clock::now(); + auto dt = std::chrono::duration_cast<std::chrono::microseconds>(tend-tstart).count(); + auto mflops = npts*nthreads*5.0/dt; + std::cout << "npts: " << npts << " nthreads: " << nthreads << " pi: " << pi << '\n'; + std::cout << "time: " << 1e-6*dt << " estimated MFlops: " << mflops << '\n'; + return 0; + } diff --git a/README.md b/README.md index 9539bfd..a4ce0b0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ |1,090|[numbapig.py](Python/numbapig.py)|Python, Numba, CUDA, 5120 cores|NVIDIA V100|March 1, 2020| |315|[numbapip.py](Python/numbapip.py)|Python, Numba, parallel, fastmath<br>96 cores|Intel 2x Xeon Platinum 8175M|Feb 7, 2020| |272|[threadpi.c](C/threadpi.c)|C, 96 threads<br>gcc threadpi.c -o threadpi -O3 -ffast-math -pthread|Intel 2x Xeon Platinum 8175M|Jun 3, 2019| +|267|[threadpi.cpp](C++/threadpi.cpp)|C++, 96 threads<br>g++ threadpi.cpp -o threadpi -O3 -ffast-math -pthread|Intel 2x Xeon Platinum 8175M|Mar 1, 2020| |211|[mpipi2.c](MPI/mpipi2.c)|C, MPI, 1 node, 96 cores<br>mpicc mpipi2.c -o mpipi2 -O3 -ffast-math|Intel 2x Xeon Platinum 8175M|Oct 24, 2019| |180|[mpipi2.py](Python/mpipi2.py)|Python, Numba, MPI<br>mpirun -np 96 python mpipi2.py|Intel 2x Xeon Platinum 8175M|Feb 6, 2020| |173|[mppi.c](OpenMP/mppi.c)|C, OpenMP, 96 threads<br>gcc mppi.c -o mppi -O3 -ffast-math -fopenmp|Intel 2x Xeon Platinum 8175M|Jul 1, 2019| -- GitLab