diff --git a/C++/threadpi.cpp b/C++/threadpi.cpp new file mode 100755 index 0000000000000000000000000000000000000000..0b6e285787be77bcba1ae1d6932eacb7a891c4f9 --- /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 9539bfdd7bc5f94c3a236ad0212e518ea5452a07..a4ce0b087d155aff1d262730057ab5eb8b73783a 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|