| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_monte_carlo_integration/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <thread> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "shilin_n_monte_carlo_integration/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace shilin_n_monte_carlo_integration { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | ShilinNMonteCarloIntegrationSTL::ShilinNMonteCarloIntegrationSTL(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | 64 | GetOutput() = 0.0; | |
| 16 | 64 | } | |
| 17 | |||
| 18 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | bool ShilinNMonteCarloIntegrationSTL::ValidationImpl() { |
| 19 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 20 |
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()) { |
| 21 | return false; | ||
| 22 | } | ||
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (n <= 0) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (size_t i = 0; i < lower.size(); ++i) { |
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (lower[i] >= upper[i]) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 | } | ||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { |
| 32 | return false; | ||
| 33 | } | ||
| 34 | constexpr size_t kMaxDimensions = 10; | ||
| 35 | 64 | return lower.size() <= kMaxDimensions; | |
| 36 | } | ||
| 37 | |||
| 38 | 64 | bool ShilinNMonteCarloIntegrationSTL::PreProcessingImpl() { | |
| 39 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 40 | 64 | lower_bounds_ = lower; | |
| 41 | 64 | upper_bounds_ = upper; | |
| 42 | 64 | num_points_ = n; | |
| 43 | 64 | func_type_ = func_type; | |
| 44 | 64 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 | 64 | bool ShilinNMonteCarloIntegrationSTL::RunImpl() { | |
| 48 | 64 | auto dimensions = static_cast<int>(lower_bounds_.size()); | |
| 49 | |||
| 50 | const std::vector<double> alpha = { | ||
| 51 | 0.41421356237309504, // frac(sqrt(2)) | ||
| 52 | 0.73205080756887729, // frac(sqrt(3)) | ||
| 53 | 0.23606797749978969, // frac(sqrt(5)) | ||
| 54 | 0.64575131106459059, // frac(sqrt(7)) | ||
| 55 | 0.31662479035539984, // frac(sqrt(11)) | ||
| 56 | 0.60555127546398929, // frac(sqrt(13)) | ||
| 57 | 0.12310562561766059, // frac(sqrt(17)) | ||
| 58 | 0.35889894354067355, // frac(sqrt(19)) | ||
| 59 | 0.79583152331271838, // frac(sqrt(23)) | ||
| 60 | 0.38516480713450403 // frac(sqrt(29)) | ||
| 61 | 64 | }; | |
| 62 | |||
| 63 | 64 | unsigned int num_threads = std::thread::hardware_concurrency(); | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (num_threads == 0) { |
| 65 | ✗ | num_threads = 2; | |
| 66 | } | ||
| 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 | 256 | auto worker = [&](unsigned int tid) { | |
| 72 | 256 | std::vector<double> point(dimensions); | |
| 73 |
2/2✓ Branch 0 taken 3360000 times.
✓ Branch 1 taken 256 times.
|
3360256 | 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 | 320 | }; | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (unsigned int ti = 0; ti < num_threads; ++ti) { |
| 84 |
2/4✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 256 times.
|
256 | threads[ti] = std::thread(worker, ti); |
| 85 | } | ||
| 86 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (auto &th : threads) { |
| 87 |
1/2✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
|
256 | th.join(); |
| 88 | } | ||
| 89 | |||
| 90 | double sum = 0.0; | ||
| 91 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 64 times.
|
320 | for (unsigned int ti = 0; ti < num_threads; ++ti) { |
| 92 | 256 | 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 |