| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "batushin_i_incr_contrast_with_lhs/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <thread> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "batushin_i_incr_contrast_with_lhs/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace batushin_i_incr_contrast_with_lhs { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | BatushinIIncrContrastWithLhsALL::BatushinIIncrContrastWithLhsALL(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 | |||
| 19 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = in; |
| 20 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetOutput().resize(in.size()); |
| 21 | 12 | } | |
| 22 | |||
| 23 | 12 | bool BatushinIIncrContrastWithLhsALL::ValidationImpl() { | |
| 24 | 12 | return !GetInput().empty(); | |
| 25 | } | ||
| 26 | |||
| 27 | 12 | bool BatushinIIncrContrastWithLhsALL::PreProcessingImpl() { | |
| 28 | 12 | return true; | |
| 29 | } | ||
| 30 | |||
| 31 | namespace { | ||
| 32 | |||
| 33 | unsigned char NormalizePixel(unsigned char pixel, unsigned char min_val, double scale_factor) { | ||
| 34 | 27 | double normalized = static_cast<double>(pixel - min_val) * scale_factor; | |
| 35 | |||
| 36 | 27 | normalized = std::floor(normalized + 0.5); | |
| 37 | |||
| 38 | normalized = std::max(normalized, 0.0); | ||
| 39 | normalized = std::min(normalized, 255.0); | ||
| 40 | |||
| 41 | 27 | return static_cast<unsigned char>(normalized); | |
| 42 | } | ||
| 43 | |||
| 44 | unsigned int GetNumThreads() { | ||
| 45 | 12 | unsigned int threads = std::thread::hardware_concurrency(); | |
| 46 | 12 | return threads == 0 ? 1 : threads; | |
| 47 | } | ||
| 48 | |||
| 49 | 27 | void NormalizeBlock(const std::vector<unsigned char> &source, std::vector<unsigned char> &destination, size_t start, | |
| 50 | size_t end, unsigned char min_value, double scale) { | ||
| 51 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
|
54 | for (size_t i = start; i < end; ++i) { |
| 52 | 27 | destination[i] = NormalizePixel(source[i], min_value, scale); | |
| 53 | } | ||
| 54 | 27 | } | |
| 55 | |||
| 56 | 12 | void NormalizeParallel(const std::vector<unsigned char> &source, std::vector<unsigned char> &destination, | |
| 57 | unsigned char min_value, double scale) { | ||
| 58 | 12 | const size_t size = source.size(); | |
| 59 | |||
| 60 | unsigned int threads_count = GetNumThreads(); | ||
| 61 | |||
| 62 | 12 | size_t chunk_size = std::max(static_cast<size_t>(1), size / threads_count); | |
| 63 | |||
| 64 | 12 | size_t blocks = (size + chunk_size - 1) / chunk_size; | |
| 65 | |||
| 66 | 12 | std::vector<std::thread> threads; | |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 12 times.
|
39 | for (size_t block = 0; block < blocks; ++block) { |
| 69 | 27 | size_t start = block * chunk_size; | |
| 70 |
1/2✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
|
27 | size_t end = std::min(start + chunk_size, size); |
| 71 | |||
| 72 |
1/2✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
|
27 | threads.emplace_back([&source, &destination, start, end, min_value, scale] { |
| 73 | 27 | NormalizeBlock(source, destination, start, end, min_value, scale); | |
| 74 | 27 | }); | |
| 75 | } | ||
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 12 times.
|
39 | for (auto &t : threads) { |
| 78 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (t.joinable()) { |
| 79 |
1/2✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
|
27 | t.join(); |
| 80 | } | ||
| 81 | } | ||
| 82 | 12 | } | |
| 83 | |||
| 84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | std::pair<unsigned char, unsigned char> FindLocalMinMax(const std::vector<unsigned char> &data) { |
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (data.empty()) { |
| 86 | ✗ | return {0, 0}; | |
| 87 | } | ||
| 88 | |||
| 89 | 12 | unsigned char min_val = data[0]; | |
| 90 | 12 | unsigned char max_val = data[0]; | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 12 times.
|
27 | for (size_t i = 1; i < data.size(); ++i) { |
| 93 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4 times.
|
15 | min_val = std::min(min_val, data[i]); |
| 94 | 15 | max_val = std::max(max_val, data[i]); | |
| 95 | } | ||
| 96 | |||
| 97 | 12 | return {min_val, max_val}; | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace | ||
| 101 | |||
| 102 | 12 | bool BatushinIIncrContrastWithLhsALL::RunImpl() { | |
| 103 | 12 | int rank = 0; | |
| 104 | 12 | int size = 0; | |
| 105 | |||
| 106 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 107 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 108 | |||
| 109 | const std::vector<unsigned char> &input = GetInput(); | ||
| 110 | std::vector<unsigned char> &output = GetOutput(); | ||
| 111 | |||
| 112 | 12 | const int global_size = static_cast<int>(input.size()); | |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 115 | 6 | output.resize(global_size); | |
| 116 | } | ||
| 117 | |||
| 118 | 12 | std::vector<int> send_counts(size); | |
| 119 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> displs(size); |
| 120 | |||
| 121 | 12 | int base = global_size / size; | |
| 122 | 12 | int remainder = global_size % size; | |
| 123 | |||
| 124 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (int i = 0; i < size; ++i) { |
| 125 |
4/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 12 times.
|
42 | send_counts[i] = base + (i < remainder ? 1 : 0); |
| 126 | |||
| 127 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | displs[i] = (i == 0 ? 0 : displs[i - 1] + send_counts[i - 1]); |
| 128 | } | ||
| 129 | |||
| 130 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<unsigned char> local_data(send_counts[rank]); |
| 131 | |||
| 132 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Scatterv(input.data(), send_counts.data(), displs.data(), MPI_UNSIGNED_CHAR, local_data.data(), send_counts[rank], |
| 133 | MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD); | ||
| 134 | |||
| 135 | 12 | auto [local_min, local_max] = FindLocalMinMax(local_data); | |
| 136 | |||
| 137 | 12 | unsigned char global_min = 0; | |
| 138 | 12 | unsigned char global_max = 0; | |
| 139 | |||
| 140 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Allreduce(&local_min, &global_min, 1, MPI_UNSIGNED_CHAR, MPI_MIN, MPI_COMM_WORLD); |
| 141 | |||
| 142 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Allreduce(&local_max, &global_max, 1, MPI_UNSIGNED_CHAR, MPI_MAX, MPI_COMM_WORLD); |
| 143 | |||
| 144 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<unsigned char> local_result(local_data.size()); |
| 145 | |||
| 146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (global_min == global_max) { |
| 147 | ✗ | local_result = local_data; | |
| 148 | } else { | ||
| 149 | 12 | double scale = 255.0 / static_cast<double>(global_max - global_min); | |
| 150 | |||
| 151 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | NormalizeParallel(local_data, local_result, global_min, scale); |
| 152 | } | ||
| 153 | |||
| 154 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Gatherv(local_result.data(), send_counts[rank], MPI_UNSIGNED_CHAR, output.data(), send_counts.data(), |
| 155 | displs.data(), MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD); | ||
| 156 | |||
| 157 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank != 0) { |
| 158 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | output.resize(global_size); |
| 159 | } | ||
| 160 | |||
| 161 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(output.data(), global_size, MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD); |
| 162 | |||
| 163 | 12 | return true; | |
| 164 | } | ||
| 165 | |||
| 166 | 12 | bool BatushinIIncrContrastWithLhsALL::PostProcessingImpl() { | |
| 167 | 12 | return true; | |
| 168 | } | ||
| 169 | |||
| 170 | } // namespace batushin_i_incr_contrast_with_lhs | ||
| 171 |