|
| 1 | +#include <pybind11/pybind11.h> |
| 2 | +#include <pybind11/numpy.h> |
| 3 | +#include <pybind11/stl.h> |
| 4 | +#include <vector> |
| 5 | +#include <cstdint> |
| 6 | +#include <algorithm> |
| 7 | +#include <omp.h> |
| 8 | + |
| 9 | +namespace py = pybind11; |
| 10 | + |
| 11 | +// Constants |
| 12 | +const uint32_t MERSENNE_PRIME = 2147483647; // 2^31 - 1 |
| 13 | +const uint32_t MAX_HASH = 4294967295; // 2^32 - 1 |
| 14 | + |
| 15 | +uint32_t simple_hash(const std::string& token) { |
| 16 | + uint32_t hash = 5381; |
| 17 | + for (const uint8_t c : token) { |
| 18 | + hash = ((hash << 5) + hash) + c; |
| 19 | + } |
| 20 | + return hash; |
| 21 | +} |
| 22 | + |
| 23 | +std::vector<std::tuple<uint32_t, py::bytes, uint64_t>> calc_minhash_c( |
| 24 | + const std::vector<std::string>& tokens, |
| 25 | + const py::array_t<uint32_t>& perm_a, |
| 26 | + const py::array_t<uint32_t>& perm_b, |
| 27 | + const py::bytes& empty_hash_value, |
| 28 | + const std::vector<std::pair<size_t, size_t>>& hash_ranges, |
| 29 | + uint32_t union_find_parallel_num, |
| 30 | + uint64_t uid) |
| 31 | +{ |
| 32 | + std::vector<std::tuple<uint32_t, py::bytes, uint64_t>> pairs; |
| 33 | + |
| 34 | + if (tokens.empty()) { |
| 35 | + pairs.emplace_back(MAX_HASH % union_find_parallel_num, empty_hash_value, uid); |
| 36 | + return pairs; |
| 37 | + } |
| 38 | + |
| 39 | + std::vector<uint32_t> hv; |
| 40 | + hv.reserve(tokens.size()); |
| 41 | + for (const std::string& token : tokens) { |
| 42 | + hv.push_back(simple_hash(token)); |
| 43 | + } |
| 44 | + |
| 45 | + auto perm_a_data = perm_a.unchecked<1>(); |
| 46 | + auto perm_b_data = perm_b.unchecked<1>(); |
| 47 | + size_t num_permutation = perm_a.shape(0); |
| 48 | + |
| 49 | + std::vector<uint32_t> hash_values(num_permutation, MAX_HASH); |
| 50 | + for (size_t i = 0; i < num_permutation; ++i) { |
| 51 | + for (uint32_t h : hv) { |
| 52 | + uint32_t phv = ((static_cast<uint64_t>(h) * perm_a_data(i) + perm_b_data(i)) % MERSENNE_PRIME) & MAX_HASH; |
| 53 | + hash_values[i] = std::min(hash_values[i], phv); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + for (size_t i = 0; i < hash_ranges.size(); ++i) { |
| 59 | + const auto& [start, end] = hash_ranges[i]; |
| 60 | + std::vector<uint32_t> band_hash_values(hash_values.begin() + start, hash_values.begin() + end); |
| 61 | + |
| 62 | + py::bytes hash_value = py::bytes( |
| 63 | + std::string(reinterpret_cast<char*>(&i), sizeof(uint32_t)) + |
| 64 | + std::string(reinterpret_cast<char*>(band_hash_values.data()), band_hash_values.size() * sizeof(uint32_t)) |
| 65 | + ); |
| 66 | + |
| 67 | + uint32_t hash_table_id = hash_values[start] % union_find_parallel_num; |
| 68 | + pairs.emplace_back(hash_table_id, hash_value, uid); |
| 69 | + } |
| 70 | + |
| 71 | + return pairs; |
| 72 | +} |
| 73 | + |
| 74 | +py::list calc_minhash_batch_c( |
| 75 | + const std::vector<std::vector<std::string>>& tokens_list, |
| 76 | + const uint64_t uid_begin, |
| 77 | + const std::vector<uint64_t>& perm_a, |
| 78 | + const std::vector<uint64_t>& perm_b, |
| 79 | + const std::string& empty_hash_value, |
| 80 | + const std::vector<std::pair<size_t, size_t>>& hash_ranges, |
| 81 | + uint32_t union_find_parallel_num, |
| 82 | + uint32_t num_threads) |
| 83 | +{ |
| 84 | + omp_set_num_threads(num_threads); |
| 85 | + size_t total_docs = tokens_list.size(); |
| 86 | + std::vector<std::tuple<uint32_t, std::string, uint64_t>> intermediate_pairs; |
| 87 | + intermediate_pairs.reserve(total_docs * hash_ranges.size()); |
| 88 | + |
| 89 | + size_t num_permutation = perm_a.size(); |
| 90 | + |
| 91 | + #pragma omp parallel |
| 92 | + { |
| 93 | + std::vector<std::tuple<uint32_t, std::string, uint64_t>> local_pairs; |
| 94 | + local_pairs.reserve(total_docs * hash_ranges.size() / num_threads); |
| 95 | + std::vector<uint32_t> hash_values(num_permutation); |
| 96 | + |
| 97 | + #pragma omp for nowait |
| 98 | + for (size_t doc_idx = 0; doc_idx < total_docs; ++doc_idx) { |
| 99 | + const auto& tokens = tokens_list[doc_idx]; |
| 100 | + uint64_t uid = uid_begin + doc_idx; |
| 101 | + |
| 102 | + if (tokens.empty()) { |
| 103 | + local_pairs.emplace_back(MAX_HASH % union_find_parallel_num, empty_hash_value, uid); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + std::fill(hash_values.begin(), hash_values.end(), MAX_HASH); |
| 108 | + for (const auto& token : tokens) { |
| 109 | + uint32_t h = simple_hash(token); |
| 110 | + for (size_t i = 0; i < num_permutation; ++i) { |
| 111 | + uint32_t phv = (static_cast<uint64_t>(h) * perm_a[i] + perm_b[i]) >> 32; |
| 112 | + hash_values[i] = std::min(hash_values[i], phv); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + for (size_t i = 0; i < hash_ranges.size(); ++i) { |
| 117 | + const auto& [start, end] = hash_ranges[i]; |
| 118 | + std::string hash_value(reinterpret_cast<char*>(&i), sizeof(uint32_t)); |
| 119 | + hash_value.append(reinterpret_cast<char*>(&hash_values[start]), (end - start) * sizeof(uint32_t)); |
| 120 | + |
| 121 | + uint32_t hash_table_id = hash_values[start] % union_find_parallel_num; |
| 122 | + local_pairs.emplace_back(hash_table_id, std::move(hash_value), uid); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + #pragma omp critical |
| 127 | + { |
| 128 | + intermediate_pairs.insert(intermediate_pairs.end(), local_pairs.begin(), local_pairs.end()); |
| 129 | + } |
| 130 | + } |
| 131 | + py::list result; |
| 132 | + for (const auto& item : intermediate_pairs) { |
| 133 | + uint32_t first = std::get<0>(item); |
| 134 | + py::bytes second = py::bytes(std::get<1>(item)); |
| 135 | + uint64_t third = std::get<2>(item); |
| 136 | + result.append(py::make_tuple(first, second, third)); |
| 137 | + } |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +std::vector<std::tuple<uint32_t, py::bytes>> calc_simple_minhash_c( |
| 142 | + const std::vector<std::string>& tokens, |
| 143 | + const py::array_t<uint32_t>& perm_a, |
| 144 | + const py::array_t<uint32_t>& perm_b, |
| 145 | + const std::vector<std::pair<size_t, size_t>>& hash_ranges, |
| 146 | + uint32_t bucket_per_band, |
| 147 | + uint64_t uid) |
| 148 | +{ |
| 149 | + std::vector<std::tuple<uint32_t, py::bytes>> pairs; |
| 150 | + |
| 151 | + if (tokens.empty()) { |
| 152 | + pairs.emplace_back(0, py::bytes("")); |
| 153 | + return pairs; |
| 154 | + } |
| 155 | + |
| 156 | + std::vector<uint32_t> hv; |
| 157 | + hv.reserve(tokens.size()); |
| 158 | + for (const std::string& token : tokens) { |
| 159 | + hv.push_back(simple_hash(token)); |
| 160 | + } |
| 161 | + |
| 162 | + auto perm_a_data = perm_a.unchecked<1>(); |
| 163 | + auto perm_b_data = perm_b.unchecked<1>(); |
| 164 | + size_t num_permutation = perm_a.shape(0); |
| 165 | + |
| 166 | + std::vector<uint32_t> hash_values(num_permutation, MAX_HASH); |
| 167 | + for (size_t i = 0; i < num_permutation; ++i) { |
| 168 | + for (uint32_t h : hv) { |
| 169 | + uint32_t phv = ((static_cast<uint64_t>(h) * perm_a_data(i) + perm_b_data(i)) % MERSENNE_PRIME) & MAX_HASH; |
| 170 | + hash_values[i] = std::min(hash_values[i], phv); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + for (size_t i = 0; i < hash_ranges.size(); ++i) { |
| 176 | + const auto& [start, end] = hash_ranges[i]; |
| 177 | + std::vector<uint32_t> band_hash_values(hash_values.begin() + start, hash_values.begin() + end); |
| 178 | + |
| 179 | + py::bytes hash_value = py::bytes( |
| 180 | + std::string(reinterpret_cast<char*>(band_hash_values.data()), band_hash_values.size() * sizeof(uint32_t)) |
| 181 | + ); |
| 182 | + |
| 183 | + uint32_t hash_table_id = bucket_per_band * i + (hash_values[start] % bucket_per_band); |
| 184 | + pairs.emplace_back(hash_table_id, hash_value); |
| 185 | + } |
| 186 | + |
| 187 | + return pairs; |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +PYBIND11_MODULE(minhash, m) { |
| 192 | + m.def("calc_minhash_c", &calc_minhash_c, "C++ implementation of calc_minhash"); |
| 193 | + m.def("calc_simple_minhash_c", &calc_simple_minhash_c, "C++ implementation of calc_simple_minhash"); |
| 194 | + m.def("calc_minhash_batch_c", &calc_minhash_batch_c, "C++ implementation of calc_minhash (batch version)"); |
| 195 | +} |
0 commit comments