1 /* 2 Copyright (c) 2023 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "common.h" 18 #include "oneapi/tbb/blocked_range.h" 19 #include "oneapi/tbb/global_control.h" 20 #include "oneapi/tbb/parallel_reduce.h" 21 22 struct reduce_body { 23 double my_pi; reduce_bodyreduce_body24 reduce_body() : my_pi(0) {} reduce_bodyreduce_body25 reduce_body(reduce_body& x, tbb::split) : my_pi(0) {} operator ()reduce_body26 void operator()(const tbb::blocked_range<number_t>& r) { 27 my_pi += pi_slice_kernel(r.begin(), r.size()); 28 } joinreduce_body29 void join(const reduce_body& y) { 30 my_pi += y.my_pi; 31 } 32 }; 33 compute_pi_parallel()34double compute_pi_parallel() { 35 step = pi_t(1.0) / num_intervals; 36 37 double ret = 0.0; 38 39 reduce_body body; 40 tbb::parallel_reduce(tbb::blocked_range<number_t>(0, num_intervals), body); 41 42 ret = body.my_pi * step; 43 44 return ret; 45 } 46 47 static std::unique_ptr<tbb::global_control> gc; 48 threading(int p)49threading::threading(int p) { 50 gc.reset(new tbb::global_control(tbb::global_control::max_allowed_parallelism, p)); 51 } 52 ~threading()53threading::~threading() { 54 gc.reset(); 55 } 56