| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kosolapov_v_calc_mult_integrals_m_simpson/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <tuple> | ||
| 7 | |||
| 8 | #include "kosolapov_v_calc_mult_integrals_m_simpson/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace kosolapov_v_calc_mult_integrals_m_simpson { | ||
| 11 | |||
| 12 | 16 | KosolapovVCalcMultIntegralsMSimpsonMPI::KosolapovVCalcMultIntegralsMSimpsonMPI(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | 16 | GetInput() = InType(in); | |
| 15 | GetOutput() = 0.0; | ||
| 16 | 16 | } | |
| 17 | |||
| 18 | 16 | bool KosolapovVCalcMultIntegralsMSimpsonMPI::ValidationImpl() { | |
| 19 | 16 | int rank = 0; | |
| 20 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 23 | 8 | int steps = std::get<0>(GetInput()); | |
| 24 | 8 | int func_id = std::get<1>(GetInput()); | |
| 25 |
3/6✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
|
8 | return steps > 0 && (steps % 2 == 0) && func_id >= 1 && func_id <= 4; |
| 26 | } | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | |||
| 30 | 16 | bool KosolapovVCalcMultIntegralsMSimpsonMPI::PreProcessingImpl() { | |
| 31 | 16 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 16 | bool KosolapovVCalcMultIntegralsMSimpsonMPI::RunImpl() { | |
| 35 | 16 | int processes_count = 0; | |
| 36 | 16 | int rank = 0; | |
| 37 | 16 | MPI_Comm_size(MPI_COMM_WORLD, &processes_count); | |
| 38 | 16 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 39 | 16 | int steps = 0; | |
| 40 | 16 | int func_id = 0; | |
| 41 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 42 | 8 | steps = std::get<0>(GetInput()); | |
| 43 | 8 | func_id = std::get<1>(GetInput()); | |
| 44 | } | ||
| 45 | 16 | MPI_Bcast(&steps, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 46 | 16 | MPI_Bcast(&func_id, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 47 | 16 | int rows_per_process = (steps + 1) / processes_count; | |
| 48 | 16 | int remainder = (steps + 1) % processes_count; | |
| 49 | |||
| 50 | int start_i = 0; | ||
| 51 | int end_i = 0; | ||
| 52 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank < remainder) { |
| 53 | 8 | start_i = rank * (rows_per_process + 1); | |
| 54 | 8 | end_i = start_i + rows_per_process; | |
| 55 | } else { | ||
| 56 | 8 | start_i = (remainder * (rows_per_process + 1)) + ((rank - remainder) * rows_per_process); | |
| 57 | 8 | end_i = start_i + rows_per_process - 1; | |
| 58 | } | ||
| 59 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == processes_count - 1) { |
| 60 | end_i = steps; | ||
| 61 | } | ||
| 62 | 16 | std::tuple<double, double, double, double> temp = GetBounds(func_id); | |
| 63 | 16 | double a = std::get<0>(temp); | |
| 64 | 16 | double b = std::get<1>(temp); | |
| 65 | 16 | double c = std::get<2>(temp); | |
| 66 | 16 | double d = std::get<3>(temp); | |
| 67 | 16 | double local_sum = ComputePartialSimpsonIntegral(func_id, steps, a, b, c, d, start_i, end_i); | |
| 68 | 16 | double global_sum = 0.0; | |
| 69 | 16 | MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); | |
| 70 | 16 | double result = 0.0; | |
| 71 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | if (rank == 0) { |
| 72 | 8 | double hx = (b - a) / steps; | |
| 73 | 8 | double hy = (d - c) / steps; | |
| 74 | 8 | result = global_sum * (hx * hy) / 9.0; | |
| 75 | } | ||
| 76 | 16 | MPI_Bcast(&result, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 77 | 16 | GetOutput() = result; | |
| 78 | 16 | return true; | |
| 79 | } | ||
| 80 | |||
| 81 | 16 | bool KosolapovVCalcMultIntegralsMSimpsonMPI::PostProcessingImpl() { | |
| 82 | 16 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | ✗ | double KosolapovVCalcMultIntegralsMSimpsonMPI::Function1(double x, double y) { | |
| 86 | // f(x,y) = x^2 + y^2 | ||
| 87 | 2122 | return (x * x) + (y * y); | |
| 88 | } | ||
| 89 | ✗ | double KosolapovVCalcMultIntegralsMSimpsonMPI::Function2(double x, double y) { | |
| 90 | // f(x,y) = sin(x) * cos(y) | ||
| 91 | 2122 | return std::sin(x) * std::cos(y); | |
| 92 | } | ||
| 93 | ✗ | double KosolapovVCalcMultIntegralsMSimpsonMPI::Function3(double x, double y) { | |
| 94 | // f(x,y) = exp(-(x^2 + y^2)) | ||
| 95 | 2122 | return std::exp(-((x * x) + (y * y))); | |
| 96 | } | ||
| 97 | ✗ | double KosolapovVCalcMultIntegralsMSimpsonMPI::Function4(double x, double y) { | |
| 98 | // f(x,y) = sin(x + y) | ||
| 99 | 2122 | return std::sin(x + y); | |
| 100 | } | ||
| 101 | 8488 | double KosolapovVCalcMultIntegralsMSimpsonMPI::CallFunction(int func_id, double x, double y) { | |
| 102 |
4/5✓ Branch 0 taken 2122 times.
✓ Branch 1 taken 2122 times.
✓ Branch 2 taken 2122 times.
✓ Branch 3 taken 2122 times.
✗ Branch 4 not taken.
|
8488 | switch (func_id) { |
| 103 | case 1: | ||
| 104 | 2122 | return Function1(x, y); | |
| 105 | case 2: | ||
| 106 | 2122 | return Function2(x, y); | |
| 107 | case 3: | ||
| 108 | 2122 | return Function3(x, y); | |
| 109 | case 4: | ||
| 110 | 2122 | return Function4(x, y); | |
| 111 | default: | ||
| 112 | ✗ | return Function1(x, y); | |
| 113 | } | ||
| 114 | } | ||
| 115 | 16 | std::tuple<double, double, double, double> KosolapovVCalcMultIntegralsMSimpsonMPI::GetBounds(int func_id) { | |
| 116 |
4/5✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
16 | switch (func_id) { |
| 117 | 4 | case 1: | |
| 118 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 119 | 4 | case 2: | |
| 120 | return {0.0, kPi, 0.0, kPi / 2.0}; | ||
| 121 | 4 | case 3: | |
| 122 | return {-1.0, 1.0, -1.0, 1.0}; | ||
| 123 | 4 | case 4: | |
| 124 | return {0.0, kPi, 0.0, kPi}; | ||
| 125 | ✗ | default: | |
| 126 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | 16 | double KosolapovVCalcMultIntegralsMSimpsonMPI::ComputePartialSimpsonIntegral(int func_id, int steps, double a, double b, | |
| 130 | double c, double d, int start_i, | ||
| 131 | int end_i) { | ||
| 132 | 16 | double hx = (b - a) / steps; | |
| 133 | 16 | double hy = (d - c) / steps; | |
| 134 | double local_sum = 0.0; | ||
| 135 |
2/2✓ Branch 0 taken 248 times.
✓ Branch 1 taken 16 times.
|
264 | for (int i = start_i; i <= end_i; i++) { |
| 136 | 248 | double x = a + (i * hx); | |
| 137 | double wx = GetSimpsonWeight(i, steps); | ||
| 138 |
2/2✓ Branch 0 taken 8488 times.
✓ Branch 1 taken 248 times.
|
8736 | for (int j = 0; j <= steps; j++) { |
| 139 | 8488 | double y = c + (j * hy); | |
| 140 | double wy = GetSimpsonWeight(j, steps); | ||
| 141 | 8488 | local_sum += (wx * wy) * CallFunction(func_id, x, y); | |
| 142 | } | ||
| 143 | } | ||
| 144 | 16 | return local_sum; | |
| 145 | } | ||
| 146 | ✗ | double KosolapovVCalcMultIntegralsMSimpsonMPI::GetSimpsonWeight(int index, int steps) { | |
| 147 |
4/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 7992 times.
✓ Branch 5 taken 496 times.
|
8736 | if (index == 0 || index == steps) { |
| 148 | return 1.0; | ||
| 149 | } | ||
| 150 |
4/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 112 times.
✓ Branch 4 taken 4120 times.
✓ Branch 5 taken 3872 times.
|
8224 | return (index % 2 == 0) ? 2.0 : 4.0; |
| 151 | } | ||
| 152 | |||
| 153 | } // namespace kosolapov_v_calc_mult_integrals_m_simpson | ||
| 154 |