| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eremin_v_integrals_monte_carlo/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <oneapi/tbb/parallel_reduce.h> | ||
| 4 | #include <tbb/tbb.h> | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <cmath> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <functional> | ||
| 10 | #include <random> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "eremin_v_integrals_monte_carlo/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace eremin_v_integrals_monte_carlo { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | EreminVIntegralsMonteCarloTBB::EreminVIntegralsMonteCarloTBB(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 | GetInput() = in; | ||
| 20 | 12 | GetOutput() = 0.0; | |
| 21 | 12 | } | |
| 22 | |||
| 23 | 12 | bool EreminVIntegralsMonteCarloTBB::ValidationImpl() { | |
| 24 | const auto &input = GetInput(); | ||
| 25 | |||
| 26 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.samples <= 0) { |
| 27 | return false; | ||
| 28 | } | ||
| 29 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.bounds.empty()) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.func == nullptr) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | |||
| 36 | return std::ranges::all_of(input.bounds, [](const auto &p) { | ||
| 37 | const auto &[a, b] = p; | ||
| 38 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
24 | return (a < b) && (std::abs(a) <= 1e9) && (std::abs(b) <= 1e9); |
| 39 | }); | ||
| 40 | } | ||
| 41 | |||
| 42 | 12 | bool EreminVIntegralsMonteCarloTBB::PreProcessingImpl() { | |
| 43 | 12 | GetOutput() = 0.0; | |
| 44 | 12 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 | 12 | bool EreminVIntegralsMonteCarloTBB::RunImpl() { | |
| 48 | const auto &input = GetInput(); | ||
| 49 | 12 | const auto &bounds = input.bounds; | |
| 50 | 12 | const int samples = input.samples; | |
| 51 | 12 | const auto &func = input.func; | |
| 52 | |||
| 53 | 12 | const std::size_t dimension = bounds.size(); | |
| 54 | |||
| 55 | double volume = 1.0; | ||
| 56 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (const auto &[a, b] : bounds) { |
| 57 | 24 | volume *= (b - a); | |
| 58 | } | ||
| 59 | |||
| 60 | 24 | const double sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, samples), 0.0, | |
| 61 | 12 | [&](const tbb::blocked_range<int> &range, double local_sum) { | |
| 62 | // Thread-local RNG and distributions for this range chunk. | ||
| 63 | 7190 | std::mt19937 local_gen(std::random_device{}() + static_cast<unsigned>(range.begin())); | |
| 64 | |||
| 65 | 3595 | std::vector<std::uniform_real_distribution<double>> local_distributions; | |
| 66 |
1/2✓ Branch 1 taken 3595 times.
✗ Branch 2 not taken.
|
3595 | local_distributions.reserve(dimension); |
| 67 |
2/2✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 3595 times.
|
10795 | for (const auto &[a, b] : bounds) { |
| 68 |
1/2✓ Branch 1 taken 7200 times.
✗ Branch 2 not taken.
|
7200 | local_distributions.emplace_back(a, b); |
| 69 | } | ||
| 70 | |||
| 71 |
1/4✓ Branch 1 taken 3595 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
3595 | std::vector<double> point(dimension); |
| 72 |
2/2✓ Branch 0 taken 12000000 times.
✓ Branch 1 taken 3595 times.
|
12003595 | for (int i = range.begin(); i < range.end(); ++i) { |
| 73 |
2/2✓ Branch 0 taken 24000000 times.
✓ Branch 1 taken 12000000 times.
|
36000000 | for (std::size_t dim = 0; dim < dimension; ++dim) { |
| 74 | 24000000 | point[dim] = local_distributions[dim](local_gen); | |
| 75 | } | ||
| 76 |
1/2✓ Branch 1 taken 12000000 times.
✗ Branch 2 not taken.
|
12000000 | local_sum += func(point); |
| 77 | } | ||
| 78 | 3595 | return local_sum; | |
| 79 | 12 | }, std::plus<double>{}); | |
| 80 | |||
| 81 | 12 | GetOutput() = volume * (sum / static_cast<double>(samples)); | |
| 82 | 12 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | 12 | bool EreminVIntegralsMonteCarloTBB::PostProcessingImpl() { | |
| 86 | 12 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | } // namespace eremin_v_integrals_monte_carlo | ||
| 90 |