| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <omp.h> | ||
| 2 | |||
| 3 | #include <cstddef> | ||
| 4 | #include <cstdint> | ||
| 5 | #include <random> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "savva_d_monte_carlo/common/include/common.hpp" | ||
| 9 | #include "savva_d_monte_carlo/omp/include/ops_omp.hpp" | ||
| 10 | |||
| 11 | namespace savva_d_monte_carlo { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
|
68 | SavvaDMonteCarloOMP::SavvaDMonteCarloOMP(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
|
68 | GetInput() = in; |
| 16 | 68 | GetOutput() = 0.0; | |
| 17 | 68 | } | |
| 18 | |||
| 19 | 68 | bool SavvaDMonteCarloOMP::ValidationImpl() { | |
| 20 | const auto &input = GetInput(); | ||
| 21 | |||
| 22 | // Проверка количества точек | ||
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (input.count_points == 0) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 | |||
| 27 | // Проверка наличия функции | ||
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (!input.f) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 | |||
| 32 | // Проверка размерности | ||
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (input.Dimension() == 0) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | |||
| 37 | // Проверка корректности границ | ||
| 38 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 68 times.
|
236 | for (size_t i = 0; i < input.Dimension(); ++i) { |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
|
168 | if (input.lower_bounds[i] > input.upper_bounds[i]) { |
| 40 | return false; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | return true; | ||
| 45 | } | ||
| 46 | |||
| 47 | 68 | bool SavvaDMonteCarloOMP::PreProcessingImpl() { | |
| 48 | 68 | return true; | |
| 49 | } | ||
| 50 | |||
| 51 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | bool SavvaDMonteCarloOMP::RunImpl() { |
| 52 | const auto &input = GetInput(); | ||
| 53 | auto &result = GetOutput(); | ||
| 54 | |||
| 55 | const size_t dim = input.Dimension(); | ||
| 56 | const double vol = input.Volume(); | ||
| 57 | 68 | const auto n = static_cast<int64_t>(input.count_points); | |
| 58 | 68 | const auto &func = input.f; | |
| 59 | |||
| 60 | double sum = 0.0; | ||
| 61 | |||
| 62 | 68 | #pragma omp parallel default(none) shared(input, func, dim, n) reduction(+ : sum) | |
| 63 | { | ||
| 64 | std::minstd_rand gen(1337 + omp_get_thread_num()); | ||
| 65 | |||
| 66 | std::vector<std::uniform_real_distribution<double>> dists(dim); | ||
| 67 | for (size_t i = 0; i < dim; ++i) { | ||
| 68 | dists[i] = std::uniform_real_distribution<double>(input.lower_bounds[i], input.upper_bounds[i]); | ||
| 69 | } | ||
| 70 | |||
| 71 | std::vector<double> point(dim); | ||
| 72 | |||
| 73 | #pragma omp for schedule(static) | ||
| 74 | for (int64_t i = 0; i < n; ++i) { | ||
| 75 | for (size_t j = 0; j < dim; ++j) { | ||
| 76 | point[j] = dists[j](gen); | ||
| 77 | } | ||
| 78 | sum += func(point); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | 68 | result = vol * sum / static_cast<double>(n); | |
| 83 | 68 | return true; | |
| 84 | } | ||
| 85 | |||
| 86 | 68 | bool SavvaDMonteCarloOMP::PostProcessingImpl() { | |
| 87 | 68 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | } // namespace savva_d_monte_carlo | ||
| 91 |