| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "galkin_d_multidim_integrals_rectangles/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/blocked_range.h> | ||
| 4 | #include <tbb/parallel_reduce.h> | ||
| 5 | |||
| 6 | #include <climits> | ||
| 7 | #include <cmath> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <functional> | ||
| 11 | #include <limits> | ||
| 12 | #include <tuple> | ||
| 13 | #include <utility> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #include "galkin_d_multidim_integrals_rectangles/common/include/common.hpp" | ||
| 17 | |||
| 18 | namespace galkin_d_multidim_integrals_rectangles { | ||
| 19 | |||
| 20 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | GalkinDMultidimIntegralsRectanglesTBB::GalkinDMultidimIntegralsRectanglesTBB(const InType &in) { |
| 21 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 22 | GetInput() = in; | ||
| 23 | 92 | GetOutput() = 0.0; | |
| 24 | 92 | } | |
| 25 | |||
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | bool GalkinDMultidimIntegralsRectanglesTBB::ValidationImpl() { |
| 27 | const auto &[func, borders, n] = GetInput(); | ||
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (borders.empty()) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 | |||
| 32 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 92 times.
|
252 | for (const auto &[left_border, right_border] : borders) { |
| 33 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 160 times.
|
160 | if (!std::isfinite(left_border) || !std::isfinite(right_border)) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (left_border >= right_border) { |
| 37 | return false; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 92 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 92 times.
|
92 | return func && (n > 0) && (GetOutput() == 0.0); |
| 42 | } | ||
| 43 | |||
| 44 | 92 | bool GalkinDMultidimIntegralsRectanglesTBB::PreProcessingImpl() { | |
| 45 | 92 | GetOutput() = 0.0; | |
| 46 | 92 | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | 92 | bool GalkinDMultidimIntegralsRectanglesTBB::RunImpl() { | |
| 50 | const InType &input = GetInput(); | ||
| 51 | const auto &func = std::get<0>(input); | ||
| 52 | const auto &borders = std::get<1>(input); | ||
| 53 | 92 | const int n = std::get<2>(input); | |
| 54 | 92 | const std::size_t dim = borders.size(); | |
| 55 | |||
| 56 | 92 | std::vector<double> h(dim); | |
| 57 | double cell_v = 1.0; | ||
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 92 times.
|
252 | for (std::size_t i = 0; i < dim; ++i) { |
| 60 | 160 | const double left_border = borders[i].first; | |
| 61 | 160 | const double right_border = borders[i].second; | |
| 62 | |||
| 63 |
1/2✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
|
160 | h[i] = (right_border - left_border) / static_cast<double>(n); |
| 64 |
2/4✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 160 times.
|
160 | if (!(h[i] > 0.0) || !std::isfinite(h[i])) { |
| 65 | return false; | ||
| 66 | } | ||
| 67 | |||
| 68 | 160 | cell_v *= h[i]; | |
| 69 | } | ||
| 70 | |||
| 71 | std::size_t total_cells = 1; | ||
| 72 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 92 times.
|
252 | for (std::size_t i = 0; i < dim; ++i) { |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (total_cells > (std::numeric_limits<std::size_t>::max() / static_cast<std::size_t>(n))) { |
| 74 | return false; | ||
| 75 | } | ||
| 76 | 160 | total_cells *= static_cast<std::size_t>(n); | |
| 77 | } | ||
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
|
92 | if (total_cells > static_cast<std::size_t>(LLONG_MAX)) { |
| 79 | return false; | ||
| 80 | } | ||
| 81 | |||
| 82 | const auto total_cells_i64 = static_cast<std::int64_t>(total_cells); | ||
| 83 | |||
| 84 | 184 | double sum = tbb::parallel_reduce(tbb::blocked_range<std::int64_t>(0, total_cells_i64), 0.0, | |
| 85 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | [&](const tbb::blocked_range<std::int64_t> &r, double local_sum) { |
| 86 | 23404 | std::vector<double> x(dim); | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 10203000 times.
✓ Branch 1 taken 23404 times.
|
10226404 | for (std::int64_t linear_idx = r.begin(); linear_idx != r.end(); ++linear_idx) { |
| 89 | 10203000 | auto tmp = static_cast<std::size_t>(linear_idx); | |
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 33182200 times.
✓ Branch 1 taken 10203000 times.
|
43385200 | for (std::size_t i = 0; i < dim; ++i) { |
| 92 | 33182200 | const std::size_t idx_i = tmp % static_cast<std::size_t>(n); | |
| 93 | 33182200 | tmp /= static_cast<std::size_t>(n); | |
| 94 | 33182200 | x[i] = borders[i].first + ((static_cast<double>(idx_i) + 0.5) * h[i]); | |
| 95 | } | ||
| 96 | |||
| 97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10203000 times.
|
20406000 | local_sum += func(x); |
| 98 | } | ||
| 99 | 23404 | return local_sum; | |
| 100 | ✗ | }, std::plus<>()); | |
| 101 | |||
| 102 | 92 | GetOutput() = sum * cell_v; | |
| 103 | |||
| 104 | 92 | return std::isfinite(GetOutput()); | |
| 105 | } | ||
| 106 | |||
| 107 | 92 | bool GalkinDMultidimIntegralsRectanglesTBB::PostProcessingImpl() { | |
| 108 | 92 | return std::isfinite(GetOutput()); | |
| 109 | } | ||
| 110 | |||
| 111 | } // namespace galkin_d_multidim_integrals_rectangles | ||
| 112 |