| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "samoylenko_i_integral_trapezoid/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <functional> | ||
| 7 | #include <thread> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "samoylenko_i_integral_trapezoid/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace samoylenko_i_integral_trapezoid { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | 40 | std::function<double(const std::vector<double> &)> GetIntegrationFunction(int64_t choice) { | |
| 17 |
4/5✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
40 | switch (choice) { |
| 18 | case 0: | ||
| 19 | return [](const std::vector<double> &values) { | ||
| 20 | double sum = 0.0; | ||
| 21 |
2/2✓ Branch 0 taken 171224 times.
✓ Branch 1 taken 89616 times.
|
260840 | for (double val : values) { |
| 22 | 171224 | sum += val; | |
| 23 | } | ||
| 24 | return sum; | ||
| 25 | }; | ||
| 26 | case 1: | ||
| 27 | return [](const std::vector<double> &values) { | ||
| 28 | double mult = 1.0; | ||
| 29 |
2/2✓ Branch 0 taken 163216 times.
✓ Branch 1 taken 81608 times.
|
244824 | for (double val : values) { |
| 30 | 163216 | mult *= val; | |
| 31 | } | ||
| 32 | return mult; | ||
| 33 | }; | ||
| 34 | case 2: | ||
| 35 | return [](const std::vector<double> &values) { | ||
| 36 | double sum = 0.0; | ||
| 37 |
2/2✓ Branch 0 taken 3183624 times.
✓ Branch 1 taken 1061208 times.
|
4244832 | for (double val : values) { |
| 38 | 3183624 | sum += val * val; | |
| 39 | } | ||
| 40 | return sum; | ||
| 41 | }; | ||
| 42 | case 3: | ||
| 43 | return [](const std::vector<double> &values) { | ||
| 44 | double sum = 0.0; | ||
| 45 |
2/2✓ Branch 0 taken 8008 times.
✓ Branch 1 taken 8008 times.
|
16016 | for (double val : values) { |
| 46 | 8008 | sum += val; | |
| 47 | } | ||
| 48 | 8008 | return std::sin(sum); | |
| 49 | }; | ||
| 50 | default: | ||
| 51 | return [](const std::vector<double> &) { return 0.0; }; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } // namespace | ||
| 55 | |||
| 56 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | SamoylenkoIIntegralTrapezoidSTL::SamoylenkoIIntegralTrapezoidSTL(const InType &in) { |
| 57 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 58 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | GetInput() = in; |
| 59 | 40 | GetOutput() = 0.0; | |
| 60 | 40 | } | |
| 61 | |||
| 62 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool SamoylenkoIIntegralTrapezoidSTL::ValidationImpl() { |
| 63 | const auto &in = GetInput(); | ||
| 64 | |||
| 65 |
3/6✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 40 times.
|
40 | if (in.a.empty() || in.a.size() != in.b.size() || in.a.size() != in.n.size()) { |
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
|
112 | for (size_t i = 0; i < in.a.size(); ++i) { |
| 70 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72 times.
|
72 | if (in.n[i] <= 0 || in.a[i] >= in.b[i]) { |
| 71 | return false; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | 40 | return in.function_choice >= 0 && in.function_choice <= 3; | |
| 76 | } | ||
| 77 | |||
| 78 | 40 | bool SamoylenkoIIntegralTrapezoidSTL::PreProcessingImpl() { | |
| 79 | 40 | GetOutput() = 0.0; | |
| 80 | 40 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 | namespace { | ||
| 84 | 100 | double GetLocalSum(int64_t start, int64_t end, int dimensions, const std::vector<int64_t> &dim_sizes, | |
| 85 | const std::vector<double> &h, const auto &in, auto &integral_function) { | ||
| 86 | 100 | std::vector<double> current_point(dimensions); | |
| 87 | double local_sum = 0.0; | ||
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 1240440 times.
✓ Branch 1 taken 100 times.
|
1240540 | for (int64_t pnt = start; pnt < end; pnt++) { |
| 90 | int64_t rem_index = pnt; | ||
| 91 | int weight = 1; | ||
| 92 | |||
| 93 |
2/2✓ Branch 0 taken 3526072 times.
✓ Branch 1 taken 1240440 times.
|
4766512 | for (int dim = 0; dim < dimensions; dim++) { |
| 94 |
2/2✓ Branch 0 taken 3460400 times.
✓ Branch 1 taken 65672 times.
|
3526072 | int dim_coord = static_cast<int>(rem_index % dim_sizes[dim]); |
| 95 | 3526072 | rem_index /= dim_sizes[dim]; | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 3460400 times.
✓ Branch 1 taken 65672 times.
|
3526072 | current_point[dim] = in.a[dim] + (dim_coord * h[dim]); |
| 98 | |||
| 99 |
4/4✓ Branch 0 taken 3460400 times.
✓ Branch 1 taken 65672 times.
✓ Branch 2 taken 3394728 times.
✓ Branch 3 taken 65672 times.
|
3526072 | if (dim_coord > 0 && dim_coord < in.n[dim]) { |
| 100 | 3394728 | weight *= 2; | |
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | 1240440 | local_sum += integral_function(current_point) * weight; | |
| 105 | } | ||
| 106 | |||
| 107 | 100 | return local_sum; | |
| 108 | } | ||
| 109 | } // namespace | ||
| 110 | |||
| 111 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | bool SamoylenkoIIntegralTrapezoidSTL::RunImpl() { |
| 112 | const auto &in = GetInput(); | ||
| 113 | 40 | const int dimensions = static_cast<int>(in.a.size()); | |
| 114 | 40 | auto integral_function = GetIntegrationFunction(in.function_choice); | |
| 115 | |||
| 116 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<double> h(dimensions); |
| 117 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
|
112 | for (int i = 0; i < dimensions; i++) { |
| 118 | 72 | h[i] = (in.b[i] - in.a[i]) / in.n[i]; | |
| 119 | } | ||
| 120 | |||
| 121 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<int64_t> dim_sizes(dimensions); |
| 122 | int64_t points = 1; | ||
| 123 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
|
112 | for (int i = 0; i < dimensions; i++) { |
| 124 | 72 | dim_sizes[i] = in.n[i] + 1; | |
| 125 | 72 | points *= dim_sizes[i]; | |
| 126 | } | ||
| 127 | |||
| 128 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | const int num_threads = ppc::util::GetNumThreads(); |
| 129 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<double> local_sums(num_threads, 0.0); |
| 130 |
1/4✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<std::thread> threads(num_threads); |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (int thr = 0; thr < num_threads; thr++) { |
| 133 | 100 | int64_t start = (points * thr) / num_threads; | |
| 134 | 100 | int64_t end = (points * (thr + 1)) / num_threads; | |
| 135 | |||
| 136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
|
100 | threads[thr] = std::thread([&h, &dimensions, &dim_sizes, &in, &integral_function, thr, start, end, &local_sums]() { |
| 137 | 100 | local_sums[thr] = GetLocalSum(start, end, dimensions, dim_sizes, h, in, integral_function); | |
| 138 |
1/2✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
|
100 | }); |
| 139 | } | ||
| 140 | |||
| 141 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (auto &thread : threads) { |
| 142 |
1/2✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
|
100 | thread.join(); |
| 143 | } | ||
| 144 | |||
| 145 | double sum = 0.0; | ||
| 146 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (int thr = 0; thr < num_threads; thr++) { |
| 147 | 100 | sum += local_sums[thr]; | |
| 148 | } | ||
| 149 | |||
| 150 | double h_mult = 1.0; | ||
| 151 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 40 times.
|
112 | for (int i = 0; i < dimensions; i++) { |
| 152 | 72 | h_mult *= h[i]; | |
| 153 | } | ||
| 154 | |||
| 155 | 40 | GetOutput() = sum * (h_mult / std::pow(2.0, dimensions)); | |
| 156 | |||
| 157 | 40 | return true; | |
| 158 | 40 | } | |
| 159 | |||
| 160 | 40 | bool SamoylenkoIIntegralTrapezoidSTL::PostProcessingImpl() { | |
| 161 | 40 | return true; | |
| 162 | } | ||
| 163 | |||
| 164 | } // namespace samoylenko_i_integral_trapezoid | ||
| 165 |