| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "klimenko_v_lsh_contrast_incr/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <climits> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <thread> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "klimenko_v_lsh_contrast_incr/common/include/common.hpp" | ||
| 13 | #include "oneapi/tbb/parallel_for.h" | ||
| 14 | #include "util/include/util.hpp" | ||
| 15 | |||
| 16 | namespace klimenko_v_lsh_contrast_incr { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | void GetThreadRange(size_t tid, size_t total, size_t num_t, size_t &begin, size_t &end) { | ||
| 21 | 24 | size_t chunk = total / num_t; | |
| 22 | 24 | begin = tid * chunk; | |
| 23 | 12 | end = (tid == num_t - 1) ? total : begin + chunk; | |
| 24 | } | ||
| 25 | |||
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | std::pair<int, int> FindMinMaxSTL(const std::vector<int> &data, int num_threads) { |
| 27 | 6 | const size_t size = data.size(); | |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (size == 0) { |
| 29 | ✗ | return {INT_MAX, INT_MIN}; | |
| 30 | } | ||
| 31 | |||
| 32 | 6 | std::vector<std::thread> threads(num_threads); | |
| 33 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | std::vector<int> local_min(num_threads); |
| 34 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> local_max(num_threads); |
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int tid = 0; tid < num_threads; tid++) { |
| 37 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12 | threads[tid] = std::thread([&, tid]() { |
| 38 | size_t begin = 0; | ||
| 39 | size_t end = 0; | ||
| 40 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | GetThreadRange(tid, size, num_threads, begin, end); |
| 41 | |||
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (begin >= size) { |
| 43 | ✗ | local_min[tid] = INT_MAX; | |
| 44 | ✗ | local_max[tid] = INT_MIN; | |
| 45 | ✗ | return; | |
| 46 | } | ||
| 47 | |||
| 48 | 12 | int min_val = data[begin]; | |
| 49 | 12 | int max_val = data[begin]; | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 12 times.
|
1308 | for (size_t i = begin; i < end; i++) { |
| 52 |
2/2✓ Branch 0 taken 461 times.
✓ Branch 1 taken 835 times.
|
1296 | min_val = std::min(min_val, data[i]); |
| 53 | 1296 | max_val = std::max(max_val, data[i]); | |
| 54 | } | ||
| 55 | |||
| 56 | 12 | local_min[tid] = min_val; | |
| 57 | 12 | local_max[tid] = max_val; | |
| 58 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | }); |
| 59 | } | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (auto &th : threads) { |
| 62 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | th.join(); |
| 63 | } | ||
| 64 | |||
| 65 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | int global_min = *std::ranges::min_element(local_min); |
| 66 | 6 | int global_max = *std::ranges::max_element(local_max); | |
| 67 | |||
| 68 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | return {global_min, global_max}; |
| 69 | 6 | } | |
| 70 | |||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | void StretchContrastSTL(const std::vector<int> &input, std::vector<int> &output, int min_val, int max_val, |
| 72 | int num_threads) { | ||
| 73 | 6 | const size_t size = input.size(); | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (size == 0) { |
| 75 | ✗ | return; | |
| 76 | } | ||
| 77 | |||
| 78 | 6 | std::vector<std::thread> threads(num_threads); | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int tid = 0; tid < num_threads; tid++) { |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | threads[tid] = std::thread([&, tid]() { |
| 82 | size_t begin = 0; | ||
| 83 | size_t end = 0; | ||
| 84 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | GetThreadRange(tid, size, num_threads, begin, end); |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 1296 times.
✓ Branch 1 taken 12 times.
|
1308 | for (size_t i = begin; i < end; i++) { |
| 87 | 1296 | output[i] = ((input[i] - min_val) * 255) / (max_val - min_val); | |
| 88 | } | ||
| 89 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | }); |
| 90 | } | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (auto &th : threads) { |
| 93 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | th.join(); |
| 94 | } | ||
| 95 | 6 | } | |
| 96 | |||
| 97 | } // namespace | ||
| 98 | |||
| 99 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | KlimenkoVLSHContrastIncrALL::KlimenkoVLSHContrastIncrALL(const InType &in) { |
| 100 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 101 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | GetInput() = in; |
| 102 | 6 | } | |
| 103 | |||
| 104 | 6 | bool KlimenkoVLSHContrastIncrALL::ValidationImpl() { | |
| 105 | 6 | int initialized = 0; | |
| 106 | 6 | MPI_Initialized(&initialized); | |
| 107 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (initialized != 0) { |
| 108 | 6 | int rank = 0; | |
| 109 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 110 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 111 | 3 | return !GetInput().empty(); | |
| 112 | } | ||
| 113 | return true; | ||
| 114 | } | ||
| 115 | ✗ | return !GetInput().empty(); | |
| 116 | } | ||
| 117 | |||
| 118 | 6 | bool KlimenkoVLSHContrastIncrALL::PreProcessingImpl() { | |
| 119 | 6 | GetOutput().resize(GetInput().size()); | |
| 120 | 6 | return true; | |
| 121 | } | ||
| 122 | |||
| 123 | 6 | bool KlimenkoVLSHContrastIncrALL::RunImpl() { | |
| 124 | 6 | int rank = 0; | |
| 125 | 6 | int size = 1; | |
| 126 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 127 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 128 | |||
| 129 | const auto &input = GetInput(); | ||
| 130 | auto &output = GetOutput(); | ||
| 131 | |||
| 132 | 6 | int total_size = 0; | |
| 133 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 134 | 3 | total_size = static_cast<int>(input.size()); | |
| 135 | } | ||
| 136 | 6 | MPI_Bcast(&total_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 137 | |||
| 138 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (total_size == 0) { |
| 139 | return false; | ||
| 140 | } | ||
| 141 | |||
| 142 | 6 | const int num_stl_threads = ppc::util::GetNumThreads(); | |
| 143 | |||
| 144 | 6 | std::vector<int> recv_counts(size); | |
| 145 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> displs(size); |
| 146 | |||
| 147 | 6 | int chunk = total_size / size; | |
| 148 | 6 | int remainder = total_size % size; | |
| 149 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int i = 0; i < size; i++) { |
| 150 |
3/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
|
24 | recv_counts[i] = chunk + (i < remainder ? 1 : 0); |
| 151 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | displs[i] = (i == 0) ? 0 : displs[i - 1] + recv_counts[i - 1]; |
| 152 | } | ||
| 153 | |||
| 154 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | int local_size = recv_counts[rank]; |
| 155 |
2/6✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
6 | std::vector<int> local_data(local_size); |
| 156 | |||
| 157 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatterv(input.data(), recv_counts.data(), displs.data(), MPI_INT, local_data.data(), local_size, MPI_INT, 0, |
| 158 | MPI_COMM_WORLD); | ||
| 159 | |||
| 160 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | auto [local_min, local_max] = FindMinMaxSTL(local_data, num_stl_threads); |
| 161 | |||
| 162 | 6 | int global_min = 0; | |
| 163 | 6 | int global_max = 0; | |
| 164 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Allreduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); |
| 165 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Allreduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); |
| 166 | |||
| 167 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> local_output(local_size); |
| 168 | |||
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (global_max == global_min) { |
| 170 | std::ranges::copy(local_data, local_output.begin()); | ||
| 171 | } else { | ||
| 172 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | StretchContrastSTL(local_data, local_output, global_min, global_max, num_stl_threads); |
| 173 | } | ||
| 174 | |||
| 175 | 6 | std::vector<int> recv_buffer; | |
| 176 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 177 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | recv_buffer.resize(total_size); |
| 178 | } | ||
| 179 | |||
| 180 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Gatherv(local_output.data(), local_size, MPI_INT, recv_buffer.data(), recv_counts.data(), displs.data(), MPI_INT, |
| 181 | 0, MPI_COMM_WORLD); | ||
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 184 | std::ranges::copy(recv_buffer, output.begin()); | ||
| 185 | } | ||
| 186 | |||
| 187 | return true; | ||
| 188 | } | ||
| 189 | |||
| 190 | 6 | bool KlimenkoVLSHContrastIncrALL::PostProcessingImpl() { | |
| 191 | 6 | int rank = 0; | |
| 192 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 193 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 194 | 3 | return GetOutput().size() == GetInput().size(); | |
| 195 | } | ||
| 196 | return true; | ||
| 197 | } | ||
| 198 | |||
| 199 | } // namespace klimenko_v_lsh_contrast_incr | ||
| 200 |