| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "samoylenko_i_integral_trapezoid/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <functional> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "samoylenko_i_integral_trapezoid/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace samoylenko_i_integral_trapezoid { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | 20 | std::function<double(const std::vector<double> &)> GetIntegrationFunction(int64_t choice) { | |
| 16 |
4/5✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
20 | switch (choice) { |
| 17 | case 0: | ||
| 18 | return [](const std::vector<double> &values) { | ||
| 19 | double sum = 0.0; | ||
| 20 |
2/2✓ Branch 0 taken 85612 times.
✓ Branch 1 taken 44808 times.
|
130420 | for (double val : values) { |
| 21 | 85612 | sum += val; | |
| 22 | } | ||
| 23 | return sum; | ||
| 24 | }; | ||
| 25 | case 1: | ||
| 26 | return [](const std::vector<double> &values) { | ||
| 27 | double mult = 1.0; | ||
| 28 |
2/2✓ Branch 0 taken 81608 times.
✓ Branch 1 taken 40804 times.
|
122412 | for (double val : values) { |
| 29 | 81608 | mult *= val; | |
| 30 | } | ||
| 31 | return mult; | ||
| 32 | }; | ||
| 33 | case 2: | ||
| 34 | return [](const std::vector<double> &values) { | ||
| 35 | double sum = 0.0; | ||
| 36 |
2/2✓ Branch 0 taken 1591812 times.
✓ Branch 1 taken 530604 times.
|
2122416 | for (double val : values) { |
| 37 | 1591812 | sum += val * val; | |
| 38 | } | ||
| 39 | return sum; | ||
| 40 | }; | ||
| 41 | case 3: | ||
| 42 | return [](const std::vector<double> &values) { | ||
| 43 | double sum = 0.0; | ||
| 44 |
2/2✓ Branch 0 taken 4004 times.
✓ Branch 1 taken 4004 times.
|
8008 | for (double val : values) { |
| 45 | 4004 | sum += val; | |
| 46 | } | ||
| 47 | 4004 | return std::sin(sum); | |
| 48 | }; | ||
| 49 | default: | ||
| 50 | return [](const std::vector<double> &) { return 0.0; }; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } // namespace | ||
| 54 | |||
| 55 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | SamoylenkoIIntegralTrapezoidOMP::SamoylenkoIIntegralTrapezoidOMP(const InType &in) { |
| 56 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 57 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | GetInput() = in; |
| 58 | 20 | GetOutput() = 0.0; | |
| 59 | 20 | } | |
| 60 | |||
| 61 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | bool SamoylenkoIIntegralTrapezoidOMP::ValidationImpl() { |
| 62 | const auto &in = GetInput(); | ||
| 63 | |||
| 64 |
3/6✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
|
20 | if (in.a.empty() || in.a.size() != in.b.size() || in.a.size() != in.n.size()) { |
| 65 | return false; | ||
| 66 | } | ||
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (size_t i = 0; i < in.a.size(); ++i) { |
| 69 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
36 | if (in.n[i] <= 0 || in.a[i] >= in.b[i]) { |
| 70 | return false; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 20 | return in.function_choice >= 0 && in.function_choice <= 3; | |
| 75 | } | ||
| 76 | |||
| 77 | 20 | bool SamoylenkoIIntegralTrapezoidOMP::PreProcessingImpl() { | |
| 78 | 20 | GetOutput() = 0.0; | |
| 79 | 20 | return true; | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | bool SamoylenkoIIntegralTrapezoidOMP::RunImpl() { |
| 83 | const auto &in = GetInput(); | ||
| 84 | 20 | const int dimensions = static_cast<int>(in.a.size()); | |
| 85 | 20 | auto integral_function = GetIntegrationFunction(in.function_choice); | |
| 86 | double sum = 0.0; | ||
| 87 | |||
| 88 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<double> h(dimensions); |
| 89 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 90 | 36 | h[i] = (in.b[i] - in.a[i]) / in.n[i]; | |
| 91 | } | ||
| 92 | |||
| 93 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int64_t> dim_sizes(dimensions); |
| 94 | int64_t points = 1; | ||
| 95 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 96 | 36 | dim_sizes[i] = in.n[i] + 1; | |
| 97 | 36 | points *= dim_sizes[i]; | |
| 98 | } | ||
| 99 | |||
| 100 | 20 | #pragma omp parallel default(none) shared(dimensions, points, dim_sizes, h, in, integral_function) reduction(+ : sum) \ | |
| 101 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | num_threads(ppc::util::GetNumThreads()) |
| 102 | { | ||
| 103 | std::vector<double> current_point_local(dimensions); | ||
| 104 | |||
| 105 | #pragma omp for | ||
| 106 | for (int64_t pnt = 0; pnt < points; pnt++) { | ||
| 107 | int64_t rem_index = pnt; | ||
| 108 | int weight = 1; | ||
| 109 | |||
| 110 | for (int dim = 0; dim < dimensions; dim++) { | ||
| 111 | int dim_coord = static_cast<int>(rem_index % dim_sizes[dim]); | ||
| 112 | rem_index /= dim_sizes[dim]; | ||
| 113 | |||
| 114 | current_point_local[dim] = in.a[dim] + (dim_coord * h[dim]); | ||
| 115 | |||
| 116 | if (dim_coord > 0 && dim_coord < in.n[dim]) { | ||
| 117 | weight *= 2; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sum += integral_function(current_point_local) * weight; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | double h_mult = 1.0; | ||
| 126 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 127 | 36 | h_mult *= h[i]; | |
| 128 | } | ||
| 129 | |||
| 130 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | GetOutput() = sum * (h_mult / std::pow(2.0, dimensions)); |
| 131 | |||
| 132 | 20 | return true; | |
| 133 | } | ||
| 134 | |||
| 135 | 20 | bool SamoylenkoIIntegralTrapezoidOMP::PostProcessingImpl() { | |
| 136 | 20 | return true; | |
| 137 | } | ||
| 138 | |||
| 139 | } // namespace samoylenko_i_integral_trapezoid | ||
| 140 |