| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Nazarova_K_rad_sort_batcher_metod/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | #include <tbb/blocked_range.h> | ||
| 5 | #include <tbb/parallel_reduce.h> | ||
| 6 | |||
| 7 | #include <cmath> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <functional> | ||
| 10 | #include <limits> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "Nazarova_K_rad_sort_batcher_metod/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace nazarova_k_calc_integ_rectangles { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | bool HasValidInput(const InType &input) { |
| 20 | const std::size_t dimension = input.lower_bounds.size(); | ||
| 21 |
4/8✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 14 times.
|
14 | if (!input.function || dimension == 0U || input.upper_bounds.size() != dimension || input.steps.size() != dimension) { |
| 22 | return false; | ||
| 23 | } | ||
| 24 | |||
| 25 | std::size_t total_cells = 1U; | ||
| 26 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
|
42 | for (std::size_t i = 0; i < dimension; ++i) { |
| 27 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
|
28 | if (input.steps[i] == 0U || !std::isfinite(input.lower_bounds[i]) || !std::isfinite(input.upper_bounds[i]) || |
| 28 | input.lower_bounds[i] > input.upper_bounds[i]) { | ||
| 29 | return false; | ||
| 30 | } | ||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (total_cells > (std::numeric_limits<std::size_t>::max() / input.steps[i])) { |
| 32 | return false; | ||
| 33 | } | ||
| 34 | 28 | total_cells *= input.steps[i]; | |
| 35 | } | ||
| 36 | |||
| 37 | return true; | ||
| 38 | } | ||
| 39 | |||
| 40 | 124412 | void FillPointFromLinearIndex(const InType &input, const std::vector<double> &step_sizes, std::size_t linear_index, | |
| 41 | std::vector<double> &point) { | ||
| 42 | std::size_t current_index = linear_index; | ||
| 43 |
2/2✓ Branch 0 taken 250364 times.
✓ Branch 1 taken 124412 times.
|
374776 | for (std::size_t axis = 0U; axis < point.size(); ++axis) { |
| 44 | 250364 | const std::size_t coordinate_index = current_index % input.steps[axis]; | |
| 45 | 250364 | current_index /= input.steps[axis]; | |
| 46 | 250364 | point[axis] = input.lower_bounds[axis] + ((static_cast<double>(coordinate_index) + 0.5) * step_sizes[axis]); | |
| 47 | } | ||
| 48 | 124412 | } | |
| 49 | |||
| 50 | } // namespace | ||
| 51 | |||
| 52 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | NazarovaKCalcIntegRectanglesALL::NazarovaKCalcIntegRectanglesALL(const InType &in) { |
| 53 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 54 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | GetInput() = in; |
| 55 | 14 | GetOutput() = {}; | |
| 56 | 14 | } | |
| 57 | |||
| 58 | 14 | bool NazarovaKCalcIntegRectanglesALL::ValidationImpl() { | |
| 59 | 14 | return HasValidInput(GetInput()); | |
| 60 | } | ||
| 61 | |||
| 62 | 14 | bool NazarovaKCalcIntegRectanglesALL::PreProcessingImpl() { | |
| 63 | const auto &input = GetInput(); | ||
| 64 | 14 | dimension_ = input.lower_bounds.size(); | |
| 65 | 14 | step_sizes_.assign(dimension_, 0.0); | |
| 66 | 14 | cell_volume_ = 1.0; | |
| 67 | 14 | total_cells_ = 1U; | |
| 68 | 14 | result_ = 0.0; | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
|
42 | for (std::size_t i = 0; i < dimension_; ++i) { |
| 71 | 28 | step_sizes_[i] = (input.upper_bounds[i] - input.lower_bounds[i]) / static_cast<double>(input.steps[i]); | |
| 72 | 28 | cell_volume_ *= step_sizes_[i]; | |
| 73 | 28 | total_cells_ *= input.steps[i]; | |
| 74 | } | ||
| 75 | |||
| 76 | 14 | GetOutput() = 0.0; | |
| 77 | 14 | return true; | |
| 78 | } | ||
| 79 | |||
| 80 | 14 | bool NazarovaKCalcIntegRectanglesALL::RunImpl() { | |
| 81 | const auto &input = GetInput(); | ||
| 82 | |||
| 83 | 14 | int rank = 0; | |
| 84 | 14 | int size = 1; | |
| 85 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 86 | 14 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 87 | |||
| 88 | 14 | const std::size_t mpi_size = size > 0 ? static_cast<std::size_t>(size) : 1U; | |
| 89 | 14 | const std::size_t mpi_rank = rank >= 0 ? static_cast<std::size_t>(rank) : 0U; | |
| 90 | 14 | const std::size_t base_chunk = total_cells_ / mpi_size; | |
| 91 | 14 | const std::size_t remainder = total_cells_ % mpi_size; | |
| 92 | 14 | const std::size_t local_begin = (mpi_rank * base_chunk) + (mpi_rank < remainder ? mpi_rank : remainder); | |
| 93 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | const std::size_t local_size = base_chunk + (mpi_rank < remainder ? 1U : 0U); |
| 94 | 14 | const std::size_t local_end = local_begin + local_size; | |
| 95 | |||
| 96 | 28 | const double local_sum = tbb::parallel_reduce(tbb::blocked_range<std::size_t>(local_begin, local_end), 0.0, | |
| 97 | 14 | [&](const tbb::blocked_range<std::size_t> &range, double partial_sum) { | |
| 98 | 1480 | std::vector<double> point(dimension_, 0.0); | |
| 99 |
2/2✓ Branch 0 taken 124412 times.
✓ Branch 1 taken 1480 times.
|
125892 | for (std::size_t linear_index = range.begin(); linear_index < range.end(); ++linear_index) { |
| 100 | 124412 | FillPointFromLinearIndex(input, step_sizes_, linear_index, point); | |
| 101 | 124412 | partial_sum += input.function(point); | |
| 102 | } | ||
| 103 | 1480 | return partial_sum; | |
| 104 | 14 | }, std::plus<>()); | |
| 105 | |||
| 106 | 14 | double global_sum = 0.0; | |
| 107 | 14 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); | |
| 108 | |||
| 109 | 14 | result_ = global_sum * cell_volume_; | |
| 110 | 14 | GetOutput() = result_; | |
| 111 | 14 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | 14 | bool NazarovaKCalcIntegRectanglesALL::PostProcessingImpl() { | |
| 115 | 14 | return true; | |
| 116 | } | ||
| 117 | |||
| 118 | } // namespace nazarova_k_calc_integ_rectangles | ||
| 119 |