| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zyuzin_n_multi_integrals_simpson/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "zyuzin_n_multi_integrals_simpson/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace zyuzin_n_multi_integrals_simpson { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | ZyuzinNSimpsonALL::ZyuzinNSimpsonALL(const InType &in) { |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 15 | 24 | GetOutput() = 0.0; | |
| 16 | 24 | } | |
| 17 | |||
| 18 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | bool ZyuzinNSimpsonALL::ValidationImpl() { |
| 19 | const auto &input = GetInput(); | ||
| 20 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | if (input.lower_bounds.size() != input.upper_bounds.size() || input.lower_bounds.size() != input.n_steps.size()) { |
| 21 | return false; | ||
| 22 | } | ||
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (input.lower_bounds.empty()) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (size_t i = 0; i < input.lower_bounds.size(); ++i) { |
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (input.lower_bounds[i] > input.upper_bounds[i]) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
48 | if (input.n_steps[i] <= 0 || input.n_steps[i] % 2 != 0) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | 24 | return static_cast<bool>(input.func); | |
| 35 | } | ||
| 36 | |||
| 37 | 24 | bool ZyuzinNSimpsonALL::PreProcessingImpl() { | |
| 38 | 24 | result_ = 0.0; | |
| 39 | 24 | return true; | |
| 40 | } | ||
| 41 | |||
| 42 | ✗ | double ZyuzinNSimpsonALL::GetSimpsonWeight(int index, int n) { | |
| 43 | ✗ | if (index == 0 || index == n) { | |
| 44 | return 1.0; | ||
| 45 | } | ||
| 46 | ✗ | if (index % 2 == 1) { | |
| 47 | ✗ | return 4.0; | |
| 48 | } | ||
| 49 | return 2.0; | ||
| 50 | } | ||
| 51 | |||
| 52 | 24 | double ZyuzinNSimpsonALL::ComputeLocalSimpsonSum(size_t begin, size_t end) { | |
| 53 | const auto &input = GetInput(); | ||
| 54 | const size_t num_dims = input.lower_bounds.size(); | ||
| 55 | |||
| 56 | 24 | std::vector<double> h(num_dims); | |
| 57 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 58 | 48 | h[dim] = (input.upper_bounds[dim] - input.lower_bounds[dim]) / static_cast<double>(input.n_steps[dim]); | |
| 59 | } | ||
| 60 | |||
| 61 | double local_sum = 0.0; | ||
| 62 | |||
| 63 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | #pragma omp parallel for default(none) shared(input, h, begin, end, num_dims) reduction(+ : local_sum) |
| 64 | for (size_t point_idx = begin; point_idx < end; ++point_idx) { | ||
| 65 | auto temp = point_idx; | ||
| 66 | std::vector<double> point(num_dims); | ||
| 67 | double weight = 1.0; | ||
| 68 | |||
| 69 | for (size_t dim = 0; dim < num_dims; ++dim) { | ||
| 70 | const auto axis_points = static_cast<size_t>(input.n_steps[dim]) + 1U; | ||
| 71 | const auto index = static_cast<int>(temp % axis_points); | ||
| 72 | temp /= axis_points; | ||
| 73 | point[dim] = input.lower_bounds[dim] + (static_cast<double>(index) * h[dim]); | ||
| 74 | weight *= GetSimpsonWeight(index, input.n_steps[dim]); | ||
| 75 | } | ||
| 76 | |||
| 77 | local_sum += weight * input.func(point); | ||
| 78 | } | ||
| 79 | |||
| 80 | 24 | return local_sum; | |
| 81 | } | ||
| 82 | |||
| 83 | 24 | bool ZyuzinNSimpsonALL::RunImpl() { | |
| 84 | const auto &input = GetInput(); | ||
| 85 | const size_t num_dims = input.lower_bounds.size(); | ||
| 86 | |||
| 87 | size_t total_points = 1; | ||
| 88 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 89 | 48 | total_points *= static_cast<size_t>(input.n_steps[dim]) + 1U; | |
| 90 | } | ||
| 91 | |||
| 92 | 24 | int rank = 0; | |
| 93 | 24 | int size = 1; | |
| 94 | 24 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 95 | 24 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 96 | |||
| 97 | 24 | const auto usize = static_cast<size_t>(size); | |
| 98 | 24 | const auto urank = static_cast<size_t>(rank); | |
| 99 | 24 | const size_t begin = (total_points * urank) / usize; | |
| 100 | 24 | const size_t end = (total_points * (urank + 1U)) / usize; | |
| 101 | |||
| 102 | 24 | const double local_sum = ComputeLocalSimpsonSum(begin, end); | |
| 103 | |||
| 104 | 24 | double global_sum = 0.0; | |
| 105 | 24 | MPI_Allreduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); | |
| 106 | |||
| 107 | 24 | std::vector<double> h(num_dims); | |
| 108 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 109 | 48 | h[dim] = (input.upper_bounds[dim] - input.lower_bounds[dim]) / static_cast<double>(input.n_steps[dim]); | |
| 110 | } | ||
| 111 | |||
| 112 | double factor = 1.0; | ||
| 113 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 114 | 48 | factor *= h[dim] / 3.0; | |
| 115 | } | ||
| 116 | |||
| 117 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | result_ = global_sum * factor; |
| 118 | 24 | return true; | |
| 119 | } | ||
| 120 | |||
| 121 | 24 | bool ZyuzinNSimpsonALL::PostProcessingImpl() { | |
| 122 | 24 | GetOutput() = result_; | |
| 123 | 24 | return true; | |
| 124 | } | ||
| 125 | |||
| 126 | } // namespace zyuzin_n_multi_integrals_simpson | ||
| 127 |