| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kutuzov_i_simpson_integration/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "kutuzov_i_simpson_integration/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace kutuzov_i_simpson_integration { | ||
| 12 | |||
| 13 | 32 | KutuzovISimpsonIntegrationMPI::KutuzovISimpsonIntegrationMPI(const InType &in) { | |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | GetOutput() = {}; | ||
| 17 | 32 | } | |
| 18 | |||
| 19 | 32 | bool KutuzovISimpsonIntegrationMPI::ValidationImpl() { | |
| 20 | 32 | int n = std::get<0>(GetInput()); | |
| 21 | 32 | double x_min = std::get<0>(std::get<1>(GetInput())); | |
| 22 | 32 | double x_max = std::get<1>(std::get<1>(GetInput())); | |
| 23 | 32 | double y_min = std::get<0>(std::get<2>(GetInput())); | |
| 24 | 32 | double y_max = std::get<1>(std::get<2>(GetInput())); | |
| 25 | 32 | int function_id = std::get<3>(GetInput()); | |
| 26 | |||
| 27 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (x_min >= x_max) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (y_min >= y_max) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 | if (n <= 0 || n % 2 != 0) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (function_id <= 0 || function_id > 4) { |
| 37 | ✗ | return false; | |
| 38 | } | ||
| 39 | return true; | ||
| 40 | } | ||
| 41 | |||
| 42 | 32 | bool KutuzovISimpsonIntegrationMPI::PreProcessingImpl() { | |
| 43 | 32 | GetOutput() = 0.0; | |
| 44 | 32 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 | 32 | bool KutuzovISimpsonIntegrationMPI::RunImpl() { | |
| 48 | 32 | int rank = 0; | |
| 49 | 32 | int process_count = 0; | |
| 50 | 32 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 51 | 32 | MPI_Comm_size(MPI_COMM_WORLD, &process_count); | |
| 52 | |||
| 53 | 32 | int n = 0; | |
| 54 | double x_min = 0.0; | ||
| 55 | double x_max = 0.0; | ||
| 56 | double y_min = 0.0; | ||
| 57 | double y_max = 0.0; | ||
| 58 | 32 | int function_id = 0; | |
| 59 | |||
| 60 | // To reduce the number of MPI_Bcast calls we can send similar data in bulk | ||
| 61 | 32 | std::vector<double> data_package(4, 0.0); | |
| 62 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | if (rank == 0) { |
| 63 | 16 | n = std::get<0>(GetInput()); | |
| 64 | 16 | x_min = std::get<1>(GetInput()).first; | |
| 65 | 16 | x_max = std::get<1>(GetInput()).second; | |
| 66 | 16 | y_min = std::get<2>(GetInput()).first; | |
| 67 | 16 | y_max = std::get<2>(GetInput()).second; | |
| 68 | 16 | function_id = std::get<3>(GetInput()); | |
| 69 | |||
| 70 |
1/4✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
32 | data_package = {x_min, x_max, y_min, y_max}; |
| 71 | } | ||
| 72 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 73 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MPI_Bcast(&function_id, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 74 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MPI_Bcast(data_package.data(), 4, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 75 | 32 | x_min = data_package[0]; | |
| 76 | 32 | x_max = data_package[1]; | |
| 77 | 32 | y_min = data_package[2]; | |
| 78 | 32 | y_max = data_package[3]; | |
| 79 | |||
| 80 | 32 | int task_per_process = (n + 1) / process_count; | |
| 81 | 32 | int tail = (n + 1) - (task_per_process * process_count); | |
| 82 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | int task_start = (task_per_process * rank) + std::min(rank, tail); |
| 83 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | int task_end = task_start + task_per_process + (rank < tail ? 1 : 0); |
| 84 | |||
| 85 | 32 | double step_x = (x_max - x_min) / n; | |
| 86 | 32 | double step_y = (y_max - y_min) / n; | |
| 87 | |||
| 88 | 32 | double local_sum = 0.0; | |
| 89 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 32 times.
|
168 | for (int i = task_start; i < task_end; i++) { |
| 90 | 136 | double x = x_min + (step_x * i); | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 2000 times.
✓ Branch 1 taken 136 times.
|
2136 | for (int j = 0; j <= n; j++) { |
| 93 | 2000 | double y = y_min + (step_y * j); | |
| 94 | 2000 | double a = GetWeight(i, n) * GetWeight(j, n) * CallFunction(function_id, x, y); | |
| 95 | 2000 | local_sum += a; | |
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | 32 | double sum = 0.0; | |
| 100 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MPI_Reduce(&local_sum, &sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 101 | 32 | sum *= step_x * step_y / 9; | |
| 102 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MPI_Bcast(&sum, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
| 103 | 32 | GetOutput() = sum; | |
| 104 | |||
| 105 | 32 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 32 | bool KutuzovISimpsonIntegrationMPI::PostProcessingImpl() { | |
| 109 | 32 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | double KutuzovISimpsonIntegrationMPI::GetWeight(int i, int n) { | |
| 113 |
4/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1728 times.
✓ Branch 3 taken 272 times.
✓ Branch 4 taken 1728 times.
✓ Branch 5 taken 272 times.
|
3728 | if (i == 0 || i == n) { |
| 114 | return 1.0; | ||
| 115 | } | ||
| 116 |
4/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 932 times.
✓ Branch 3 taken 796 times.
✓ Branch 4 taken 932 times.
✓ Branch 5 taken 796 times.
|
3456 | if (i % 2 == 1) { |
| 117 | 1864 | return 4.0; | |
| 118 | } | ||
| 119 | return 2.0; | ||
| 120 | } | ||
| 121 | |||
| 122 | } // namespace kutuzov_i_simpson_integration | ||
| 123 |