| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eremin_v_integrals_monte_carlo/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <random> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "eremin_v_integrals_monte_carlo/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace eremin_v_integrals_monte_carlo { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | EreminVIntegralsMonteCarloOMP::EreminVIntegralsMonteCarloOMP(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 12 | GetOutput() = 0.0; | |
| 19 | 12 | } | |
| 20 | |||
| 21 | 12 | bool EreminVIntegralsMonteCarloOMP::ValidationImpl() { | |
| 22 | const auto &input = GetInput(); | ||
| 23 | |||
| 24 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.samples <= 0) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.bounds.empty()) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | 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 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); |
| 37 | }); | ||
| 38 | } | ||
| 39 | |||
| 40 | 12 | bool EreminVIntegralsMonteCarloOMP::PreProcessingImpl() { | |
| 41 | 12 | GetOutput() = 0.0; | |
| 42 | 12 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 12 | bool EreminVIntegralsMonteCarloOMP::RunImpl() { | |
| 46 | const auto &input = GetInput(); | ||
| 47 | 12 | const auto &bounds = input.bounds; | |
| 48 | 12 | int samples = input.samples; | |
| 49 | 12 | const auto &func = input.func; | |
| 50 | |||
| 51 | const std::size_t dimension = bounds.size(); | ||
| 52 | |||
| 53 | double volume = 1.0; | ||
| 54 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (const auto &[a, b] : bounds) { |
| 55 | 24 | volume *= (b - a); | |
| 56 | } | ||
| 57 | |||
| 58 | double sum = 0.0; | ||
| 59 | 12 | #pragma omp parallel reduction(+ : sum) default(none) shared(bounds, samples, func, dimension) | |
| 60 | { | ||
| 61 | std::mt19937 local_gen(std::random_device{}() + omp_get_thread_num()); | ||
| 62 | |||
| 63 | std::vector<std::uniform_real_distribution<double>> local_distributions; | ||
| 64 | local_distributions.reserve(dimension); | ||
| 65 | |||
| 66 | for (const auto &[a, b] : bounds) { | ||
| 67 | local_distributions.emplace_back(a, b); | ||
| 68 | } | ||
| 69 | |||
| 70 | std::vector<double> point(dimension); | ||
| 71 | |||
| 72 | #pragma omp for | ||
| 73 | for (int i = 0; i < samples; ++i) { | ||
| 74 | for (std::size_t dim = 0; dim < dimension; ++dim) { | ||
| 75 | point[dim] = local_distributions[dim](local_gen); | ||
| 76 | } | ||
| 77 | |||
| 78 | sum += func(point); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | 12 | GetOutput() = volume * (sum / static_cast<double>(samples)); | |
| 83 | 12 | return true; | |
| 84 | } | ||
| 85 | |||
| 86 | 12 | bool EreminVIntegralsMonteCarloOMP::PostProcessingImpl() { | |
| 87 | 12 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | } // namespace eremin_v_integrals_monte_carlo | ||
| 91 |