| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sabirov_s_monte_carlo/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <random> | ||
| 8 | #include <thread> | ||
| 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 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 21 | 880000 | 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 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 29 | 880000 | 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 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 37 | 880000 | 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 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 45 | 880000 | s += x; | |
| 46 | } | ||
| 47 | 480000 | 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 2480000 times.
✓ Branch 1 taken 880000 times.
|
3360000 | for (double x : point) { |
| 53 | 2480000 | 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 80000 times.
✓ Branch 1 taken 80000 times.
|
160000 | for (double x : point) { |
| 61 | 80000 | 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 80000 times.
✓ Branch 1 taken 80000 times.
|
160000 | for (double x : point) { |
| 69 | 80000 | 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 800000 times.
✓ Branch 1 taken 400000 times.
|
1200000 | for (double x : point) { |
| 77 | 800000 | s += x * x * x * x; | |
| 78 | } | ||
| 79 | return s; | ||
| 80 | } | ||
| 81 | |||
| 82 | 3360000 | double EvaluateAt(FuncType func_type, const std::vector<double> &point) { | |
| 83 |
8/9✓ Branch 0 taken 480000 times.
✓ Branch 1 taken 480000 times.
✓ Branch 2 taken 480000 times.
✓ Branch 3 taken 480000 times.
✓ Branch 4 taken 880000 times.
✓ Branch 5 taken 80000 times.
✓ Branch 6 taken 80000 times.
✓ Branch 7 taken 400000 times.
✗ Branch 8 not taken.
|
3360000 | 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 | 480000 | 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 104 times.
✗ Branch 2 not taken.
|
104 | SabirovSMonteCarloSTL::SabirovSMonteCarloSTL(const InType &in) { |
| 108 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 109 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | GetInput() = in; |
| 110 | 104 | GetOutput() = 0.0; | |
| 111 | 104 | } | |
| 112 | |||
| 113 |
1/2✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
|
104 | bool SabirovSMonteCarloSTL::ValidationImpl() { |
| 114 | const auto &in = GetInput(); | ||
| 115 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | if (in.lower.size() != in.upper.size() || in.lower.empty()) { |
| 116 | return false; | ||
| 117 | } | ||
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (in.num_samples <= 0) { |
| 119 | return false; | ||
| 120 | } | ||
| 121 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (size_t i = 0; i < in.lower.size(); ++i) { |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (in.lower[i] >= in.upper[i]) { |
| 123 | return false; | ||
| 124 | } | ||
| 125 | } | ||
| 126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) { |
| 127 | return false; | ||
| 128 | } | ||
| 129 | constexpr size_t kMaxDimensions = 10; | ||
| 130 | 104 | return in.lower.size() <= kMaxDimensions; | |
| 131 | } | ||
| 132 | |||
| 133 | 104 | bool SabirovSMonteCarloSTL::PreProcessingImpl() { | |
| 134 | const auto &in = GetInput(); | ||
| 135 | 104 | lower_ = in.lower; | |
| 136 | 104 | upper_ = in.upper; | |
| 137 | 104 | num_samples_ = in.num_samples; | |
| 138 | 104 | func_type_ = in.func_type; | |
| 139 | 104 | GetOutput() = 0.0; | |
| 140 | 104 | return true; | |
| 141 | } | ||
| 142 | |||
| 143 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | bool SabirovSMonteCarloSTL::RunImpl() { |
| 144 | 104 | const int dims = static_cast<int>(lower_.size()); | |
| 145 | |||
| 146 | 104 | std::vector<std::uniform_real_distribution<double>> dists; | |
| 147 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | dists.reserve(static_cast<size_t>(dims)); |
| 148 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (int j = 0; j < dims; ++j) { |
| 149 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
160 | dists.emplace_back(lower_[j], upper_[j]); |
| 150 | } | ||
| 151 | |||
| 152 | double volume = 1.0; | ||
| 153 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (int j = 0; j < dims; ++j) { |
| 154 | 160 | volume *= (upper_[j] - lower_[j]); | |
| 155 | } | ||
| 156 | |||
| 157 | 104 | const FuncType ftype = func_type_; | |
| 158 | 104 | const int n_samples = num_samples_; | |
| 159 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | const int num_threads = std::max(1, ppc::util::GetNumThreads()); |
| 160 | |||
| 161 |
1/4✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
104 | std::vector<double> partial_sums(static_cast<size_t>(num_threads), 0.0); |
| 162 | 104 | std::vector<std::thread> threads; | |
| 163 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | threads.reserve(static_cast<size_t>(num_threads)); |
| 164 | |||
| 165 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 104 times.
|
364 | for (int ti = 0; ti < num_threads; ++ti) { |
| 166 | 260 | const int start = (ti * n_samples) / num_threads; | |
| 167 | 260 | const int end = ((ti + 1) * n_samples) / num_threads; | |
| 168 |
1/2✓ Branch 1 taken 260 times.
✗ Branch 2 not taken.
|
260 | threads.emplace_back([&, ti, start, end, dims, ftype]() { |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | if (start >= end) { |
| 170 | ✗ | return; | |
| 171 | } | ||
| 172 | 260 | std::vector<double> point(static_cast<size_t>(dims)); | |
| 173 | 260 | std::seed_seq seed{static_cast<uint32_t>(ti), static_cast<uint32_t>(start), static_cast<uint32_t>(end), | |
| 174 |
1/4✓ Branch 1 taken 260 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
260 | static_cast<uint32_t>(n_samples)}; |
| 175 | std::mt19937 gen(seed); | ||
| 176 | double local = 0.0; | ||
| 177 |
2/2✓ Branch 0 taken 3360000 times.
✓ Branch 1 taken 260 times.
|
3360260 | for (int i = start; i < end; ++i) { |
| 178 |
2/2✓ Branch 0 taken 6960000 times.
✓ Branch 1 taken 3360000 times.
|
10320000 | for (int j = 0; j < dims; ++j) { |
| 179 | 6960000 | point[static_cast<size_t>(j)] = dists[static_cast<size_t>(j)](gen); | |
| 180 | } | ||
| 181 | 3360000 | local += EvaluateAt(ftype, point); | |
| 182 | } | ||
| 183 |
1/2✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
|
260 | partial_sums[static_cast<size_t>(ti)] = local; |
| 184 | }); | ||
| 185 | } | ||
| 186 | |||
| 187 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 104 times.
|
364 | for (auto &th : threads) { |
| 188 |
1/2✓ Branch 1 taken 260 times.
✗ Branch 2 not taken.
|
260 | th.join(); |
| 189 | } | ||
| 190 | |||
| 191 | double sum = 0.0; | ||
| 192 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 104 times.
|
364 | for (double part : partial_sums) { |
| 193 | 260 | sum += part; | |
| 194 | } | ||
| 195 | |||
| 196 | 104 | GetOutput() = volume * sum / static_cast<double>(num_samples_); | |
| 197 | 104 | return true; | |
| 198 | 104 | } | |
| 199 | |||
| 200 | 104 | bool SabirovSMonteCarloSTL::PostProcessingImpl() { | |
| 201 | 104 | return true; | |
| 202 | } | ||
| 203 | |||
| 204 | } // namespace sabirov_s_monte_carlo | ||
| 205 |