GCC Code Coverage Report


Directory: ./
File: tasks/tsibareva_e_integral_calculate_trapezoid_method/all/src/ops_all.cpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 62 64 96.9%
Functions: 5 6 83.3%
Branches: 34 64 53.1%

Line Branch Exec Source
1 #include "tsibareva_e_integral_calculate_trapezoid_method/all/include/ops_all.hpp"
2
3 #include <mpi.h>
4
5 #include <cmath>
6 #include <functional>
7 #include <vector>
8
9 #include "tsibareva_e_integral_calculate_trapezoid_method/common/include/common.hpp"
10
11 namespace tsibareva_e_integral_calculate_trapezoid_method {
12
13
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 TsibarevaEIntegralCalculateTrapezoidMethodALL::TsibarevaEIntegralCalculateTrapezoidMethodALL(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 GetInput() = in;
16 12 GetOutput() = 0.0;
17 12 }
18
19 12 bool TsibarevaEIntegralCalculateTrapezoidMethodALL::ValidationImpl() {
20 12 return true;
21 }
22
23 12 bool TsibarevaEIntegralCalculateTrapezoidMethodALL::PreProcessingImpl() {
24 12 GetOutput() = 0.0;
25 12 return true;
26 }
27
28 double TsibarevaEIntegralCalculateTrapezoidMethodALL::ComputePartialSum(
29 int begin, int finish, const std::vector<double> &lo, const std::vector<double> &h, const std::vector<int> &sizes,
30 const std::vector<int> &steps, int dim, const std::function<double(const std::vector<double> &)> &f) {
31 double partial = 0.0;
32 12 #pragma omp parallel default(none) shared(begin, finish, dim, h, sizes, lo, steps, f) reduction(+ : partial)
33 {
34 std::vector<double> point(dim);
35 #pragma omp for
36 for (int node = begin; node < finish; ++node) {
37 int remainder_idx = node;
38 double node_weight = 1.0;
39 for (int i = dim - 1; i >= 0; --i) {
40 int idx = remainder_idx % sizes[i];
41 remainder_idx /= sizes[i];
42 if (idx == 0 || idx == steps[i]) {
43 node_weight *= 0.5;
44 }
45 point[i] = lo[i] + (idx * h[i]);
46 }
47 partial += node_weight * f(point);
48 }
49 }
50 return partial;
51 }
52
53 12 bool TsibarevaEIntegralCalculateTrapezoidMethodALL::RunImpl() {
54 12 int rank = 0;
55 12 int size = 0;
56 12 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57 12 MPI_Comm_size(MPI_COMM_WORLD, &size);
58
59 12 const auto &lo = GetInput().lo;
60 12 const auto &hi = GetInput().hi;
61 12 const auto &steps = GetInput().steps;
62 12 const auto &f = GetInput().f;
63 12 int dim = GetInput().dim;
64
65 12 MPI_Bcast(&dim, 1, MPI_INT, 0, MPI_COMM_WORLD);
66 12 std::vector<double> lo_vec(dim);
67
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<double> hi_vec(dim);
68
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<int> steps_vec(dim);
69
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (rank == 0) {
70
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 lo_vec = lo;
71
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 hi_vec = hi;
72
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 steps_vec = steps;
73 }
74
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Bcast(lo_vec.data(), dim, MPI_DOUBLE, 0, MPI_COMM_WORLD);
75
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Bcast(hi_vec.data(), dim, MPI_DOUBLE, 0, MPI_COMM_WORLD);
76
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Bcast(steps_vec.data(), dim, MPI_INT, 0, MPI_COMM_WORLD);
77
78
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<double> h(dim);
79
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<int> sizes(dim);
80 int total_nodes = 1;
81
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
36 for (int i = 0; i < dim; ++i) {
82 24 h[i] = (hi_vec[i] - lo_vec[i]) / static_cast<double>(steps_vec[i]);
83 24 sizes[i] = steps_vec[i] + 1;
84 24 total_nodes *= sizes[i];
85 }
86
87
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<int> all_starts(size);
88
1/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 std::vector<int> all_ends(size);
89
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (rank == 0) {
90 6 int nodes_per_proc = total_nodes / size;
91 6 int remainder = total_nodes % size;
92 int start = 0;
93
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int i = 0; i < size; ++i) {
94
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 all_starts[i] = start;
95
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
18 int end = start + nodes_per_proc + (i < remainder ? 1 : 0);
96 12 all_ends[i] = end;
97 start = end;
98 }
99 }
100
101 12 int my_start = 0;
102
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 int my_end = 0;
103
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Scatter(all_starts.data(), 1, MPI_INT, &my_start, 1, MPI_INT, 0, MPI_COMM_WORLD);
104
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Scatter(all_ends.data(), 1, MPI_INT, &my_end, 1, MPI_INT, 0, MPI_COMM_WORLD);
105
106 12 double local_sum = ComputePartialSum(my_start, my_end, lo_vec, h, sizes, steps_vec, dim, f);
107
108 12 double global_sum = 0.0;
109
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
110
111
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (rank == 0) {
112 double res_h = 1.0;
113
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int i = 0; i < dim; ++i) {
114 12 res_h *= h[i];
115 }
116 6 GetOutput() = global_sum * res_h;
117 }
118
119 12 return true;
120 }
121
122 12 bool TsibarevaEIntegralCalculateTrapezoidMethodALL::PostProcessingImpl() {
123 12 MPI_Bcast(&GetOutput(), 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
124 12 MPI_Barrier(MPI_COMM_WORLD);
125 12 return true;
126 }
127
128 } // namespace tsibareva_e_integral_calculate_trapezoid_method
129