| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Nazarova_K_rad_sort_batcher_metod/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <future> | ||
| 7 | #include <limits> | ||
| 8 | #include <thread> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "Nazarova_K_rad_sort_batcher_metod/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace nazarova_k_calc_integ_rectangles { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | bool HasValidInput(const InType &input) { |
| 18 | const std::size_t dimension = input.lower_bounds.size(); | ||
| 19 |
4/8✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 56 times.
|
56 | if (!input.function || dimension == 0U || input.upper_bounds.size() != dimension || input.steps.size() != dimension) { |
| 20 | return false; | ||
| 21 | } | ||
| 22 | |||
| 23 | std::size_t total_cells = 1U; | ||
| 24 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 56 times.
|
168 | for (std::size_t i = 0; i < dimension; ++i) { |
| 25 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 112 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 112 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 112 times.
|
112 | if (input.steps[i] == 0U || !std::isfinite(input.lower_bounds[i]) || !std::isfinite(input.upper_bounds[i]) || |
| 26 | input.lower_bounds[i] > input.upper_bounds[i]) { | ||
| 27 | return false; | ||
| 28 | } | ||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (total_cells > (std::numeric_limits<std::size_t>::max() / input.steps[i])) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 | 112 | total_cells *= input.steps[i]; | |
| 33 | } | ||
| 34 | |||
| 35 | return true; | ||
| 36 | } | ||
| 37 | |||
| 38 | 995296 | void FillPointFromLinearIndex(const InType &input, const std::vector<double> &step_sizes, std::size_t linear_index, | |
| 39 | std::vector<double> &point) { | ||
| 40 | std::size_t current_index = linear_index; | ||
| 41 |
2/2✓ Branch 0 taken 2002912 times.
✓ Branch 1 taken 995296 times.
|
2998208 | for (std::size_t axis = 0U; axis < point.size(); ++axis) { |
| 42 | 2002912 | const std::size_t coordinate_index = current_index % input.steps[axis]; | |
| 43 | 2002912 | current_index /= input.steps[axis]; | |
| 44 | 2002912 | point[axis] = input.lower_bounds[axis] + ((static_cast<double>(coordinate_index) + 0.5) * step_sizes[axis]); | |
| 45 | } | ||
| 46 | 995296 | } | |
| 47 | |||
| 48 | } // namespace | ||
| 49 | |||
| 50 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | NazarovaKCalcIntegRectanglesSTL::NazarovaKCalcIntegRectanglesSTL(const InType &in) { |
| 51 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 52 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | GetInput() = in; |
| 53 | 56 | GetOutput() = {}; | |
| 54 | 56 | } | |
| 55 | |||
| 56 | 56 | bool NazarovaKCalcIntegRectanglesSTL::ValidationImpl() { | |
| 57 | 56 | return HasValidInput(GetInput()); | |
| 58 | } | ||
| 59 | |||
| 60 | 56 | bool NazarovaKCalcIntegRectanglesSTL::PreProcessingImpl() { | |
| 61 | const auto &input = GetInput(); | ||
| 62 | 56 | dimension_ = input.lower_bounds.size(); | |
| 63 | 56 | step_sizes_.assign(dimension_, 0.0); | |
| 64 | 56 | cell_volume_ = 1.0; | |
| 65 | 56 | total_cells_ = 1U; | |
| 66 | 56 | result_ = 0.0; | |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 56 times.
|
168 | for (std::size_t i = 0; i < dimension_; ++i) { |
| 69 | 112 | step_sizes_[i] = (input.upper_bounds[i] - input.lower_bounds[i]) / static_cast<double>(input.steps[i]); | |
| 70 | 112 | cell_volume_ *= step_sizes_[i]; | |
| 71 | 112 | total_cells_ *= input.steps[i]; | |
| 72 | } | ||
| 73 | |||
| 74 | 56 | GetOutput() = 0.0; | |
| 75 | 56 | return true; | |
| 76 | } | ||
| 77 | |||
| 78 | 56 | bool NazarovaKCalcIntegRectanglesSTL::RunImpl() { | |
| 79 | const auto &input = GetInput(); | ||
| 80 | 56 | std::size_t thread_count = std::thread::hardware_concurrency(); | |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (thread_count == 0U) { |
| 82 | ✗ | thread_count = 1U; | |
| 83 | } | ||
| 84 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | thread_count = std::min(thread_count, total_cells_ == 0U ? 1U : total_cells_); |
| 85 | |||
| 86 | 56 | const std::size_t base_chunk = total_cells_ / thread_count; | |
| 87 | 56 | const std::size_t remainder = total_cells_ % thread_count; | |
| 88 | |||
| 89 | 56 | std::vector<std::future<double>> futures; | |
| 90 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | futures.reserve(thread_count); |
| 91 | |||
| 92 | std::size_t begin = 0U; | ||
| 93 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 56 times.
|
280 | for (std::size_t thread_id = 0U; thread_id < thread_count; ++thread_id) { |
| 94 |
1/2✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
|
224 | const std::size_t extra = thread_id < remainder ? 1U : 0U; |
| 95 | 224 | const std::size_t end = begin + base_chunk + extra; | |
| 96 |
2/4✓ Branch 1 taken 224 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 224 times.
✗ Branch 5 not taken.
|
448 | futures.emplace_back(std::async(std::launch::async, [&, begin, end] { |
| 97 | 224 | std::vector<double> point(dimension_, 0.0); | |
| 98 | double local_sum = 0.0; | ||
| 99 |
2/2✓ Branch 0 taken 995296 times.
✓ Branch 1 taken 224 times.
|
995520 | for (std::size_t linear_index = begin; linear_index < end; ++linear_index) { |
| 100 | 995296 | FillPointFromLinearIndex(input, step_sizes_, linear_index, point); | |
| 101 | 995296 | local_sum += input.function(point); | |
| 102 | } | ||
| 103 | 224 | return local_sum; | |
| 104 | })); | ||
| 105 | begin = end; | ||
| 106 | } | ||
| 107 | |||
| 108 | double sum = 0.0; | ||
| 109 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 56 times.
|
280 | for (auto &future : futures) { |
| 110 |
1/2✓ Branch 1 taken 224 times.
✗ Branch 2 not taken.
|
224 | sum += future.get(); |
| 111 | } | ||
| 112 | |||
| 113 | 56 | result_ = sum * cell_volume_; | |
| 114 | 56 | GetOutput() = result_; | |
| 115 | 56 | return true; | |
| 116 | 56 | } | |
| 117 | |||
| 118 | 56 | bool NazarovaKCalcIntegRectanglesSTL::PostProcessingImpl() { | |
| 119 | 56 | return true; | |
| 120 | } | ||
| 121 | |||
| 122 | } // namespace nazarova_k_calc_integ_rectangles | ||
| 123 |