| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "liulin_y_integ_mnog_func_monte_carlo/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <random> | ||
| 7 | |||
| 8 | #include "liulin_y_integ_mnog_func_monte_carlo/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace liulin_y_integ_mnog_func_monte_carlo { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | LiulinYIntegMnogFuncMonteCarloMPI::LiulinYIntegMnogFuncMonteCarloMPI(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | GetInput() = in; | ||
| 15 | 20 | GetOutput() = 0.0; | |
| 16 | 20 | } | |
| 17 | |||
| 18 | 20 | bool LiulinYIntegMnogFuncMonteCarloMPI::ValidationImpl() { | |
| 19 | 20 | int world_rank = 0; | |
| 20 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (world_rank != 0) { |
| 23 | return true; | ||
| 24 | } | ||
| 25 | |||
| 26 | const auto &input = GetInput(); | ||
| 27 |
3/6✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | return input.num_points > 0 && input.x_min <= input.x_max && input.y_min <= input.y_max; |
| 28 | } | ||
| 29 | |||
| 30 | 20 | bool LiulinYIntegMnogFuncMonteCarloMPI::PreProcessingImpl() { | |
| 31 | 20 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 20 | bool LiulinYIntegMnogFuncMonteCarloMPI::RunImpl() { | |
| 35 | const auto &input = GetInput(); | ||
| 36 | auto &result = GetOutput(); | ||
| 37 | |||
| 38 | 20 | int world_size = 0; | |
| 39 | 20 | int world_rank = 0; | |
| 40 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 41 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 42 | |||
| 43 | 20 | int64_t total_points = input.num_points; | |
| 44 | 20 | MPI_Bcast(&total_points, 1, MPI_INT64_T, 0, MPI_COMM_WORLD); | |
| 45 | |||
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (total_points <= 0) { |
| 47 | ✗ | result = 0.0; | |
| 48 | ✗ | return true; | |
| 49 | } | ||
| 50 | |||
| 51 | 20 | double area = (input.x_max - input.x_min) * (input.y_max - input.y_min); | |
| 52 | 20 | MPI_Bcast(&area, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
|
20 | if (area <= 0.0) { |
| 55 | 2 | result = 0.0; | |
| 56 | 2 | return true; | |
| 57 | } | ||
| 58 | |||
| 59 | 18 | int64_t base_points = total_points / world_size; | |
| 60 | 18 | int64_t remainder = total_points % world_size; | |
| 61 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | int64_t local_points = base_points + (world_rank < remainder ? 1 : 0); |
| 62 | |||
| 63 | 18 | std::random_device rd; | |
| 64 | 18 | std::mt19937 gen(rd() + world_rank); | |
| 65 | 18 | std::uniform_real_distribution<double> dist_x(input.x_min, input.x_max); | |
| 66 | 18 | std::uniform_real_distribution<double> dist_y(input.y_min, input.y_max); | |
| 67 | |||
| 68 | 18 | double local_sum = 0.0; | |
| 69 |
2/2✓ Branch 0 taken 4101000 times.
✓ Branch 1 taken 18 times.
|
4101018 | for (int64_t i = 0; i < local_points; ++i) { |
| 70 | double x = dist_x(gen); | ||
| 71 | double y = dist_y(gen); | ||
| 72 | 4101000 | local_sum += input.f(x, y); | |
| 73 | } | ||
| 74 | |||
| 75 | 18 | double global_sum = 0.0; | |
| 76 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (world_rank == 0) { |
| 79 | 9 | result = global_sum / static_cast<double>(total_points) * area; | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&result, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 83 | |||
| 84 | return true; | ||
| 85 | } | ||
| 86 | |||
| 87 | 20 | bool LiulinYIntegMnogFuncMonteCarloMPI::PostProcessingImpl() { | |
| 88 | 20 | return true; | |
| 89 | } | ||
| 90 | |||
| 91 | } // namespace liulin_y_integ_mnog_func_monte_carlo | ||
| 92 |