GCC Code Coverage Report


Directory: ./
File: tasks/shkrebko_m_calc_of_integral_rect/tbb/src/ops_tbb.cpp
Date: 2026-05-11 08:26:31
Exec Total Coverage
Lines: 48 49 98.0%
Functions: 6 6 100.0%
Branches: 27 42 64.3%

Line Branch Exec Source
1 #include "shkrebko_m_calc_of_integral_rect/tbb/include/ops_tbb.hpp"
2
3 #include <tbb/tbb.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <cstddef>
8 #include <functional>
9 #include <vector>
10
11 #include "shkrebko_m_calc_of_integral_rect/common/include/common.hpp"
12 #include "util/include/util.hpp"
13
14 namespace shkrebko_m_calc_of_integral_rect {
15
16
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 ShkrebkoMCalcOfIntegralRectTBB::ShkrebkoMCalcOfIntegralRectTBB(const InType &in) {
17 SetTypeOfTask(GetStaticTypeOfTask());
18
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 GetInput() = in;
19 40 GetOutput() = 0.0;
20 40 }
21
22
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 bool ShkrebkoMCalcOfIntegralRectTBB::ValidationImpl() {
23 const auto &input = GetInput();
24
25
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (!input.func) {
26 return false;
27 }
28
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 if (input.limits.size() != input.n_steps.size() || input.limits.empty()) {
29 return false;
30 }
31
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (!std::ranges::all_of(input.n_steps, [](int n) { return n > 0; })) {
32 return false;
33 }
34
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (!std::ranges::all_of(input.limits, [](const auto &lim) { return lim.first < lim.second; })) {
35 return false;
36 }
37
38 return true;
39 }
40
41 40 bool ShkrebkoMCalcOfIntegralRectTBB::PreProcessingImpl() {
42 40 local_input_ = GetInput();
43 40 res_ = 0.0;
44 40 return true;
45 }
46
47 40 bool ShkrebkoMCalcOfIntegralRectTBB::RunImpl() {
48 const std::size_t dim = local_input_.limits.size();
49
50 40 std::vector<double> h(dim);
51 double cell_volume = 1.0;
52 std::size_t total_points = 1;
53
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 40 times.
100 for (std::size_t i = 0; i < dim; ++i) {
54 60 const double left = local_input_.limits[i].first;
55 60 const double right = local_input_.limits[i].second;
56 60 const int steps = local_input_.n_steps[i];
57 60 h[i] = (right - left) / static_cast<double>(steps);
58 60 cell_volume *= h[i];
59 60 total_points *= static_cast<std::size_t>(steps);
60 }
61
62
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 int num_threads = ppc::util::GetNumThreads();
63
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 tbb::global_control global_limit(tbb::global_control::max_allowed_parallelism, num_threads);
64
65 80 double total_sum = tbb::parallel_reduce(tbb::blocked_range<std::size_t>(0, total_points), 0.0,
66
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 [&](const tbb::blocked_range<std::size_t> &range, double partial_sum) {
67 8999 return partial_sum + ComputeBlockSum(range.begin(), range.end(), h);
68 }, std::plus<>());
69
70 40 res_ = total_sum * cell_volume;
71 40 return true;
72 }
73
74 40 bool ShkrebkoMCalcOfIntegralRectTBB::PostProcessingImpl() {
75 40 GetOutput() = res_;
76 40 return true;
77 }
78
79 8999 double ShkrebkoMCalcOfIntegralRectTBB::ComputeBlockSum(std::size_t start_idx, std::size_t end_idx,
80 const std::vector<double> &h) const {
81
1/2
✓ Branch 0 taken 8999 times.
✗ Branch 1 not taken.
8999 if (start_idx >= end_idx) {
82 return 0.0;
83 }
84
85 const std::size_t dim = local_input_.limits.size();
86 const auto &limits = local_input_.limits;
87 const auto &n_steps = local_input_.n_steps;
88
89 8999 std::vector<int> indices(dim);
90 std::size_t temp = start_idx;
91
2/2
✓ Branch 0 taken 15322 times.
✓ Branch 1 taken 8999 times.
24321 for (int idx = static_cast<int>(dim) - 1; idx >= 0; --idx) {
92 {
93 15322 indices[idx] = static_cast<int>(temp % static_cast<std::size_t>(n_steps[idx]));
94 15322 temp /= static_cast<std::size_t>(n_steps[idx]);
95 }
96 }
97 double block_sum = 0.0;
98
1/4
✓ Branch 1 taken 8999 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
8999 std::vector<double> point(dim);
99
100
2/2
✓ Branch 0 taken 351200 times.
✓ Branch 1 taken 8999 times.
360199 for (std::size_t iter = 0; iter < (end_idx - start_idx); ++iter) {
101
2/2
✓ Branch 0 taken 954400 times.
✓ Branch 1 taken 351200 times.
1305600 for (std::size_t dim_idx = 0; dim_idx < dim; ++dim_idx) {
102 954400 point[dim_idx] = limits[dim_idx].first + ((static_cast<double>(indices[dim_idx]) + 0.5) * h[dim_idx]);
103 }
104 351200 block_sum += local_input_.func(point);
105
106 int level = static_cast<int>(dim) - 1;
107
2/2
✓ Branch 0 taken 358800 times.
✓ Branch 1 taken 40 times.
358840 while (level >= 0) {
108
2/2
✓ Branch 0 taken 7640 times.
✓ Branch 1 taken 351160 times.
358800 indices[level]++;
109
2/2
✓ Branch 0 taken 7640 times.
✓ Branch 1 taken 351160 times.
358800 if (indices[level] < n_steps[level]) {
110 break;
111 }
112 7640 indices[level] = 0;
113 7640 level--;
114 }
115 }
116
117 return block_sum;
118 }
119
120 } // namespace shkrebko_m_calc_of_integral_rect
121