| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_monte_carlo_integration/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <functional> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "oneapi/tbb/blocked_range.h" | ||
| 10 | #include "oneapi/tbb/global_control.h" | ||
| 11 | #include "oneapi/tbb/parallel_reduce.h" | ||
| 12 | #include "shilin_n_monte_carlo_integration/common/include/common.hpp" | ||
| 13 | #include "util/include/util.hpp" | ||
| 14 | |||
| 15 | namespace shilin_n_monte_carlo_integration { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | ShilinNMonteCarloIntegrationTBB::ShilinNMonteCarloIntegrationTBB(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 | GetInput() = in; | ||
| 20 | 32 | GetOutput() = 0.0; | |
| 21 | 32 | } | |
| 22 | |||
| 23 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | bool ShilinNMonteCarloIntegrationTBB::ValidationImpl() { |
| 24 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 25 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
|
32 | if (lower.size() != upper.size() || lower.empty()) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (n <= 0) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
|
96 | for (size_t i = 0; i < lower.size(); ++i) { |
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (lower[i] >= upper[i]) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | } | ||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { |
| 37 | return false; | ||
| 38 | } | ||
| 39 | constexpr size_t kMaxDimensions = 10; | ||
| 40 | 32 | return lower.size() <= kMaxDimensions; | |
| 41 | } | ||
| 42 | |||
| 43 | 32 | bool ShilinNMonteCarloIntegrationTBB::PreProcessingImpl() { | |
| 44 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 45 | 32 | lower_bounds_ = lower; | |
| 46 | 32 | upper_bounds_ = upper; | |
| 47 | 32 | num_points_ = n; | |
| 48 | 32 | func_type_ = func_type; | |
| 49 | 32 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 | 32 | bool ShilinNMonteCarloIntegrationTBB::RunImpl() { | |
| 53 | 32 | auto dimensions = static_cast<int>(lower_bounds_.size()); | |
| 54 | |||
| 55 | const std::vector<double> alpha = { | ||
| 56 | 0.41421356237309504, // frac(sqrt(2)) | ||
| 57 | 0.73205080756887729, // frac(sqrt(3)) | ||
| 58 | 0.23606797749978969, // frac(sqrt(5)) | ||
| 59 | 0.64575131106459059, // frac(sqrt(7)) | ||
| 60 | 0.31662479035539984, // frac(sqrt(11)) | ||
| 61 | 0.60555127546398929, // frac(sqrt(13)) | ||
| 62 | 0.12310562561766059, // frac(sqrt(17)) | ||
| 63 | 0.35889894354067355, // frac(sqrt(19)) | ||
| 64 | 0.79583152331271838, // frac(sqrt(23)) | ||
| 65 | 0.38516480713450403 // frac(sqrt(29)) | ||
| 66 | 32 | }; | |
| 67 | |||
| 68 | // Honor PPC_NUM_THREADS so reports can construct a real T-scaling table. | ||
| 69 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | const int num_threads = std::max(1, ppc::util::GetNumThreads()); |
| 70 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | tbb::global_control global_limit(tbb::global_control::max_allowed_parallelism, static_cast<std::size_t>(num_threads)); |
| 71 | // Explicit grain prevents oneTBB from over-splitting a very light per-iteration body. | ||
| 72 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | const int grain = std::max(1024, num_points_ / (num_threads * 4)); |
| 73 | |||
| 74 | 64 | double sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, num_points_, grain), 0.0, | |
| 75 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | [&](const tbb::blocked_range<int> &range, double local_sum) { |
| 76 | 352 | std::vector<double> point(dimensions); | |
| 77 |
2/2✓ Branch 0 taken 1680000 times.
✓ Branch 1 taken 352 times.
|
1680352 | for (int i = range.begin(); i < range.end(); ++i) { |
| 78 |
2/2✓ Branch 0 taken 4080000 times.
✓ Branch 1 taken 1680000 times.
|
5760000 | for (int di = 0; di < dimensions; ++di) { |
| 79 | 4080000 | double val = 0.5 + (static_cast<double>(i + 1) * alpha[di]); | |
| 80 | 4080000 | double current = val - std::floor(val); | |
| 81 | 4080000 | point[di] = lower_bounds_[di] + ((upper_bounds_[di] - lower_bounds_[di]) * current); | |
| 82 | } | ||
| 83 | 1680000 | local_sum += IntegrandFunction::Evaluate(func_type_, point); | |
| 84 | } | ||
| 85 | 352 | return local_sum; | |
| 86 | ✗ | }, std::plus<>()); | |
| 87 | |||
| 88 | double volume = 1.0; | ||
| 89 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
|
96 | for (int di = 0; di < dimensions; ++di) { |
| 90 | 64 | volume *= (upper_bounds_[di] - lower_bounds_[di]); | |
| 91 | } | ||
| 92 | |||
| 93 | 32 | GetOutput() = volume * sum / static_cast<double>(num_points_); | |
| 94 | 32 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | 32 | bool ShilinNMonteCarloIntegrationTBB::PostProcessingImpl() { | |
| 98 | 32 | return true; | |
| 99 | } | ||
| 100 | |||
| 101 | } // namespace shilin_n_monte_carlo_integration | ||
| 102 |