GCC Code Coverage Report


Directory: ./
File: tasks/sabirov_s_monte_carlo/omp/src/ops_omp.cpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 52 52 100.0%
Functions: 6 6 100.0%
Branches: 36 45 80.0%

Line Branch Exec Source
1 #include "sabirov_s_monte_carlo/omp/include/ops_omp.hpp"
2
3 #include <omp.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <cstddef>
8 #include <random>
9 #include <vector>
10
11 #include "sabirov_s_monte_carlo/common/include/common.hpp"
12 #include "util/include/util.hpp"
13
14 namespace sabirov_s_monte_carlo {
15
16 namespace {
17
18 double EvalLinear(const std::vector<double> &point) {
19 double s = 0.0;
20
2/2
✓ Branch 0 taken 440000 times.
✓ Branch 1 taken 240000 times.
680000 for (double x : point) {
21 440000 s += x;
22 }
23 return s;
24 }
25
26 double EvalSumCubes(const std::vector<double> &point) {
27 double s = 0.0;
28
2/2
✓ Branch 0 taken 440000 times.
✓ Branch 1 taken 240000 times.
680000 for (double x : point) {
29 440000 s += x * x * x;
30 }
31 return s;
32 }
33
34 double EvalCosProduct(const std::vector<double> &point) {
35 double p = 1.0;
36
2/2
✓ Branch 0 taken 440000 times.
✓ Branch 1 taken 240000 times.
680000 for (double x : point) {
37 440000 p *= std::cos(x);
38 }
39 return p;
40 }
41
42 double EvalExpNeg(const std::vector<double> &point) {
43 double s = 0.0;
44
2/2
✓ Branch 0 taken 440000 times.
✓ Branch 1 taken 240000 times.
680000 for (double x : point) {
45 440000 s += x;
46 }
47 240000 return std::exp(-s);
48 }
49
50 double EvalMixedPoly(const std::vector<double> &point) {
51 double s = 0.0;
52
2/2
✓ Branch 0 taken 1240000 times.
✓ Branch 1 taken 440000 times.
1680000 for (double x : point) {
53 1240000 s += (x * x) + x;
54 }
55 return s;
56 }
57
58 double EvalSinSum(const std::vector<double> &point) {
59 double s = 0.0;
60
2/2
✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 40000 times.
80000 for (double x : point) {
61 40000 s += std::sin(x);
62 }
63 return s;
64 }
65
66 double EvalSqrtSum(const std::vector<double> &point) {
67 double s = 0.0;
68
2/2
✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 40000 times.
80000 for (double x : point) {
69 40000 s += std::sqrt(x);
70 }
71 return s;
72 }
73
74 double EvalQuarticSum(const std::vector<double> &point) {
75 double s = 0.0;
76
2/2
✓ Branch 0 taken 400000 times.
✓ Branch 1 taken 200000 times.
600000 for (double x : point) {
77 400000 s += x * x * x * x;
78 }
79 return s;
80 }
81
82 1680000 double EvaluateAt(FuncType func_type, const std::vector<double> &point) {
83
8/9
✓ Branch 0 taken 240000 times.
✓ Branch 1 taken 240000 times.
✓ Branch 2 taken 240000 times.
✓ Branch 3 taken 240000 times.
✓ Branch 4 taken 440000 times.
✓ Branch 5 taken 40000 times.
✓ Branch 6 taken 40000 times.
✓ Branch 7 taken 200000 times.
✗ Branch 8 not taken.
1680000 switch (func_type) {
84 case FuncType::kLinear:
85 return EvalLinear(point);
86 case FuncType::kSumCubes:
87 return EvalSumCubes(point);
88 case FuncType::kCosProduct:
89 return EvalCosProduct(point);
90 case FuncType::kExpNeg:
91 240000 return EvalExpNeg(point);
92 case FuncType::kMixedPoly:
93 return EvalMixedPoly(point);
94 case FuncType::kSinSum:
95 return EvalSinSum(point);
96 case FuncType::kSqrtSum:
97 return EvalSqrtSum(point);
98 case FuncType::kQuarticSum:
99 return EvalQuarticSum(point);
100 default:
101 return 0.0;
102 }
103 }
104
105 } // namespace
106
107
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 SabirovSMonteCarloOMP::SabirovSMonteCarloOMP(const InType &in) {
108 SetTypeOfTask(GetStaticTypeOfTask());
109
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 GetInput() = in;
110 52 GetOutput() = 0.0;
111 52 }
112
113
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 bool SabirovSMonteCarloOMP::ValidationImpl() {
114 const auto &in = GetInput();
115
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
52 if (in.lower.size() != in.upper.size() || in.lower.empty()) {
116 return false;
117 }
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (in.num_samples <= 0) {
119 return false;
120 }
121
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 52 times.
132 for (size_t i = 0; i < in.lower.size(); ++i) {
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (in.lower[i] >= in.upper[i]) {
123 return false;
124 }
125 }
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) {
127 return false;
128 }
129 constexpr size_t kMaxDimensions = 10;
130 52 return in.lower.size() <= kMaxDimensions;
131 }
132
133 52 bool SabirovSMonteCarloOMP::PreProcessingImpl() {
134 const auto &in = GetInput();
135 52 lower_ = in.lower;
136 52 upper_ = in.upper;
137 52 num_samples_ = in.num_samples;
138 52 func_type_ = in.func_type;
139 52 GetOutput() = 0.0;
140 52 return true;
141 }
142
143 52 bool SabirovSMonteCarloOMP::RunImpl() {
144 52 omp_set_num_threads(std::max(1, ppc::util::GetNumThreads()));
145
146 52 auto dims = static_cast<int>(lower_.size());
147
148 double volume = 1.0;
149
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 52 times.
132 for (int i = 0; i < dims; ++i) {
150 80 volume *= (upper_[i] - lower_[i]);
151 }
152
153 double sum = 0.0;
154 52 const std::vector<double> *const plower = &lower_;
155 52 const std::vector<double> *const pupper = &upper_;
156 52 const int n_samples = num_samples_;
157 52 const FuncType ftype = func_type_;
158 52 #pragma omp parallel default(none) shared(plower, pupper, n_samples, ftype, dims, volume, sum)
159 {
160 std::random_device rd;
161 std::mt19937 gen(rd());
162 std::vector<std::uniform_real_distribution<double>> dists;
163 dists.reserve(static_cast<size_t>(dims));
164 for (int i = 0; i < dims; ++i) {
165 dists.emplace_back((*plower)[i], (*pupper)[i]);
166 }
167 std::vector<double> point(static_cast<size_t>(dims));
168
169 #pragma omp for reduction(+ : sum) schedule(static)
170 for (int i = 0; i < n_samples; ++i) {
171 for (int j = 0; j < dims; ++j) {
172 point[j] = dists[j](gen);
173 }
174 sum += EvaluateAt(ftype, point);
175 }
176 }
177
178 52 GetOutput() = volume * sum / static_cast<double>(num_samples_);
179 52 return true;
180 }
181
182 52 bool SabirovSMonteCarloOMP::PostProcessingImpl() {
183 52 return true;
184 }
185
186 } // namespace sabirov_s_monte_carlo
187