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