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