| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "eremin_v_integrals_monte_carlo/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | #include <omp.h> | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <cmath> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <random> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "eremin_v_integrals_monte_carlo/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace eremin_v_integrals_monte_carlo { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | EreminVIntegralsMonteCarloALL::EreminVIntegralsMonteCarloALL(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 | GetInput() = in; | ||
| 19 | 6 | GetOutput() = 0.0; | |
| 20 | 6 | } | |
| 21 | |||
| 22 | 6 | bool EreminVIntegralsMonteCarloALL::ValidationImpl() { | |
| 23 | 6 | int rank = 0; | |
| 24 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 25 | |||
| 26 | 6 | int valid = 1; | |
| 27 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 28 | const auto &input = GetInput(); | ||
| 29 |
4/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
|
6 | const bool bad = (input.samples <= 0) || input.bounds.empty() || (input.func == nullptr) || |
| 30 | !std::ranges::all_of(input.bounds, [](const auto &p) { | ||
| 31 | const auto &[a, b] = p; | ||
| 32 |
3/6✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | return (a < b) && (std::abs(a) <= 1e9) && (std::abs(b) <= 1e9); |
| 33 | }); | ||
| 34 | 3 | valid = bad ? 0 : 1; | |
| 35 | } | ||
| 36 | |||
| 37 | 6 | MPI_Bcast(&valid, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 38 | 6 | return valid == 1; | |
| 39 | } | ||
| 40 | |||
| 41 | 6 | bool EreminVIntegralsMonteCarloALL::PreProcessingImpl() { | |
| 42 | 6 | GetOutput() = 0.0; | |
| 43 | 6 | return true; | |
| 44 | } | ||
| 45 | |||
| 46 | 6 | bool EreminVIntegralsMonteCarloALL::RunImpl() { | |
| 47 | const auto &input = GetInput(); | ||
| 48 | 6 | const auto &bounds = input.bounds; | |
| 49 | 6 | const int total_samples = input.samples; | |
| 50 | 6 | const auto &func = input.func; | |
| 51 | const std::size_t dimension = bounds.size(); | ||
| 52 | |||
| 53 | 6 | int rank = 0; | |
| 54 | 6 | int num_procs = 1; | |
| 55 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 56 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &num_procs); | |
| 57 | |||
| 58 | double volume = 1.0; | ||
| 59 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (const auto &[a, b] : bounds) { |
| 60 | 12 | volume *= (b - a); | |
| 61 | } | ||
| 62 | |||
| 63 | 6 | const int base_samples = total_samples / num_procs; | |
| 64 | 6 | const int remainder = total_samples % num_procs; | |
| 65 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | const int local_samples = base_samples + (rank == 0 ? remainder : 0); |
| 66 | |||
| 67 | 6 | double local_sum = 0.0; | |
| 68 | |||
| 69 | 6 | #pragma omp parallel reduction(+ : local_sum) default(none) \ | |
| 70 | shared(bounds, local_samples, func, dimension, rank, num_procs) | ||
| 71 | { | ||
| 72 | const unsigned int seed = static_cast<unsigned int>(std::random_device{}()) + | ||
| 73 | (static_cast<unsigned int>(rank) * 1000U) + | ||
| 74 | static_cast<unsigned int>(omp_get_thread_num()); | ||
| 75 | |||
| 76 | std::mt19937 local_gen(seed); | ||
| 77 | |||
| 78 | std::vector<std::uniform_real_distribution<double>> local_dists; | ||
| 79 | local_dists.reserve(dimension); | ||
| 80 | for (const auto &[a, b] : bounds) { | ||
| 81 | local_dists.emplace_back(a, b); | ||
| 82 | } | ||
| 83 | |||
| 84 | std::vector<double> point(dimension); | ||
| 85 | |||
| 86 | #pragma omp for schedule(static) | ||
| 87 | for (int i = 0; i < local_samples; ++i) { | ||
| 88 | for (std::size_t dim = 0; dim < dimension; ++dim) { | ||
| 89 | point[dim] = local_dists[dim](local_gen); | ||
| 90 | } | ||
| 91 | local_sum += func(point); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | 6 | double global_sum = 0.0; | |
| 96 | 6 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 97 | |||
| 98 | 6 | double result = 0.0; | |
| 99 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 100 | 3 | result = volume * (global_sum / static_cast<double>(total_samples)); | |
| 101 | } | ||
| 102 | 6 | MPI_Bcast(&result, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 103 | |||
| 104 | 6 | GetOutput() = result; | |
| 105 | 6 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 6 | bool EreminVIntegralsMonteCarloALL::PostProcessingImpl() { | |
| 109 | 6 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | } // namespace eremin_v_integrals_monte_carlo | ||
| 113 |