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