| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_monte_carlo_integration/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "shilin_n_monte_carlo_integration/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace shilin_n_monte_carlo_integration { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | ShilinNMonteCarloIntegrationSTL::ShilinNMonteCarloIntegrationSTL(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 64 | GetOutput() = 0.0; | |
| 18 | 64 | } | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | bool ShilinNMonteCarloIntegrationSTL::ValidationImpl() { |
| 21 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 22 |
2/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
|
64 | if (lower.size() != upper.size() || lower.empty()) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (n <= 0) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (size_t i = 0; i < lower.size(); ++i) { |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (lower[i] >= upper[i]) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 | } | ||
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | constexpr size_t kMaxDimensions = 10; | ||
| 37 | 64 | return lower.size() <= kMaxDimensions; | |
| 38 | } | ||
| 39 | |||
| 40 | 64 | bool ShilinNMonteCarloIntegrationSTL::PreProcessingImpl() { | |
| 41 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 42 | 64 | lower_bounds_ = lower; | |
| 43 | 64 | upper_bounds_ = upper; | |
| 44 | 64 | num_points_ = n; | |
| 45 | 64 | func_type_ = func_type; | |
| 46 | 64 | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | 64 | bool ShilinNMonteCarloIntegrationSTL::RunImpl() { | |
| 50 | 64 | 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 | 64 | }; | |
| 64 | |||
| 65 | // Honor PPC_NUM_THREADS so reports can build a real T-scaling table. | ||
| 66 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | auto num_threads = static_cast<unsigned int>(std::max(1, ppc::util::GetNumThreads())); |
| 67 | |||
| 68 |
1/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
64 | std::vector<double> partial_sums(num_threads, 0.0); |
| 69 |
1/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
64 | std::vector<std::thread> threads(num_threads); |
| 70 | |||
| 71 | 160 | auto worker = [&](unsigned int tid) { | |
| 72 | 160 | std::vector<double> point(dimensions); | |
| 73 |
2/2✓ Branch 0 taken 3360000 times.
✓ Branch 1 taken 160 times.
|
3360160 | for (int i = static_cast<int>(tid); i < num_points_; i += static_cast<int>(num_threads)) { |
| 74 |
2/2✓ Branch 0 taken 8160000 times.
✓ Branch 1 taken 3360000 times.
|
11520000 | for (int di = 0; di < dimensions; ++di) { |
| 75 | 8160000 | double val = 0.5 + (static_cast<double>(i + 1) * alpha[di]); | |
| 76 | 8160000 | double current = val - std::floor(val); | |
| 77 | 8160000 | point[di] = lower_bounds_[di] + ((upper_bounds_[di] - lower_bounds_[di]) * current); | |
| 78 | } | ||
| 79 | 3360000 | partial_sums[tid] += IntegrandFunction::Evaluate(func_type_, point); | |
| 80 | } | ||
| 81 | 224 | }; | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 64 times.
|
224 | for (unsigned int ti = 0; ti < num_threads; ++ti) { |
| 84 |
2/4✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 160 times.
|
160 | threads[ti] = std::thread(worker, ti); |
| 85 | } | ||
| 86 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 64 times.
|
224 | for (auto &th : threads) { |
| 87 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
160 | th.join(); |
| 88 | } | ||
| 89 | |||
| 90 | double sum = 0.0; | ||
| 91 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 64 times.
|
224 | for (unsigned int ti = 0; ti < num_threads; ++ti) { |
| 92 | 160 | sum += partial_sums[ti]; | |
| 93 | } | ||
| 94 | |||
| 95 | double volume = 1.0; | ||
| 96 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int di = 0; di < dimensions; ++di) { |
| 97 | 128 | volume *= (upper_bounds_[di] - lower_bounds_[di]); | |
| 98 | } | ||
| 99 | |||
| 100 | 64 | GetOutput() = volume * sum / static_cast<double>(num_points_); | |
| 101 | 64 | return true; | |
| 102 | 64 | } | |
| 103 | |||
| 104 | 64 | bool ShilinNMonteCarloIntegrationSTL::PostProcessingImpl() { | |
| 105 | 64 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | } // namespace shilin_n_monte_carlo_integration | ||
| 109 |