| 1 | /* |
| 2 | Copyright (C) 2024 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "grn_distance.hpp" |
| 22 | |
| 23 | namespace grn { |
| 24 | namespace distance { |
| 25 | #ifdef GRN_WITH_XSIMD |
| 26 | template <typename Arch, |
| 27 | typename ElementType, |
| 28 | typename BatchFunc, |
| 29 | typename SequentialFunc> |
| 30 | void |
| 31 | each_batch(const ElementType *vector_raw, |
| 32 | size_t n_elements, |
| 33 | BatchFunc batch_func, |
| 34 | SequentialFunc sequential_func) |
| 35 | { |
| 36 | using batch = xsimd::batch<ElementType, Arch>; |
| 37 | auto address = reinterpret_cast<const uintptr_t>(vector_raw); |
| 38 | size_t i = 0; |
| 39 | auto unaligned_size = address % Arch::alignment(); |
| 40 | if (unaligned_size != 0) { |
| 41 | auto adjust_size = Arch::alignment() - unaligned_size; |
| 42 | if ((adjust_size % sizeof(ElementType)) != 0) { |
| 43 | // Can't align. |
| 44 | for (; i < n_elements; i += batch::size) { |
| 45 | auto vector_batch = batch::load_unaligned(vector_raw + i); |
| 46 | batch_func(vector_batch); |
| 47 | } |
| 48 | for (; i < n_elements; ++i) { |
| 49 | sequential_func(vector_raw, i); |
| 50 | } |
| 51 | return; |
| 52 | } |
| 53 | // Adjust start alignment. |
| 54 | for (; adjust_size > 0; ++i, adjust_size -= sizeof(ElementType)) { |
| 55 | sequential_func(vector_raw, i); |
| 56 | } |
| 57 | } |
| 58 | // Aligned batches. |
| 59 | for (; i < n_elements; i += batch::size) { |
| 60 | auto vector_batch = batch::load_aligned(vector_raw + i); |
| 61 | batch_func(vector_batch); |
| 62 | } |
| 63 | // Rest. |
| 64 | for (; i < n_elements; ++i) { |
| 65 | sequential_func(vector_raw, i); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | template <typename Arch, |
| 70 | typename ElementType, |
| 71 | typename BatchFunc, |
| 72 | typename SequentialFunc> |
| 73 | void |
| 74 | each_batch(const ElementType *vector_raw1, |
| 75 | const ElementType *vector_raw2, |
| 76 | size_t n_elements, |
| 77 | BatchFunc batch_func, |
| 78 | SequentialFunc sequential_func) |
| 79 | { |
| 80 | using batch = xsimd::batch<ElementType, Arch>; |
| 81 | auto address1 = reinterpret_cast<const uintptr_t>(vector_raw1); |
| 82 | auto address2 = reinterpret_cast<const uintptr_t>(vector_raw2); |
| 83 | size_t i = 0; |
| 84 | auto unaligned_size1 = address1 % Arch::alignment(); |
| 85 | auto unaligned_size2 = address2 % Arch::alignment(); |
| 86 | bool aligned = true; |
| 87 | if (unaligned_size1 != unaligned_size2) { |
| 88 | aligned = false; |
| 89 | } else if (unaligned_size1 != 0) { |
| 90 | auto adjust_size = Arch::alignment() - unaligned_size1; |
| 91 | if ((adjust_size % sizeof(ElementType)) == 0) { |
| 92 | // Adjust start alignment. |
| 93 | for (; adjust_size > 0; ++i, adjust_size -= sizeof(ElementType)) { |
| 94 | sequential_func(vector_raw1, vector_raw2, i); |
| 95 | } |
| 96 | } else { |
| 97 | aligned = false; |
| 98 | } |
| 99 | } |
| 100 | if (!aligned) { |
| 101 | // Can't align. |
| 102 | for (; i < n_elements; i += batch::size) { |
| 103 | auto vector_batch1 = batch::load_unaligned(vector_raw1 + i); |
| 104 | auto vector_batch2 = batch::load_unaligned(vector_raw2 + i); |
| 105 | batch_func(vector_batch1, vector_batch2); |
| 106 | } |
| 107 | for (; i < n_elements; ++i) { |
| 108 | sequential_func(vector_raw1, vector_raw2, i); |
| 109 | } |
| 110 | return; |
| 111 | } |
| 112 | // Aligned batches. |
| 113 | for (; i < n_elements; i += batch::size) { |
| 114 | auto vector_batch1 = batch::load_aligned(vector_raw1 + i); |
| 115 | auto vector_batch2 = batch::load_aligned(vector_raw2 + i); |
| 116 | batch_func(vector_batch1, vector_batch2); |
| 117 | } |
| 118 | // Rest. |
| 119 | for (; i < n_elements; ++i) { |
| 120 | sequential_func(vector_raw1, vector_raw2, i); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | template <typename Arch, typename ElementType> |
| 125 | float |
| 126 | l2_norm::operator()(Arch, const ElementType *vector_raw, size_t n_elements) |
| 127 | { |
| 128 | float square_sum = 0; |
| 129 | using batch = xsimd::batch<ElementType, Arch>; |
| 130 | each_batch<Arch, ElementType>( |
| 131 | vector_raw, |
| 132 | n_elements, |
| 133 | [&square_sum](batch &vector_batch) { |
| 134 | square_sum += xsimd::reduce_add(vector_batch * vector_batch); |
| 135 | }, |
| 136 | [&square_sum](const ElementType *vector_raw, size_t i) { |
| 137 | square_sum += vector_raw[i] * vector_raw[i]; |
| 138 | }); |
| 139 | return std::sqrt(square_sum); |
| 140 | } |
| 141 | |
| 142 | template <typename Arch, typename ElementType> |
| 143 | float |
| 144 | difference_l1_norm::operator()(Arch, |
| 145 | const ElementType *vector_raw1, |
| 146 | const ElementType *vector_raw2, |
| 147 | size_t n_elements) |
| 148 | { |
| 149 | using batch = xsimd::batch<ElementType, Arch>; |
| 150 | float absolute_sum = 0; |
| 151 | if constexpr (std::is_floating_point_v<ElementType>) { |
| 152 | each_batch<Arch, ElementType>( |
| 153 | vector_raw1, |
| 154 | vector_raw2, |
| 155 | n_elements, |
| 156 | [&absolute_sum](batch &vector_batch1, batch &vector_batch2) { |
| 157 | auto difference = vector_batch1 - vector_batch2; |
| 158 | absolute_sum += xsimd::reduce_add(xsimd::fabs(difference)); |
| 159 | }, |
| 160 | [&absolute_sum](const ElementType *vector_raw1, |
| 161 | const ElementType *vector_raw2, |
| 162 | size_t i) { |
| 163 | auto difference = vector_raw1[i] - vector_raw2[i]; |
| 164 | absolute_sum += difference < 0 ? -difference : difference; |
| 165 | }); |
| 166 | } else { |
| 167 | each_batch<Arch, ElementType>( |
| 168 | vector_raw1, |
| 169 | vector_raw2, |
| 170 | n_elements, |
| 171 | [&absolute_sum](batch &vector_batch1, batch &vector_batch2) { |
| 172 | auto difference = vector_batch1 - vector_batch2; |
| 173 | absolute_sum += xsimd::reduce_add(xsimd::abs(difference)); |
| 174 | }, |
| 175 | [&absolute_sum](const ElementType *vector_raw1, |
| 176 | const ElementType *vector_raw2, |
| 177 | size_t i) { |
| 178 | auto difference = vector_raw1[i] - vector_raw2[i]; |
| 179 | absolute_sum += difference < 0 ? -difference : difference; |
| 180 | }); |
| 181 | } |
| 182 | return absolute_sum; |
| 183 | } |
| 184 | |
| 185 | template <typename Arch, typename ElementType> |
| 186 | float |
| 187 | difference_l2_norm_squared::operator()(Arch, |
| 188 | const ElementType *vector_raw1, |
| 189 | const ElementType *vector_raw2, |
| 190 | size_t n_elements) |
| 191 | { |
| 192 | using batch = xsimd::batch<ElementType, Arch>; |
| 193 | float square_sum = 0; |
| 194 | each_batch<Arch, ElementType>( |
| 195 | vector_raw1, |
| 196 | vector_raw2, |
| 197 | n_elements, |
| 198 | [&square_sum](batch &vector_batch1, batch &vector_batch2) { |
| 199 | auto difference = vector_batch1 - vector_batch2; |
| 200 | square_sum += xsimd::reduce_add(difference * difference); |
| 201 | }, |
| 202 | [&square_sum](const ElementType *vector_raw1, |
| 203 | const ElementType *vector_raw2, |
| 204 | size_t i) { |
| 205 | auto difference = vector_raw1[i] - vector_raw2[i]; |
| 206 | square_sum += difference * difference; |
| 207 | }); |
| 208 | return square_sum; |
| 209 | } |
| 210 | |
| 211 | template <typename Arch, typename ElementType> |
| 212 | float |
| 213 | inner_product::operator()(Arch, |
| 214 | const ElementType *vector_raw1, |
| 215 | const ElementType *vector_raw2, |
| 216 | size_t n_elements) |
| 217 | { |
| 218 | using batch = xsimd::batch<ElementType, Arch>; |
| 219 | float multiplication_sum = 0; |
| 220 | each_batch<Arch, ElementType>( |
| 221 | vector_raw1, |
| 222 | vector_raw2, |
| 223 | n_elements, |
| 224 | [&multiplication_sum](batch &vector_batch1, batch &vector_batch2) { |
| 225 | multiplication_sum += |
| 226 | xsimd::reduce_add(vector_batch1 * vector_batch2); |
| 227 | }, |
| 228 | [&multiplication_sum](const ElementType *vector_raw1, |
| 229 | const ElementType *vector_raw2, |
| 230 | size_t i) { |
| 231 | multiplication_sum += vector_raw1[i] * vector_raw2[i]; |
| 232 | }); |
| 233 | return 1 - multiplication_sum; |
| 234 | } |
| 235 | |
| 236 | template <typename Arch, typename ElementType> |
| 237 | float |
| 238 | cosine::operator()(Arch, |
| 239 | const ElementType *vector_raw1, |
| 240 | const ElementType *vector_raw2, |
| 241 | size_t n_elements) |
| 242 | { |
| 243 | using batch = xsimd::batch<ElementType, Arch>; |
| 244 | ElementType inner_product = 0; |
| 245 | ElementType square_sum1 = 0; |
| 246 | ElementType square_sum2 = 0; |
| 247 | each_batch<Arch, ElementType>( |
| 248 | vector_raw1, |
| 249 | vector_raw2, |
| 250 | n_elements, |
| 251 | [&inner_product, &square_sum1, &square_sum2](batch &vector_batch1, |
| 252 | batch &vector_batch2) { |
| 253 | inner_product += xsimd::reduce_add(vector_batch1 * vector_batch2); |
| 254 | square_sum1 += xsimd::reduce_add(vector_batch1 * vector_batch1); |
| 255 | square_sum2 += xsimd::reduce_add(vector_batch2 * vector_batch2); |
| 256 | }, |
| 257 | [&inner_product, &square_sum1, &square_sum2]( |
| 258 | const ElementType *vector_raw1, |
| 259 | const ElementType *vector_raw2, |
| 260 | size_t i) { |
| 261 | ElementType value1 = vector_raw1[i]; |
| 262 | ElementType value2 = vector_raw2[i]; |
| 263 | inner_product += value1 * value2; |
| 264 | square_sum1 += value1 * value1; |
| 265 | square_sum2 += value2 * value2; |
| 266 | }); |
| 267 | if (numeric::is_zero(inner_product)) { |
| 268 | return 1; |
| 269 | } else { |
| 270 | return 1 - (inner_product / |
| 271 | (std::sqrt(square_sum1) * std::sqrt(square_sum2))); |
| 272 | } |
| 273 | } |
| 274 | #endif |
| 275 | } // namespace distance |
| 276 | } // namespace grn |