| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eremin_v_integrals_monte_carlo/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <random> | ||
| 7 | #include <thread> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "eremin_v_integrals_monte_carlo/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace eremin_v_integrals_monte_carlo { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | EreminVIntegralsMonteCarloSTL::EreminVIntegralsMonteCarloSTL(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 24 | GetOutput() = 0.0; | |
| 19 | 24 | } | |
| 20 | |||
| 21 | 24 | bool EreminVIntegralsMonteCarloSTL::ValidationImpl() { | |
| 22 | const auto &input = GetInput(); | ||
| 23 | |||
| 24 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (input.samples <= 0) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (input.bounds.empty()) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (input.func == nullptr) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | |||
| 34 | return std::ranges::all_of(input.bounds, [](const auto &p) { | ||
| 35 | const auto &[a, b] = p; | ||
| 36 |
3/6✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
|
48 | return (a < b) && (std::abs(a) <= 1e9) && (std::abs(b) <= 1e9); |
| 37 | }); | ||
| 38 | } | ||
| 39 | |||
| 40 | 24 | bool EreminVIntegralsMonteCarloSTL::PreProcessingImpl() { | |
| 41 | 24 | GetOutput() = 0.0; | |
| 42 | 24 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 24 | bool EreminVIntegralsMonteCarloSTL::RunImpl() { | |
| 46 | const auto &input = GetInput(); | ||
| 47 | 24 | const auto &bounds = input.bounds; | |
| 48 | 24 | const int samples = input.samples; | |
| 49 | 24 | const auto &func = input.func; | |
| 50 | |||
| 51 | 24 | const std::size_t dimension = bounds.size(); | |
| 52 | |||
| 53 | double volume = 1.0; | ||
| 54 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (const auto &[a, b] : bounds) { |
| 55 | 48 | volume *= (b - a); | |
| 56 | } | ||
| 57 | |||
| 58 | 24 | const int num_threads = std::max(1, ppc::util::GetNumThreads()); | |
| 59 | 24 | std::vector<double> partial_sums(static_cast<std::size_t>(num_threads), 0.0); | |
| 60 | 24 | std::vector<std::thread> threads; | |
| 61 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | threads.reserve(static_cast<std::size_t>(num_threads)); |
| 62 | |||
| 63 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
48 | const unsigned base_seed = std::random_device{}(); |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (int tid = 0; tid < num_threads; ++tid) { |
| 66 | 60 | const int begin = (samples * tid) / num_threads; | |
| 67 | 60 | const int end = (samples * (tid + 1)) / num_threads; | |
| 68 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | threads.emplace_back([&, tid, begin, end]() { |
| 69 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | std::minstd_rand local_gen(base_seed + static_cast<unsigned>(tid)); |
| 70 | 60 | std::vector<std::uniform_real_distribution<double>> local_distributions; | |
| 71 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | local_distributions.reserve(dimension); |
| 72 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 60 times.
|
180 | for (const auto &[a, b] : bounds) { |
| 73 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | local_distributions.emplace_back(a, b); |
| 74 | } | ||
| 75 | |||
| 76 |
1/4✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
60 | std::vector<double> point(dimension); |
| 77 | double local_sum = 0.0; | ||
| 78 |
2/2✓ Branch 0 taken 2400000 times.
✓ Branch 1 taken 60 times.
|
2400060 | for (int i = begin; i < end; ++i) { |
| 79 |
2/2✓ Branch 0 taken 4800000 times.
✓ Branch 1 taken 2400000 times.
|
7200000 | for (std::size_t dim = 0; dim < dimension; ++dim) { |
| 80 | 4800000 | point[dim] = local_distributions[dim](local_gen); | |
| 81 | } | ||
| 82 |
1/2✓ Branch 1 taken 2400000 times.
✗ Branch 2 not taken.
|
2400000 | local_sum += func(point); |
| 83 | } | ||
| 84 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | partial_sums[static_cast<std::size_t>(tid)] = local_sum; |
| 85 | 60 | }); | |
| 86 | } | ||
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (auto &th : threads) { |
| 89 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | th.join(); |
| 90 | } | ||
| 91 | |||
| 92 | double sum = 0.0; | ||
| 93 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (double s : partial_sums) { |
| 94 | 60 | sum += s; | |
| 95 | } | ||
| 96 | |||
| 97 | 24 | GetOutput() = volume * (sum / static_cast<double>(samples)); | |
| 98 | 24 | return true; | |
| 99 | 24 | } | |
| 100 | |||
| 101 | 24 | bool EreminVIntegralsMonteCarloSTL::PostProcessingImpl() { | |
| 102 | 24 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace eremin_v_integrals_monte_carlo | ||
| 106 |