| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "krasnopevtseva_v_monte_carlo_integration/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <random> | ||
| 8 | #include <tuple> | ||
| 9 | |||
| 10 | #include "krasnopevtseva_v_monte_carlo_integration/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace krasnopevtseva_v_monte_carlo_integration { | ||
| 13 | |||
| 14 | 40 | KrasnopevtsevaVMCIntegrationMPI::KrasnopevtsevaVMCIntegrationMPI(const InType &in) { | |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | GetOutput() = 0; | ||
| 18 | 40 | } | |
| 19 | |||
| 20 | 40 | bool KrasnopevtsevaVMCIntegrationMPI::ValidationImpl() { | |
| 21 | const auto &input = GetInput(); | ||
| 22 | 40 | double a = std::get<0>(input); | |
| 23 | 40 | double b = std::get<1>(input); | |
| 24 | 40 | int num_points = std::get<2>(input); | |
| 25 | 40 | std::uint8_t func = std::get<3>(input); | |
| 26 | |||
| 27 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | return (a <= b) && (num_points > 0) && (func <= 3); |
| 28 | } | ||
| 29 | |||
| 30 | 40 | bool KrasnopevtsevaVMCIntegrationMPI::PreProcessingImpl() { | |
| 31 | 40 | GetOutput() = 0.0; | |
| 32 | 40 | return true; | |
| 33 | } | ||
| 34 | |||
| 35 | 40 | bool KrasnopevtsevaVMCIntegrationMPI::RunImpl() { | |
| 36 | const auto &input = GetInput(); | ||
| 37 | 40 | double a = std::get<0>(input); | |
| 38 | 40 | double b = std::get<1>(input); | |
| 39 | 40 | int num_points = std::get<2>(input); | |
| 40 | 40 | std::uint8_t func = std::get<3>(input); | |
| 41 | |||
| 42 | 40 | int rank = 0; | |
| 43 | 40 | int size = 0; | |
| 44 | 40 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 45 | 40 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 46 | 40 | int local_points = num_points / size; | |
| 47 | 40 | int remainder = num_points % size; | |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 36 times.
|
40 | if (rank < remainder) { |
| 50 | 4 | local_points++; | |
| 51 | } | ||
| 52 | 40 | double local_sum = 0.0; | |
| 53 | 80 | std::mt19937 gen(std::random_device{}() + rank); | |
| 54 | std::uniform_real_distribution<double> dis(a, b); | ||
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 720694 times.
✓ Branch 1 taken 40 times.
|
720734 | for (int i = 0; i < local_points; i++) { |
| 57 | double x = dis(gen); | ||
| 58 | 720694 | double fx = FuncSystem::GetFunc(func, x); | |
| 59 | 720694 | local_sum += fx; | |
| 60 | } | ||
| 61 | 40 | double global_sum = 0.0; | |
| 62 | 40 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 63 | 40 | double integral = 0.0; | |
| 64 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
40 | if (rank == 0) { |
| 65 | 20 | integral = (b - a) * global_sum / num_points; | |
| 66 | } | ||
| 67 | 40 | MPI_Bcast(&integral, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 68 | 40 | GetOutput() = integral; | |
| 69 | 40 | return true; | |
| 70 | } | ||
| 71 | |||
| 72 | 40 | bool KrasnopevtsevaVMCIntegrationMPI::PostProcessingImpl() { | |
| 73 | 40 | return true; | |
| 74 | } | ||
| 75 | |||
| 76 | } // namespace krasnopevtseva_v_monte_carlo_integration | ||
| 77 |