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