GCC Code Coverage Report


Directory: ./
File: tasks/shilin_n_monte_carlo_integration/all/src/ops_all.cpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 39 39 100.0%
Functions: 5 5 100.0%
Branches: 15 24 62.5%

Line Branch Exec Source
1 #include "shilin_n_monte_carlo_integration/all/include/ops_all.hpp"
2
3 #include <mpi.h>
4
5 #include <cmath>
6 #include <cstddef>
7 #include <vector>
8
9 #include "shilin_n_monte_carlo_integration/common/include/common.hpp"
10
11 namespace shilin_n_monte_carlo_integration {
12
13
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 ShilinNMonteCarloIntegrationALL::ShilinNMonteCarloIntegrationALL(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 16 GetOutput() = 0.0;
17 16 }
18
19 16 bool ShilinNMonteCarloIntegrationALL::ValidationImpl() {
20 16 int rank = 0;
21 16 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (rank != 0) {
23 return true;
24 }
25 const auto &[lower, upper, n, func_type] = GetInput();
26
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (lower.size() != upper.size() || lower.empty()) {
27 return false;
28 }
29
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (n <= 0) {
30 return false;
31 }
32
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (size_t i = 0; i < lower.size(); ++i) {
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (lower[i] >= upper[i]) {
34 return false;
35 }
36 }
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) {
38 return false;
39 }
40 constexpr size_t kMaxDimensions = 10;
41 8 return lower.size() <= kMaxDimensions;
42 }
43
44 16 bool ShilinNMonteCarloIntegrationALL::PreProcessingImpl() {
45 const auto &[lower, upper, n, func_type] = GetInput();
46 16 lower_bounds_ = lower;
47 16 upper_bounds_ = upper;
48 16 num_points_ = n;
49 16 func_type_ = func_type;
50 16 return true;
51 }
52
53 16 bool ShilinNMonteCarloIntegrationALL::RunImpl() {
54 16 int rank = 0;
55 16 int num_ranks = 1;
56 16 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57 16 MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
58
59 16 auto dimensions = static_cast<int>(lower_bounds_.size());
60
61 const std::vector<double> alpha = {
62 0.41421356237309504, // frac(sqrt(2))
63 0.73205080756887729, // frac(sqrt(3))
64 0.23606797749978969, // frac(sqrt(5))
65 0.64575131106459059, // frac(sqrt(7))
66 0.31662479035539984, // frac(sqrt(11))
67 0.60555127546398929, // frac(sqrt(13))
68 0.12310562561766059, // frac(sqrt(17))
69 0.35889894354067355, // frac(sqrt(19))
70 0.79583152331271838, // frac(sqrt(23))
71 0.38516480713450403 // frac(sqrt(29))
72 16 };
73
74 int local_count = 0;
75
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (rank < num_points_) {
76 16 local_count = (((num_points_ - rank - 1) / num_ranks) + 1);
77 }
78
79 16 double local_sum = 0.0;
80
81 // MSVC OpenMP does not allow non-static data members in data-sharing clauses; use `this`.
82 auto *self = this;
83 16 #pragma omp parallel default(none) shared(dimensions, alpha, self, rank, num_ranks, local_count) \
84 reduction(+ : local_sum)
85 {
86 std::vector<double> point(dimensions);
87 #pragma omp for schedule(static)
88 for (int k = 0; k < local_count; ++k) {
89 int i = rank + (k * num_ranks);
90 for (int di = 0; di < dimensions; ++di) {
91 double val = 0.5 + (static_cast<double>(i + 1) * alpha[di]);
92 double current = val - std::floor(val);
93 point[di] = self->lower_bounds_[di] + ((self->upper_bounds_[di] - self->lower_bounds_[di]) * current);
94 }
95 local_sum += IntegrandFunction::Evaluate(self->func_type_, point);
96 }
97 }
98
99 16 double global_sum = 0.0;
100
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
101
102 double volume = 1.0;
103
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (int di = 0; di < dimensions; ++di) {
104 32 volume *= (upper_bounds_[di] - lower_bounds_[di]);
105 }
106
107 16 GetOutput() = volume * global_sum / static_cast<double>(num_points_);
108
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 MPI_Barrier(MPI_COMM_WORLD);
109 16 return true;
110 }
111
112 16 bool ShilinNMonteCarloIntegrationALL::PostProcessingImpl() {
113 16 return true;
114 }
115
116 } // namespace shilin_n_monte_carlo_integration
117