GCC Code Coverage Report


Directory: ./
File: tasks/afanasyev_a_integ_rect_method/all/src/ops_all.cpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 43 43 100.0%
Functions: 8 8 100.0%
Branches: 19 28 67.9%

Line Branch Exec Source
1 #include "afanasyev_a_integ_rect_method/all/include/ops_all.hpp"
2
3 #include <tbb/blocked_range.h>
4 #include <tbb/parallel_reduce.h>
5
6 #include <algorithm>
7 #include <array>
8 #include <cmath>
9 #include <functional>
10 #include <numeric>
11 #include <thread>
12 #include <vector>
13
14 #include "afanasyev_a_integ_rect_method/common/include/common.hpp"
15 #include "util/include/util.hpp"
16
17 namespace afanasyev_a_integ_rect_method {
18 namespace {
19
20 double ExampleIntegrand(const std::array<double, 3> &x) {
21 double s = 0.0;
22
2/2
✓ Branch 0 taken 3240000 times.
✓ Branch 1 taken 1080000 times.
4320000 for (double xi : x) {
23 3240000 s += xi * xi;
24 }
25 1080000 return std::exp(-s);
26 }
27
28 240 double ComputeSumForI(int i, int n, double h) {
29 double sum = 0.0;
30 std::array<double, 3> x{};
31 240 x[0] = (static_cast<double>(i) + 0.5) * h;
32
2/2
✓ Branch 0 taken 14800 times.
✓ Branch 1 taken 240 times.
15040 for (int j = 0; j < n; ++j) {
33 14800 x[1] = (static_cast<double>(j) + 0.5) * h;
34
2/2
✓ Branch 0 taken 1080000 times.
✓ Branch 1 taken 14800 times.
1094800 for (int k = 0; k < n; ++k) {
35 1080000 x[2] = (static_cast<double>(k) + 0.5) * h;
36 1080000 sum += ExampleIntegrand(x);
37 }
38 }
39 240 return sum;
40 }
41
42 } // namespace
43
44 6 AfanasyevAIntegRectMethodALL::AfanasyevAIntegRectMethodALL(const InType &in) {
45 SetTypeOfTask(GetStaticTypeOfTask());
46 6 GetInput() = in;
47 GetOutput() = 0.0;
48 6 }
49
50 6 bool AfanasyevAIntegRectMethodALL::ValidationImpl() {
51 6 return (GetInput() > 0);
52 }
53
54 6 bool AfanasyevAIntegRectMethodALL::PreProcessingImpl() {
55 6 return true;
56 }
57
58 6 bool AfanasyevAIntegRectMethodALL::RunImpl() {
59 6 const int n = GetInput();
60
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (n <= 0) {
61 return false;
62 }
63
64 const int k_dim = 3;
65 6 const double h = 1.0 / static_cast<double>(n);
66 const double volume = std::pow(h, k_dim);
67
68
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 const int num_threads = std::max(1, ppc::util::GetNumThreads());
69 const int worker_count = std::min(num_threads, n);
70
71 6 std::vector<std::thread> workers;
72
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 std::vector<double> local_sums(worker_count, 0.0);
73
74 6 const int chunk_size = n / worker_count;
75 6 const int remainder = n % worker_count;
76 int start_i = 0;
77
78
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int thread_id = 0; thread_id < worker_count; ++thread_id) {
79
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 const int end_i = start_i + chunk_size + (thread_id < remainder ? 1 : 0);
80
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 workers.emplace_back([thread_id, start_i, end_i, n, h, &local_sums]() {
81 24 local_sums[thread_id] = tbb::parallel_reduce(tbb::blocked_range<int>(start_i, end_i), 0.0,
82 252 [&](const tbb::blocked_range<int> &range, double sum) {
83
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 240 times.
480 for (int i = range.begin(); i < range.end(); ++i) {
84 240 sum += ComputeSumForI(i, n, h);
85 }
86 240 return sum;
87 12 }, std::plus<>());
88 12 });
89 start_i = end_i;
90 }
91
92
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (auto &worker : workers) {
93
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 worker.join();
94 }
95
96 const double sum = std::accumulate(local_sums.begin(), local_sums.end(), 0.0);
97
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 GetOutput() = sum * volume;
98 return true;
99 6 }
100
101 6 bool AfanasyevAIntegRectMethodALL::PostProcessingImpl() {
102 6 return true;
103 }
104
105 } // namespace afanasyev_a_integ_rect_method
106