GCC Code Coverage Report


Directory: ./
File: tasks/kutergin_v_multidimensional_integration_rect_method/tbb/src/rect_method_tbb.cpp
Date: 2026-04-02 17:12:27
Exec Total Coverage
Lines: 42 43 97.7%
Functions: 6 6 100.0%
Branches: 24 36 66.7%

Line Branch Exec Source
1 #include "../include/rect_method_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 "../../common/include/common.hpp"
12 #include "util/include/util.hpp"
13
14 namespace kutergin_v_multidimensional_integration_rect_method {
15
16
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 RectMethodTBB::RectMethodTBB(const InType &in) {
17 SetTypeOfTask(GetStaticTypeOfTask());
18
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 GetInput() = in;
19 20 GetOutput() = 0.0;
20 20 }
21
22
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 bool RectMethodTBB::ValidationImpl() {
23 const auto &input = GetInput();
24
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (input.limits.size() != input.n_steps.size() || input.limits.empty()) {
25 return false;
26 }
27 return std::ranges::all_of(input.n_steps, [](int n) { return n > 0; });
28 }
29
30 20 bool RectMethodTBB::PreProcessingImpl() {
31 20 local_input_ = GetInput();
32 20 res_ = 0.0;
33 20 return true;
34 }
35
36 20 bool RectMethodTBB::RunImpl() {
37 size_t dims = local_input_.limits.size(); // число размерностей пространства
38
39 // вычисление числа шагов и общего числа итераций
40 size_t total_iterations = 1;
41 20 std::vector<double> h(dims);
42 double d_v = 1.0;
43
44
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (size_t i = 0; i < dims; i++) {
45 60 total_iterations *= local_input_.n_steps[i];
46 60 h[i] = (local_input_.limits[i].second - local_input_.limits[i].first) / local_input_.n_steps[i];
47 60 d_v *= h[i];
48 }
49
50
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 int num_threads = ppc::util::GetNumThreads();
51
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 tbb::global_control global_limit(tbb::global_control::max_allowed_parallelism, num_threads);
52
53 40 double total_sum = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, total_iterations), 0.0,
54
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 [&](const tbb::blocked_range<size_t> &r, double sum) {
55 4887 return sum + CalculateChunkSum(r.begin(), r.end(), h);
56 }, std::plus<>());
57
58 20 res_ = total_sum * d_v;
59 20 return true;
60 }
61
62 20 bool RectMethodTBB::PostProcessingImpl() {
63 20 GetOutput() = res_;
64 20 return true;
65 }
66
67 4887 double RectMethodTBB::CalculateChunkSum(size_t start_idx, size_t end_idx, const std::vector<double> &h) {
68
1/2
✓ Branch 0 taken 4887 times.
✗ Branch 1 not taken.
4887 if (start_idx >= end_idx) {
69 return 0.0;
70 }
71
72 4887 size_t count = end_idx - start_idx;
73 size_t dims = local_input_.limits.size(); // число размерностей пространства
74 4887 std::vector<int> current_indices(dims, 0);
75
1/4
✓ Branch 1 taken 4887 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4887 std::vector<double> coords(dims); // создание вектора координат размером dims
76
77 size_t temp_idx = start_idx;
78
2/2
✓ Branch 0 taken 15709 times.
✓ Branch 1 taken 4887 times.
20596 for (int dim = static_cast<int>(dims) - 1; dim >= 0; --dim) {
79 15709 current_indices[dim] = static_cast<int>(temp_idx % local_input_.n_steps[dim]);
80 15709 temp_idx /= local_input_.n_steps[dim];
81 }
82
83 double chunk_sum = 0.0;
84
85
2/2
✓ Branch 0 taken 45872 times.
✓ Branch 1 taken 4887 times.
50759 for (size_t i = 0; i < count; ++i) {
86
2/2
✓ Branch 0 taken 131260 times.
✓ Branch 1 taken 45872 times.
177132 for (size_t dm = 0; dm < dims; ++dm) {
87 131260 coords[dm] = local_input_.limits[dm].first + ((current_indices[dm] + 0.5) * h[dm]); // реальная координата
88 }
89
90 45872 chunk_sum += local_input_.func(coords); // вычисление функции в точке
91
92
2/2
✓ Branch 0 taken 48852 times.
✓ Branch 1 taken 20 times.
48872 for (int dm = static_cast<int>(dims) - 1; dm >= 0; --dm) {
93
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 45852 times.
48852 current_indices[dm]++;
94
2/2
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 45852 times.
48852 if (current_indices[dm] < local_input_.n_steps[dm]) {
95 break;
96 }
97 3000 current_indices[dm] = 0;
98 }
99 }
100
101 return chunk_sum;
102 }
103
104 } // namespace kutergin_v_multidimensional_integration_rect_method
105