Debian Codegraph @

lib/grn_distance.hpp

raw symbols SCIP
1 /*
2 Copyright (C) 2024-2025 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_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/
22
23 #include <groonga/bulk.hpp>cxx . . $ `<file>/include/groonga/bulk.hpp`/
24
25 #include <cmath>
26
27 #ifdef GRN_WITH_SIMSIMD
28 # include <simsimd/simsimd.h>
29 #endif
30
31 #ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
32 # include <xsimd/xsimd.hpp>
33 #endif
34
35 #if defined(GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!) && XSIMD_VERSION_MAJORcxx . . $ `<external>/10674005289014335208/xsimd_config.hpp:15:9`! < 14
36 namespace xsimd {
37 using common = generic;
38 }
39 #endif
40
41 namespace grncxx . . $ grn/ {
42 namespace distancecxx . . $ grn/distance/ {
43 extern bool use_simsimdcxx . . $ grn/distance/use_simsimd.;
44 extern bool use_xsimdcxx . . $ grn/distance/use_xsimd.;
45 constexpr size_t use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold. = 256;
46
47 #ifdef GRN_WITH_SIMSIMD
48 namespace simsimd {
49 extern simsimd_capability_t capabilities;
50 }
51 #endif
52
53 #ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
54 struct l2_normcxx . . $ grn/distance/l2_norm# {
55 template <typename Arch, typename ElementType>
56 float
57 operatorcxx . . $ grn/distance/l2_norm#`operator()`(3899f77a1e5bc2de).()(Arch, const ElementType *vector_raw, size_t n_elements);
58 };
59
60 struct difference_l1_normcxx . . $ grn/distance/difference_l1_norm# {
61 template <typename Arch, typename ElementType>
62 float
63 operatorcxx . . $ grn/distance/difference_l1_norm#`operator()`(3d31c8686a2d08d5).()(Arch,
64 const ElementType *vector_raw1,
65 const ElementType *vector_raw2,
66 size_t n_elements);
67 };
68
69 struct difference_l2_norm_squaredcxx . . $ grn/distance/difference_l2_norm_squared# {
70 template <typename Arch, typename ElementType>
71 float
72 operatorcxx . . $ grn/distance/difference_l2_norm_squared#`operator()`(3d31c8686a2d08d5).()(Arch,
73 const ElementType *vector_raw1,
74 const ElementType *vector_raw2,
75 size_t n_elements);
76 };
77
78 struct inner_productcxx . . $ grn/distance/inner_product# {
79 template <typename Arch, typename ElementType>
80 float
81 operatorcxx . . $ grn/distance/inner_product#`operator()`(3d31c8686a2d08d5).()(Arch,
82 const ElementType *vector_raw1,
83 const ElementType *vector_raw2,
84 size_t n_elements);
85 };
86
87 struct cosinecxx . . $ grn/distance/cosine# {
88 template <typename Arch, typename ElementType>
89 float
90 operatorcxx . . $ grn/distance/cosine#`operator()`(3d31c8686a2d08d5).()(Arch,
91 const ElementType *vector_raw1,
92 const ElementType *vector_raw2,
93 size_t n_elements);
94 };
95 #endif
96 } // namespace distance
97 } // namespace grn
98
99 #ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
100 # define GRN_INSTANTIATION_EXTERNcxx . . $ `lib/grn_distance.hpp:100:11`! extern
101
102 # define GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:102:11`! avx512
103 # define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:103:11`! xsimd::avx512dq
104 # include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/
105 # undef GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:102:11`!
106 # undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:103:11`!
107
108 # define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:108:11`! xsimd::avx2
109 # include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/
110 # undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:108:11`!
111
112 # define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:112:11`! xsimd::avx
113 # include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/
114 # undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:112:11`!
115
116 # define GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:116:11`! neon
117 # define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:117:11`! xsimd::neon64
118 # include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/
119 # undef GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:116:11`!
120 # undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:117:11`!
121
122 # define GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:122:11`! serial
123 # define GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:123:11`! xsimd::common
124 # include "grn_distance_instantiation.hpp"cxx . . $ `<file>/lib/grn_distance_instantiation.hpp`/
125 # undef GRN_INSTANTIATION_SIMSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:122:11`!
126 # undef GRN_INSTANTIATION_XSIMD_ARCHcxx . . $ `lib/grn_distance.hpp:123:11`!
127
128 # undef GRN_INSTANTIATION_EXTERNcxx . . $ `lib/grn_distance.hpp:100:11`!
129 #endif
130
131 namespace grncxx . . $ grn/ {
132 namespace distancecxx . . $ grn/distance/ {
133 template <typename ElementType>
134 float
135 compute_l2_normcxx . . $ grn/distance/compute_l2_norm(684aa5212d47a79e).(grn_obj *vector)
136 {
137 auto vector_raw =
138 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#$anonymous_type_0#$anonymous_type_1#head.(vector));
139 auto n_elements = GRN_BULK_VSIZEcxx . . $ _grn_obj#header.(vector) / sizeof(ElementType);
140 #ifdef GRN_WITH_SIMDcxx . . $ `build/config.h:71:9`!
141 # ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
142 if (use_xsimdcxx . . $ grn/distance/use_xsimd. &&
143 (sizeof(ElementType) * n_elements) >= use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold.) {
144 auto dispatched = xsimdcxx . . $ xsimd/::dispatchcxx . . $ xsimd/dispatch(1323d22f5d47b102).<xsimdcxx . . $ xsimd/::arch_listcxx . . $ xsimd/arch_list#<
145 # ifdef GRN_WITH_SIMD_AVX512cxx . . $ `build/config.h:74:9`!
146 xsimdcxx . . $ xsimd/::avx512dqcxx . . $ xsimd/avx512dq#,
147 # endif
148 # ifdef GRN_WITH_SIMD_AVX2cxx . . $ `build/config.h:73:9`!
149 xsimdcxx . . $ xsimd/::avx2cxx . . $ xsimd/avx2#,
150 # endif
151 # ifdef GRN_WITH_SIMD_AVXcxx . . $ `build/config.h:72:9`!
152 xsimdcxx . . $ xsimd/::avxcxx . . $ xsimd/avx#,
153 # endif
154 # ifdef GRN_WITH_SIMD_NEON64
155 xsimd::neon64,
156 # endif
157 xsimdcxx . . $ xsimd/::commoncxx . . $ xsimd/common#>>(l2_normcxx . . $ grn/distance/l2_norm#{});
158 return dispatched(vector_raw, n_elements);
159 }
160 # endif
161 #endif
162 float square_sum = 0;
163 for (size_t i = 0; i < n_elements; ++i) {
164 square_sum += vector_raw[i] * vector_raw[i];
165 }
166 return stdcxx . . $ std/::sqrt(square_sum);
167 }
168
169 template <typename ElementType>
170 float
171 compute_difference_l1_normcxx . . $ grn/distance/compute_difference_l1_norm(51ea083f54a0f1c0).(grn_obj *vector1, grn_obj *vector2)
172 {
173 auto vector_raw1 =
174 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#$anonymous_type_0#$anonymous_type_1#head.(vector1));
175 auto vector_raw2 =
176 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#u.(vector2));
177 auto n_elements = GRN_BULK_VSIZEcxx . . $ _grn_obj#u.(vector1) / sizeof(ElementType);
178 #ifdef GRN_WITH_SIMDcxx . . $ `build/config.h:71:9`!
179 # ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
180 if (use_xsimdcxx . . $ grn/distance/use_xsimd. &&
181 (sizeof(ElementType) * n_elements * 2) >= use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold.) {
182 auto dispatched = xsimdcxx . . $ xsimd/::dispatchcxx . . $ xsimd/dispatch(1323d22f5d47b102).<xsimdcxx . . $ xsimd/::arch_listcxx . . $ xsimd/arch_list#<
183 # ifdef GRN_WITH_SIMD_AVX512cxx . . $ `build/config.h:74:9`!
184 xsimdcxx . . $ xsimd/::avx512dqcxx . . $ xsimd/avx512dq#,
185 # endif
186 # ifdef GRN_WITH_SIMD_AVX2cxx . . $ `build/config.h:73:9`!
187 xsimdcxx . . $ xsimd/::avx2cxx . . $ xsimd/avx2#,
188 # endif
189 # ifdef GRN_WITH_SIMD_AVXcxx . . $ `build/config.h:72:9`!
190 xsimdcxx . . $ xsimd/::avxcxx . . $ xsimd/avx#,
191 # endif
192 # ifdef GRN_WITH_SIMD_NEON64
193 xsimd::neon64,
194 # endif
195 xsimdcxx . . $ xsimd/::commoncxx . . $ xsimd/common#>>(difference_l1_normcxx . . $ grn/distance/difference_l1_norm#{});
196 return dispatched(vector_raw1, vector_raw2, n_elements);
197 }
198 # endif
199 #endif
200 ElementType absolute_sum = 0;
201 for (size_t i = 0; i < n_elements; ++i) {
202 auto difference = vector_raw1[i] - vector_raw2[i];
203 absolute_sum += difference * ((difference > 0) - (difference < 0));
204 }
205 return absolute_sum;
206 }
207
208 template <typename ElementType>
209 float
210 compute_difference_l2_norm_squaredcxx . . $ grn/distance/compute_difference_l2_norm_squared(51ea083f54a0f1c0).(grn_obj *vector1, grn_obj *vector2)
211 {
212 auto vector_raw1 =
213 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ `include/groonga/groonga.h:2648:9`!(vector1));
214 auto vector_raw2 =
215 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#header.(vector2));
216 auto n_elements = GRN_BULK_VSIZEcxx . . $ _grn_obj#$anonymous_type_0#$anonymous_type_1#head.(vector1) / sizeof(ElementType);
217 #ifdef GRN_WITH_SIMDcxx . . $ `build/config.h:71:9`!
218 # ifdef GRN_WITH_SIMSIMD
219 if (use_simsimd &&
220 (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) {
221 # ifdef GRN_WITH_SIMD_AVX512
222 if (simsimd::capabilities & simsimd_cap_x86_avx512_k) {
223 return simsimd::compute_distance_l2_norm_squared_avx512(vector_raw1,
224 vector_raw2,
225 n_elements);
226 }
227 # endif
228 # ifdef GRN_WITH_SIMD_NEON64
229 if (simsimd::capabilities & simsimd_cap_arm_neon_k) {
230 return simsimd::compute_distance_l2_norm_squared_neon(vector_raw1,
231 vector_raw2,
232 n_elements);
233 }
234 # endif
235 return simsimd::compute_distance_l2_norm_squared_serial(vector_raw1,
236 vector_raw2,
237 n_elements);
238 }
239 # endif
240 # ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
241 if (use_xsimdcxx . . $ grn/distance/use_xsimd. &&
242 (sizeof(ElementType) * n_elements * 2) >= use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold.) {
243 auto dispatched = xsimdcxx . . $ xsimd/::dispatchcxx . . $ xsimd/dispatch(1323d22f5d47b102).<xsimdcxx . . $ xsimd/::arch_listcxx . . $ xsimd/arch_list#<
244 # ifdef GRN_WITH_SIMD_AVX512cxx . . $ `build/config.h:74:9`!
245 xsimdcxx . . $ xsimd/::avx512dqcxx . . $ xsimd/avx512dq#,
246 # endif
247 # ifdef GRN_WITH_SIMD_AVX2cxx . . $ `build/config.h:73:9`!
248 xsimdcxx . . $ xsimd/::avx2cxx . . $ xsimd/avx2#,
249 # endif
250 # ifdef GRN_WITH_SIMD_AVXcxx . . $ `build/config.h:72:9`!
251 xsimdcxx . . $ xsimd/::avxcxx . . $ xsimd/avx#,
252 # endif
253 # ifdef GRN_WITH_SIMD_NEON64
254 xsimd::neon64,
255 # endif
256 xsimdcxx . . $ xsimd/::commoncxx . . $ xsimd/common#>>(difference_l2_norm_squaredcxx . . $ grn/distance/difference_l2_norm_squared#{});
257 return dispatched(vector_raw1, vector_raw2, n_elements);
258 }
259 # endif
260 #endif
261 ElementType square_sum = 0;
262 for (size_t i = 0; i < n_elements; ++i) {
263 auto difference = vector_raw1[i] - vector_raw2[i];
264 square_sum += difference * difference;
265 }
266 return square_sum;
267 }
268
269 template <typename ElementType>
270 float
271 compute_distance_inner_productcxx . . $ grn/distance/compute_distance_inner_product(51ea083f54a0f1c0).(grn_obj *vector1, grn_obj *vector2)
272 {
273 auto vector_raw1 =
274 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ `include/groonga/groonga.h:2648:9`!(vector1));
275 auto vector_raw2 =
276 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#u.(vector2));
277 auto n_elements = GRN_BULK_VSIZEcxx . . $ `include/groonga/groonga.h:2642:9`!(vector1) / sizeof(ElementType);
278 #ifdef GRN_WITH_SIMDcxx . . $ `build/config.h:71:9`!
279 # ifdef GRN_WITH_SIMSIMD
280 if (use_simsimd &&
281 (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) {
282 # ifdef GRN_WITH_SIMD_AVX512
283 if (simsimd::capabilities & simsimd_cap_x86_avx512_k) {
284 return simsimd::compute_distance_inner_product_avx512(vector_raw1,
285 vector_raw2,
286 n_elements);
287 }
288 # endif
289 # ifdef GRN_WITH_SIMD_NEON64
290 if (simsimd::capabilities & simsimd_cap_arm_neon_k) {
291 return simsimd::compute_distance_inner_product_neon(vector_raw1,
292 vector_raw2,
293 n_elements);
294 }
295 # endif
296 return simsimd::compute_distance_inner_product_serial(vector_raw1,
297 vector_raw2,
298 n_elements);
299 }
300 # endif
301 # ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
302 if (use_xsimdcxx . . $ grn/distance/use_xsimd. &&
303 (sizeof(ElementType) * n_elements * 2) >= use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold.) {
304 auto dispatched = xsimdcxx . . $ xsimd/::dispatchcxx . . $ xsimd/dispatch(1323d22f5d47b102).<xsimdcxx . . $ xsimd/::arch_listcxx . . $ xsimd/arch_list#<
305 # ifdef GRN_WITH_SIMD_AVX512cxx . . $ `build/config.h:74:9`!
306 xsimdcxx . . $ xsimd/::avx512dqcxx . . $ xsimd/avx512dq#,
307 # endif
308 # ifdef GRN_WITH_SIMD_AVX2cxx . . $ `build/config.h:73:9`!
309 xsimdcxx . . $ xsimd/::avx2cxx . . $ xsimd/avx2#,
310 # endif
311 # ifdef GRN_WITH_SIMD_AVXcxx . . $ `build/config.h:72:9`!
312 xsimdcxx . . $ xsimd/::avxcxx . . $ xsimd/avx#,
313 # endif
314 # ifdef GRN_WITH_SIMD_NEON64
315 xsimd::neon64,
316 # endif
317 xsimdcxx . . $ xsimd/::commoncxx . . $ xsimd/common#>>(inner_productcxx . . $ grn/distance/inner_product#{});
318 return dispatched(vector_raw1, vector_raw2, n_elements);
319 }
320 # endif
321 #endif
322 ElementType multiplication_sum = 0;
323 for (size_t i = 0; i < n_elements; ++i) {
324 multiplication_sum += vector_raw1[i] * vector_raw2[i];
325 }
326 return 1 - multiplication_sum;
327 }
328
329 template <typename ElementType>
330 float
331 compute_distance_cosinecxx . . $ grn/distance/compute_distance_cosine(51ea083f54a0f1c0).(grn_obj *vector1, grn_obj *vector2)
332 {
333 auto vector_raw1 =
334 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#u.(vector1));
335 auto vector_raw2 =
336 reinterpret_cast<const ElementType *>(GRN_BULK_HEADcxx . . $ _grn_obj#$anonymous_type_0#$anonymous_type_1#head.(vector2));
337 auto n_elements = GRN_BULK_VSIZEcxx . . $ _grn_obj#$anonymous_type_0#$anonymous_type_1#head.(vector1) / sizeof(ElementType);
338 #ifdef GRN_WITH_SIMDcxx . . $ `build/config.h:71:9`!
339 # ifdef GRN_WITH_SIMSIMD
340 if (use_simsimd &&
341 (sizeof(ElementType) * n_elements * 2) >= use_simd_threshold) {
342 # ifdef GRN_WITH_SIMD_AVX512
343 if (simsimd::capabilities & simsimd_cap_x86_avx512_k) {
344 return simsimd::compute_distance_cosine_avx512(vector_raw1,
345 vector_raw2,
346 n_elements);
347 }
348 # endif
349 # ifdef GRN_WITH_SIMD_NEON64
350 if (simsimd::capabilities & simsimd_cap_arm_neon_k) {
351 return simsimd::compute_distance_cosine_neon(vector_raw1,
352 vector_raw2,
353 n_elements);
354 }
355 # endif
356 return simsimd::compute_distance_cosine_serial(vector_raw1,
357 vector_raw2,
358 n_elements);
359 }
360 # endif
361 # ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`!
362 if (use_xsimdcxx . . $ grn/distance/use_xsimd. &&
363 (sizeof(ElementType) * n_elements * 2) >= use_simd_thresholdcxx . . $ grn/distance/use_simd_threshold.) {
364 auto dispatched = xsimdcxx . . $ xsimd/::dispatchcxx . . $ xsimd/dispatch(1323d22f5d47b102).<xsimdcxx . . $ xsimd/::arch_listcxx . . $ xsimd/arch_list#<
365 # ifdef GRN_WITH_SIMD_AVX512cxx . . $ `build/config.h:74:9`!
366 xsimdcxx . . $ xsimd/::avx512dqcxx . . $ xsimd/avx512dq#,
367 # endif
368 # ifdef GRN_WITH_SIMD_AVX2cxx . . $ `build/config.h:73:9`!
369 xsimdcxx . . $ xsimd/::avx2cxx . . $ xsimd/avx2#,
370 # endif
371 # ifdef GRN_WITH_SIMD_AVXcxx . . $ `build/config.h:72:9`!
372 xsimdcxx . . $ xsimd/::avxcxx . . $ xsimd/avx#,
373 # endif
374 # ifdef GRN_WITH_SIMD_NEON64
375 xsimd::neon64,
376 # endif
377 xsimdcxx . . $ xsimd/::commoncxx . . $ xsimd/common#>>(cosinecxx . . $ grn/distance/cosine#{});
378 return dispatched(vector_raw1, vector_raw2, n_elements);
379 }
380 # endif
381 #endif
382 ElementType inner_product = 0;
383 ElementType square_sum1 = 0;
384 ElementType square_sum2 = 0;
385 for (size_t i = 0; i < n_elements; ++i) {
386 ElementType value1 = vector_raw1[i];
387 ElementType value2 = vector_raw2[i];
388 inner_product += value1 * value2;
389 square_sum1 += value1 * value1;
390 square_sum2 += value2 * value2;
391 }
392 if (numericcxx . . $ grn/numeric/::is_zerocxx . . $ grn/numeric/is_zero(a142b9e4da30b6a4).(inner_product)) {
393 return 1;
394 } else {
395 return 1 - (inner_product /
396 (stdcxx . . $ std/::sqrtcxx . . $ std/sqrt(a358fd9634d01384).(square_sum1) * stdcxx . . $ std/::sqrtcxx . . $ std/sqrt(a358fd9634d01384).(square_sum2)));
397 }
398 }
399 } // namespace distance
400 } // namespace grn