| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "frolova_s_mult_int_trapez/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | // #include <iostream> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "frolova_s_mult_int_trapez/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace frolova_s_mult_int_trapez { | ||
| 15 | |||
| 16 | 12 | FrolovaSMultIntTrapezMPI::FrolovaSMultIntTrapezMPI(const InType &in) | |
| 17 |
3/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
|
12 | : BaseTask(), limits_(in.limits), number_of_intervals_(in.number_of_intervals) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = in; |
| 20 | 12 | } | |
| 21 | |||
| 22 | 64986 | unsigned int FrolovaSMultIntTrapezMPI::CalculationOfCoefficient(const std::vector<double> &point) { | |
| 23 | 64986 | auto degree = static_cast<unsigned int>(limits_.size()); | |
| 24 |
2/2✓ Branch 0 taken 189352 times.
✓ Branch 1 taken 64986 times.
|
254338 | for (unsigned int i = 0; i < limits_.size(); i++) { |
| 25 |
4/4✓ Branch 0 taken 183380 times.
✓ Branch 1 taken 5972 times.
✓ Branch 2 taken 5972 times.
✓ Branch 3 taken 177408 times.
|
189352 | if ((std::abs(limits_[i].first - point[i]) < 1e-9) || (std::abs(limits_[i].second - point[i]) < 1e-9)) { |
| 26 | 11944 | degree--; | |
| 27 | } | ||
| 28 | } | ||
| 29 | 64986 | return static_cast<unsigned int>(std::pow(2.0, static_cast<double>(degree))); | |
| 30 | } | ||
| 31 | |||
| 32 | 64986 | void FrolovaSMultIntTrapezMPI::Recursive(std::vector<double> &point, unsigned int &definition) { | |
| 33 | 64986 | unsigned int temp_def = definition; | |
| 34 | |||
| 35 | 64986 | std::vector<unsigned int> divisors(limits_.size(), 1); | |
| 36 |
2/2✓ Branch 0 taken 189352 times.
✓ Branch 1 taken 64986 times.
|
254338 | for (unsigned int i = 0; i < limits_.size(); i++) { |
| 37 |
2/2✓ Branch 0 taken 124366 times.
✓ Branch 1 taken 64986 times.
|
189352 | if (i > 0) { |
| 38 | 124366 | divisors[i] = divisors[i - 1] * (number_of_intervals_[i - 1] + 1); | |
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 189352 times.
✓ Branch 1 taken 64986 times.
|
254338 | for (int i = static_cast<int>(limits_.size()) - 1; i >= 0; i--) { |
| 43 |
1/2✓ Branch 0 taken 189352 times.
✗ Branch 1 not taken.
|
189352 | if (number_of_intervals_[i] != 0) { |
| 44 | 189352 | const auto quotient = temp_def / divisors[i]; | |
| 45 | 189352 | point[i] = limits_[i].first + (static_cast<double>(quotient) * (limits_[i].second - limits_[i].first) / | |
| 46 | 189352 | static_cast<double>(number_of_intervals_[i])); | |
| 47 | } | ||
| 48 | 189352 | temp_def = temp_def % divisors[i]; | |
| 49 | } | ||
| 50 | |||
| 51 |
1/2✓ Branch 0 taken 64986 times.
✗ Branch 1 not taken.
|
64986 | definition = temp_def; |
| 52 | 64986 | } | |
| 53 | |||
| 54 | 64986 | std::vector<double> FrolovaSMultIntTrapezMPI::GetPointFromNumber(unsigned int number) { | |
| 55 | 64986 | std::vector<double> point(limits_.size()); | |
| 56 |
1/2✓ Branch 0 taken 64986 times.
✗ Branch 1 not taken.
|
64986 | unsigned int definition = number; |
| 57 |
1/2✓ Branch 0 taken 64986 times.
✗ Branch 1 not taken.
|
64986 | if (!limits_.empty()) { |
| 58 |
1/2✓ Branch 1 taken 64986 times.
✗ Branch 2 not taken.
|
64986 | Recursive(point, definition); |
| 59 | } | ||
| 60 | 64986 | return point; | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | bool FrolovaSMultIntTrapezMPI::ValidateInputData(const InType &input) { | |
| 64 |
2/8✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
6 | if (input.limits.empty() || input.number_of_intervals.empty()) { |
| 65 | return false; | ||
| 66 | } | ||
| 67 | |||
| 68 |
1/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 | if (input.limits.size() != input.number_of_intervals.size()) { |
| 69 | return false; | ||
| 70 | } | ||
| 71 | |||
| 72 |
1/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 | if (!input.function) { |
| 73 | ✗ | return false; | |
| 74 | } | ||
| 75 | |||
| 76 | return true; | ||
| 77 | } | ||
| 78 | |||
| 79 | 6 | bool FrolovaSMultIntTrapezMPI::ValidateLimitsAndIntervals(const InType &input) { | |
| 80 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (size_t i = 0; i < input.limits.size(); i++) { |
| 81 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.limits[i].first >= input.limits[i].second) { |
| 82 | return false; | ||
| 83 | } | ||
| 84 | |||
| 85 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (input.number_of_intervals[i] == 0) { |
| 86 | return false; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | 12 | bool FrolovaSMultIntTrapezMPI::ValidationImpl() { | |
| 94 | 12 | int rank = 0; | |
| 95 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 98 | 6 | auto input = GetInput(); | |
| 99 | |||
| 100 | if (!ValidateInputData(input)) { | ||
| 101 | ✗ | bool validation_result = false; | |
| 102 | ✗ | MPI_Bcast(&validation_result, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); | |
| 103 | ✗ | return validation_result; | |
| 104 | } | ||
| 105 | |||
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!ValidateLimitsAndIntervals(input)) { |
| 107 | ✗ | bool validation_result = false; | |
| 108 | ✗ | MPI_Bcast(&validation_result, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); | |
| 109 | ✗ | return validation_result; | |
| 110 | } | ||
| 111 | |||
| 112 | 6 | bool validation_result = true; | |
| 113 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Bcast(&validation_result, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); |
| 114 | 6 | return validation_result; | |
| 115 | 6 | } | |
| 116 | |||
| 117 | 6 | bool validation_result = false; | |
| 118 | 6 | MPI_Bcast(&validation_result, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); | |
| 119 | 6 | return validation_result; | |
| 120 | } | ||
| 121 | |||
| 122 | 12 | bool FrolovaSMultIntTrapezMPI::PreProcessingImpl() { | |
| 123 | 12 | int rank = 0; | |
| 124 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 127 | 6 | auto input = GetInput(); | |
| 128 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | limits_ = input.limits; |
| 129 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | number_of_intervals_ = input.number_of_intervals; |
| 130 | 6 | result_ = 0.0; | |
| 131 | 6 | } | |
| 132 | |||
| 133 | 12 | return true; | |
| 134 | } | ||
| 135 | |||
| 136 | 12 | bool FrolovaSMultIntTrapezMPI::RunImpl() { | |
| 137 | 12 | int rank = 0; | |
| 138 | 12 | int size = 0; | |
| 139 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 140 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 141 | |||
| 142 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | int dims = (rank == 0) ? static_cast<int>(limits_.size()) : 0; |
| 143 | 12 | MPI_Bcast(&dims, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 144 | |||
| 145 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank != 0) { |
| 146 | 6 | limits_.resize(dims); | |
| 147 | 6 | number_of_intervals_.resize(dims); | |
| 148 | } | ||
| 149 | |||
| 150 | 12 | MPI_Bcast(limits_.data(), dims * 2, MPI_DOUBLE, 0, MPI_COMM_WORLD); | |
| 151 | 12 | MPI_Bcast(number_of_intervals_.data(), dims, MPI_UNSIGNED, 0, MPI_COMM_WORLD); | |
| 152 | |||
| 153 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (dims == 0) { |
| 154 | return true; | ||
| 155 | } | ||
| 156 | |||
| 157 | std::uint64_t total_points = 1; | ||
| 158 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (unsigned int val : number_of_intervals_) { |
| 159 | 24 | total_points *= (static_cast<std::uint64_t>(val) + 1); | |
| 160 | } | ||
| 161 | |||
| 162 | 12 | auto u_size = static_cast<unsigned int>(size); | |
| 163 | 12 | auto base_delta = static_cast<unsigned int>(total_points / u_size); | |
| 164 | 12 | auto remainder = static_cast<unsigned int>(total_points % u_size); | |
| 165 | |||
| 166 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | unsigned int local_count = base_delta + (std::cmp_less(rank, remainder) ? 1 : 0); |
| 167 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | unsigned int local_start = (rank * base_delta) + (std::cmp_less(rank, remainder) ? rank : remainder); |
| 168 | |||
| 169 | 12 | double local_sum = 0.0; | |
| 170 | 12 | auto func = GetInput().function; | |
| 171 | |||
| 172 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (func != nullptr) { |
| 173 |
2/2✓ Branch 0 taken 64986 times.
✓ Branch 1 taken 12 times.
|
64998 | for (unsigned int i = 0; i < local_count; i++) { |
| 174 |
1/2✓ Branch 1 taken 64986 times.
✗ Branch 2 not taken.
|
64986 | std::vector<double> point = GetPointFromNumber(local_start + i); |
| 175 |
3/8✓ Branch 2 taken 64986 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 64986 times.
✓ Branch 6 taken 64986 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
129972 | local_sum += static_cast<double>(CalculationOfCoefficient(point)) * func(point); |
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Reduce(&local_sum, &result_, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 180 | |||
| 181 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 182 | double step_prod = 1.0; | ||
| 183 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (unsigned int i = 0; i < limits_.size(); i++) { |
| 184 | 12 | step_prod *= (limits_[i].second - limits_[i].first) / static_cast<double>(number_of_intervals_[i]); | |
| 185 | } | ||
| 186 | 6 | result_ *= step_prod / std::pow(2.0, static_cast<double>(limits_.size())); | |
| 187 | } | ||
| 188 | |||
| 189 | return true; | ||
| 190 | } | ||
| 191 | |||
| 192 | 12 | bool FrolovaSMultIntTrapezMPI::PostProcessingImpl() { | |
| 193 | 12 | int rank = 0; | |
| 194 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 195 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 196 | 6 | GetOutput() = result_; | |
| 197 | } | ||
| 198 | 12 | return true; | |
| 199 | } | ||
| 200 | |||
| 201 | } // namespace frolova_s_mult_int_trapez | ||
| 202 |