| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "popova_e_integr_monte_carlo/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "popova_e_integr_monte_carlo/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace popova_e_integr_monte_carlo { | ||
| 11 | |||
| 12 | 20 | PopovaEIntegrMonteCarloMPI::PopovaEIntegrMonteCarloMPI(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | GetOutput() = 0; | ||
| 16 | 20 | } | |
| 17 | |||
| 18 | 20 | bool PopovaEIntegrMonteCarloMPI::ValidationImpl() { | |
| 19 | const auto &[a, b, n, func_id] = GetInput(); | ||
| 20 |
3/6✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
|
20 | return (a < b) && (n > 0) && (func_id >= FuncType::kLinearFunc) && (func_id <= FuncType::kExpFunc); |
| 21 | } | ||
| 22 | |||
| 23 | 20 | bool PopovaEIntegrMonteCarloMPI::PreProcessingImpl() { | |
| 24 | const auto &[a, b, n, func_id] = GetInput(); | ||
| 25 | 20 | a_ = a; | |
| 26 | 20 | b_ = b; | |
| 27 | 20 | point_count_ = n; | |
| 28 | 20 | func_id_ = func_id; | |
| 29 | |||
| 30 | 20 | return true; | |
| 31 | } | ||
| 32 | |||
| 33 | 20 | bool PopovaEIntegrMonteCarloMPI::RunImpl() { | |
| 34 | 20 | int rank = 0; | |
| 35 | 20 | int size = 0; | |
| 36 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 37 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 38 | |||
| 39 | 20 | std::vector<int> points_per_process(size); | |
| 40 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int> displacements(size); |
| 41 | |||
| 42 | 20 | int base_point_count = point_count_ / size; | |
| 43 | 20 | int extra_points = point_count_ % size; | |
| 44 | |||
| 45 | int curr_start_index = 0; | ||
| 46 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
|
60 | for (int i = 0; i < size; ++i) { |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
|
40 | if (i < extra_points) { |
| 48 | ✗ | points_per_process[i] = base_point_count + 1; | |
| 49 | } else { | ||
| 50 | 40 | points_per_process[i] = base_point_count; | |
| 51 | } | ||
| 52 | |||
| 53 | 40 | displacements[i] = curr_start_index; | |
| 54 | 40 | curr_start_index += points_per_process[i]; | |
| 55 | } | ||
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | int local_points_to_process = points_per_process[rank]; |
| 58 | |||
| 59 | 20 | std::vector<double> all_seeds; | |
| 60 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 61 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | all_seeds.resize(point_count_); |
| 62 |
2/2✓ Branch 0 taken 218600 times.
✓ Branch 1 taken 10 times.
|
218610 | for (int i = 0; i < point_count_; ++i) { |
| 63 | 218600 | all_seeds[i] = static_cast<double>(i); | |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 |
2/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
20 | std::vector<double> local_seeds(local_points_to_process); |
| 68 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatterv(all_seeds.data(), points_per_process.data(), displacements.data(), MPI_DOUBLE, local_seeds.data(), |
| 69 | local_points_to_process, MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| 70 | |||
| 71 | 20 | double local_sum = 0.0; | |
| 72 | const double magic_constant = 0.75487766624669276; | ||
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 218600 times.
✓ Branch 1 taken 20 times.
|
218620 | for (int i = 0; i < local_points_to_process; ++i) { |
| 75 | 218600 | double t = std::fmod(local_seeds[i] * magic_constant, 1.0); | |
| 76 | 218600 | double x = a_ + ((b_ - a_) * t); | |
| 77 | |||
| 78 | double fx = 0.0; | ||
| 79 | 218600 | fx = FunctionPair::Function(func_id_, x); | |
| 80 | 218600 | local_sum += fx; | |
| 81 | } | ||
| 82 | |||
| 83 | 20 | double total_sum = 0.0; | |
| 84 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Reduce(&local_sum, &total_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 85 | |||
| 86 | 20 | double integral = 0.0; | |
| 87 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (rank == 0) { |
| 88 | 10 | double average = total_sum / static_cast<double>(point_count_); | |
| 89 | 10 | integral = (b_ - a_) * average; | |
| 90 | } | ||
| 91 | |||
| 92 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Bcast(&integral, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 93 | |||
| 94 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | GetOutput() = integral; |
| 95 | 20 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | 20 | bool PopovaEIntegrMonteCarloMPI::PostProcessingImpl() { | |
| 99 | 20 | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | } // namespace popova_e_integr_monte_carlo | ||
| 103 |