Donjan Rodic committed Sep 30, 2016 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 ``````// Example codes for HPC course // (c) 2012 Matthias Troyer, ETH Zurich #include #include #include #include #include // sum terms [i-j) of the power series for pi/4 void sumterms(long double& sum, std::size_t i, std::size_t j) { sum = 0.0; for (std::size_t t = i; t < j; ++t) sum += (1.0 - 2* (t % 2)) / (2*t + 1); } int main() { // decide how many threads to use std::size_t const nthreads = std::max(1u, std::thread::hardware_concurrency()); std::vector threads(nthreads); std::vector results(nthreads); unsigned long const nterms = 100000000; long double const step = (nterms+0.5l) / nthreads; for (unsigned i = 0; i < nthreads; ++i) threads[i] = std::thread(sumterms,std::ref(results[i]), i * step, (i+1) * step); for (std::thread& t : threads) t.join(); long double pi = 4 * std::accumulate(results.begin(), results.end(), 0. ); std::cout << "pi=" << std::setprecision(18) << pi << std::endl; return 0; }``````