| 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 | #include "grn_distance.hpp"cxx . . $ `<file>/lib/grn_distance.hpp`/ |
| 20 | |
| 21 | #include <groonga/bulk.hpp>cxx . . $ `<file>/include/groonga/bulk.hpp`/ |
| 22 | |
| 23 | #include "grn_ctx.hpp"cxx . . $ `<file>/lib/grn_ctx.hpp`/ |
| 24 | #include "grn_db.h"cxx . . $ `<file>/lib/grn_db.h`/ |
| 25 | |
| 26 | namespace grncxx . . $ grn/ { |
| 27 | namespace distancecxx . . $ grn/distance/ { |
| 28 | #ifdef GRN_WITH_SIMSIMD |
| 29 | bool use_simsimd = true; |
| 30 | namespace simsimd { |
| 31 | simsimd_capability_t capabilities = simsimd_cap_serial_k; |
| 32 | } |
| 33 | #else |
| 34 | bool use_simsimdcxx . . $ grn/distance/use_simsimd. = false; |
| 35 | #endif |
| 36 | |
| 37 | #ifdef GRN_WITH_XSIMDcxx . . $ `build/config.h:118:9`! |
| 38 | bool use_xsimdcxx . . $ grn/distance/use_xsimd. = true; |
| 39 | #else |
| 40 | bool use_xsimd = false; |
| 41 | #endif |
| 42 | } // namespace distance |
| 43 | } // namespace grn |
| 44 | |
| 45 | namespacecxx . . $ `$anonymous_namespace_lib/distance.cpp`/ { |
| 46 | bool |
| 47 | validate_vectorscxx . . $ `$anonymous_namespace_lib/distance.cpp`/validate_vectors(51723934e9143375).(grn_ctx *ctx, |
| 48 | grn_obj *vector1, |
| 49 | grn_obj *vector2, |
| 50 | const char *tag) |
| 51 | { |
| 52 | if (!grn_obj_is_number_family_vector(ctx, vector1)) { |
| 53 | grncxx . . $ grn/::TextBulkcxx . . $ grn/TextBulk# inspectedcxx . . $ grn/TextBulk#TextBulk(f33f77cd46f8f264).(ctx); |
| 54 | grn_inspect(ctx, *cxx . . $ grn/TextBulk#`operator*`(d5f0a9fb11b2b135).inspected, vector1); |
| 55 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 56 | "%s the 1st argument must be number family vector: %.*s", |
| 57 | tag, |
| 58 | static_cast<int>(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(*inspected)), |
| 59 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(*inspected)); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | if (!grn_obj_is_number_family_vector(ctx, vector2)) { |
| 64 | grncxx . . $ grn/::TextBulkcxx . . $ grn/TextBulk# inspectedcxx . . $ grn/TextBulk#TextBulk(f33f77cd46f8f264).(ctx); |
| 65 | grn_inspect(ctx, *cxx . . $ grn/TextBulk#`operator*`(d5f0a9fb11b2b135).inspected, vector2); |
| 66 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 67 | "%s the 2nd argument must be number family vector: %.*s", |
| 68 | tag, |
| 69 | static_cast<int>(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(*inspected)), |
| 70 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(*inspected)); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (vector1->headercxx . . $ _grn_obj#header..domain != vector2->headercxx . . $ _grn_obj#header..domain) { |
| 75 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 76 | "%s both vectors must be the same type: %s: %s", |
| 77 | tag, |
| 78 | grn_type_id_to_string_builtin(ctx, vector1->header.domain), |
| 79 | grn_type_id_to_string_builtin(ctx, vector2->header.domain)); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | auto n_elements1 = grn_vector_size(ctx, vector1); |
| 84 | auto n_elements2 = grn_vector_size(ctx, vector2); |
| 85 | if (n_elements1 != n_elements2) { |
| 86 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 87 | "%s both vectors must be the same size: %d: %d", |
| 88 | tag, |
| 89 | n_elements1, |
| 90 | n_elements2); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | template <typename ElementType> |
| 98 | float |
| 99 | compute_distance_cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_cosine(540677c5b0a4fe92).(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 100 | { |
| 101 | return grncxx . . $ grn/::distancecxx . . $ grn/distance/::compute_distance_cosinecxx . . $ grn/distance/compute_distance_cosine(51ea083f54a0f1c0).<ElementType>(vector1, |
| 102 | vector2); |
| 103 | } |
| 104 | |
| 105 | template <typename ElementType> |
| 106 | float |
| 107 | compute_distance_inner_productcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_inner_product(540677c5b0a4fe92).(grn_ctx *ctx, |
| 108 | grn_obj *vector1, |
| 109 | grn_obj *vector2) |
| 110 | { |
| 111 | return grncxx . . $ grn/::distancecxx . . $ grn/distance/::compute_distance_inner_productcxx . . $ grn/distance/compute_distance_inner_product(51ea083f54a0f1c0).<ElementType>(vector1, |
| 112 | vector2); |
| 113 | } |
| 114 | |
| 115 | template <typename ElementType> |
| 116 | float |
| 117 | compute_distance_l1_normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l1_norm(540677c5b0a4fe92).(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 118 | { |
| 119 | return grncxx . . $ grn/::distancecxx . . $ grn/distance/::compute_difference_l1_normcxx . . $ grn/distance/compute_difference_l1_norm(51ea083f54a0f1c0).<ElementType>(vector1, |
| 120 | vector2); |
| 121 | } |
| 122 | |
| 123 | template <typename ElementType> |
| 124 | float |
| 125 | compute_distance_l2_norm_squaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l2_norm_squared(540677c5b0a4fe92).(grn_ctx *ctx, |
| 126 | grn_obj *vector1, |
| 127 | grn_obj *vector2) |
| 128 | { |
| 129 | return grncxx . . $ grn/::distancecxx . . $ grn/distance/::compute_difference_l2_norm_squaredcxx . . $ grn/distance/compute_difference_l2_norm_squared(51ea083f54a0f1c0).<ElementType>( |
| 130 | vector1, |
| 131 | vector2); |
| 132 | } |
| 133 | |
| 134 | enum class Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method# { |
| 135 | Cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#Cosine., |
| 136 | InnerProductcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#InnerProduct., |
| 137 | L1Normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L1Norm., |
| 138 | L2NormSquaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L2NormSquared., |
| 139 | }; |
| 140 | |
| 141 | template <typename ElementType, typename DistanceFunc> |
| 142 | void |
| 143 | distance_applycxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_apply(50ef572f491a43b6).(grn_ctx *ctx, |
| 144 | grn_applier_data *data, |
| 145 | grn_obj *table, |
| 146 | grn_obj *output_column, |
| 147 | grn_obj *input_column, |
| 148 | grn_id input_column_range, |
| 149 | grn_obj *literal, |
| 150 | const char *function_name, |
| 151 | DistanceFunc distance_func) |
| 152 | { |
| 153 | auto n_records = grn_table_size(ctx, table); |
| 154 | if (n_records == 0) { |
| 155 | return; |
| 156 | } |
| 157 | auto n_elements = GRN_BULK_VSIZEcxx . . $ `include/groonga/groonga.h:2642:9`!(literal) / sizeof(ElementType); |
| 158 | auto task_executor = grn_ctx_get_task_executorcxx . . $ grn_ctx_get_task_executor(a6ad17b0d499b7c8).(ctx); |
| 159 | auto n_workers = task_executor->get_n_workerscxx . . $ grn/TaskExecutor#get_n_workers(db52760e79bf9054).(); |
| 160 | if (n_workers == 0) { |
| 161 | n_workers = 1; |
| 162 | } |
| 163 | n_workers = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(n_workers, n_records); |
| 164 | int limit = (n_records == n_workers) ? 1 : ((n_records / n_workers) + 1); |
| 165 | for (size_t i = 0; i < n_workers; i++) { |
| 166 | auto execute = [&, i]() { |
| 167 | grn_obj input; |
| 168 | GRN_VALUE_FIX_SIZE_INITcxx . . $ `include/groonga/groonga.h:2738:9`!(&input, |
| 169 | GRN_OBJ_VECTORcxx . . $ `include/groonga/groonga.h:2734:9`! | GRN_OBJ_DO_SHALLOW_COPYcxx . . $ `include/groonga/groonga.h:2732:9`!, |
| 170 | input_column_range); |
| 171 | grn_obj output; |
| 172 | GRN_FLOAT_INITcxx . . $ `include/groonga/groonga.h:2864:9`!(&output, 0); |
| 173 | int offset = limit * i; |
| 174 | grn_ctx *task_ctx = ctx; |
| 175 | grn_ctx *child_ctx = nullptr; |
| 176 | if (task_executor->is_parallelcxx . . $ grn/TaskExecutor#is_parallel(c9857a73d68e59ba).()) { |
| 177 | task_ctx = child_ctx = grn_ctx_pull_child(ctx); |
| 178 | } |
| 179 | grncxx . . $ grn/::ChildCtxReleasercxx . . $ grn/ChildCtxReleaser# releasercxx . . $ grn/ChildCtxReleaser#ChildCtxReleaser(7e746d4b1777d1a6).(ctx, child_ctx); |
| 180 | auto cursor = grn_table_cursor_open(task_ctx, |
| 181 | table, |
| 182 | nullptr, |
| 183 | 0, |
| 184 | nullptr, |
| 185 | 0, |
| 186 | offset, |
| 187 | limit, |
| 188 | GRN_CURSOR_BY_IDcxx . . $ `include/groonga/table.h:280:9`!); |
| 189 | if (cursor) { |
| 190 | grn_id id; |
| 191 | while ((id = grn_table_cursor_next(task_ctx, cursor)) != GRN_ID_NILcxx . . $ `include/groonga/groonga.h:55:9`!) { |
| 192 | uint32_tcxx . . $ uint32_t# size; |
| 193 | auto value = grn_obj_get_value_(task_ctx, input_column, id, &size); |
| 194 | GRN_TEXT_SETcxx . . $ `include/groonga/groonga.h:2761:9`!(task_ctx, &input, value, size); |
| 195 | if ((GRN_BULK_VSIZEcxx . . $ `include/groonga/groonga.h:2642:9`!(&input) / sizeof(ElementType)) != n_elements) { |
| 196 | continue; |
| 197 | } |
| 198 | auto distance_raw = distance_func(task_ctx, &input, literal); |
| 199 | if (task_ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESS) { |
| 200 | continue; |
| 201 | } |
| 202 | GRN_FLOAT_SETcxx . . $ `include/groonga/groonga.h:2934:9`!(task_ctx, &output, distance_raw); |
| 203 | grn_obj_set_value(task_ctx, |
| 204 | output_column, |
| 205 | id, |
| 206 | &output, |
| 207 | GRN_OBJ_SETcxx . . $ `include/groonga/groonga.h:1466:9`!); |
| 208 | if (task_ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESS) { |
| 209 | continue; |
| 210 | } |
| 211 | } |
| 212 | grn_table_cursor_close(task_ctx, cursor); |
| 213 | } |
| 214 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(task_ctx, &input); |
| 215 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(task_ctx, &output); |
| 216 | return true; |
| 217 | }; |
| 218 | task_executor->executecxx . . $ grn/TaskExecutor#execute(61277e778d1aa7c1).(i, execute, function_name); |
| 219 | } |
| 220 | task_executor->wait_allcxx . . $ grn/TaskExecutor#wait_all(c9857a73d68e59ba).(); |
| 221 | } |
| 222 | |
| 223 | grn_rc |
| 224 | distance_appliercxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).(grn_ctx *ctx, |
| 225 | grn_applier_data *data, |
| 226 | Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method# method, |
| 227 | const char *function_name) |
| 228 | { |
| 229 | auto table = grn_applier_data_get_table(ctx, data); |
| 230 | auto output_column = grn_applier_data_get_output_column(ctx, data); |
| 231 | size_t n_args; |
| 232 | auto args = grn_applier_data_get_args(ctx, data, &n_args); |
| 233 | |
| 234 | if (n_args != 2) { |
| 235 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 236 | "%s: wrong number of arguments (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! " for 2)", |
| 237 | function_name, |
| 238 | n_args); |
| 239 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 240 | } |
| 241 | |
| 242 | auto input_column = args[0]; |
| 243 | if (!(grn_obj_is_vector_column(ctx, input_column) || |
| 244 | grn_obj_is_vector_accessor(ctx, input_column))) { |
| 245 | grncxx . . $ grn/::TextBulkcxx . . $ grn/TextBulk# inspectedcxx . . $ grn/TextBulk#TextBulk(f33f77cd46f8f264).(ctx); |
| 246 | grn_inspect(ctx, *cxx . . $ grn/TextBulk#`operator*`(d5f0a9fb11b2b135).inspected, input_column); |
| 247 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 248 | "%s: 1st argument must be a vector column or accessor: %.*s", |
| 249 | function_name, |
| 250 | static_cast<int>(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(*inspected)), |
| 251 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(*inspected)); |
| 252 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 253 | } |
| 254 | auto input_column_range = grn_obj_get_range(ctx, input_column); |
| 255 | switch (input_column_range) { |
| 256 | case GRN_DB_FLOAT32: |
| 257 | case GRN_DB_FLOAT: |
| 258 | break; |
| 259 | default: |
| 260 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 261 | "%s: 1st argument must be Float or Float32: %s", |
| 262 | function_name, |
| 263 | grn_type_id_to_string_builtin(ctx, input_column_range)); |
| 264 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 265 | } |
| 266 | |
| 267 | auto literal = args[1]; |
| 268 | grn_obj casted_literal_buffer; |
| 269 | grn_obj *casted_literal = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!; |
| 270 | if (grn_obj_is_uvector(ctx, literal) && |
| 271 | literal->headercxx . . $ _grn_obj#header..domain == input_column_range) { |
| 272 | /* We can use this as-is. */ |
| 273 | } else { |
| 274 | GRN_VALUE_FIX_SIZE_INITcxx . . $ `include/groonga/groonga.h:2738:9`!(&casted_literal_buffer, |
| 275 | GRN_OBJ_VECTORcxx . . $ `include/groonga/groonga.h:2734:9`!, |
| 276 | input_column_range); |
| 277 | if (grn_obj_cast(ctx, literal, &casted_literal_buffer, false) != |
| 278 | GRN_SUCCESS) { |
| 279 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &casted_literal_buffer); |
| 280 | grncxx . . $ grn/::TextBulkcxx . . $ grn/TextBulk# inspectedcxx . . $ grn/TextBulk#TextBulk(f33f77cd46f8f264).(ctx); |
| 281 | grn_inspect(ctx, *cxx . . $ grn/TextBulk#`operator*`(d5f0a9fb11b2b135).inspected, literal); |
| 282 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 283 | "%s: 2nd argument must be a Float or Float32 vector literal: %.*s", |
| 284 | function_name, |
| 285 | static_cast<int>(GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(*inspected)), |
| 286 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(*inspected)); |
| 287 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 288 | } |
| 289 | casted_literal = &casted_literal_buffer; |
| 290 | literal = casted_literal; |
| 291 | } |
| 292 | |
| 293 | switch (input_column_range) { |
| 294 | case GRN_DB_FLOAT32: |
| 295 | { |
| 296 | auto apply = [&](auto distance_func) { |
| 297 | return distance_applycxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_apply(50ef572f491a43b6).<float>(ctx, |
| 298 | data, |
| 299 | table, |
| 300 | output_column, |
| 301 | input_column, |
| 302 | input_column_range, |
| 303 | literal, |
| 304 | function_name, |
| 305 | distance_func); |
| 306 | }; |
| 307 | switch (method) { |
| 308 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::Cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#Cosine.: |
| 309 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_0#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 310 | return compute_distance_cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_cosine(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 311 | }); |
| 312 | break; |
| 313 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::InnerProductcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#InnerProduct.: |
| 314 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_0#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 315 | return compute_distance_inner_productcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_inner_product(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 316 | }); |
| 317 | break; |
| 318 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L1Normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L1Norm.: |
| 319 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_0#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 320 | return compute_distance_l1_normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l1_norm(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 321 | }); |
| 322 | break; |
| 323 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L2NormSquaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L2NormSquared.: |
| 324 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_0#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 325 | return compute_distance_l2_norm_squaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l2_norm_squared(540677c5b0a4fe92).<float>(ctx, |
| 326 | vector1, |
| 327 | vector2); |
| 328 | }); |
| 329 | break; |
| 330 | } |
| 331 | break; |
| 332 | } |
| 333 | case GRN_DB_FLOAT: |
| 334 | { |
| 335 | auto apply = [&](auto distance_func) { |
| 336 | distance_applycxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_apply(50ef572f491a43b6).<double>(ctx, |
| 337 | data, |
| 338 | table, |
| 339 | output_column, |
| 340 | input_column, |
| 341 | input_column_range, |
| 342 | literal, |
| 343 | function_name, |
| 344 | distance_func); |
| 345 | }; |
| 346 | switch (method) { |
| 347 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::Cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#Cosine.: |
| 348 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_1#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 349 | return compute_distance_cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_cosine(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 350 | }); |
| 351 | break; |
| 352 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::InnerProductcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#InnerProduct.: |
| 353 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_1#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 354 | return compute_distance_inner_productcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_inner_product(540677c5b0a4fe92).<double>(ctx, |
| 355 | vector1, |
| 356 | vector2); |
| 357 | }); |
| 358 | break; |
| 359 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L1Normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L1Norm.: |
| 360 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_1#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 361 | return compute_distance_l1_normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l1_norm(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 362 | }); |
| 363 | break; |
| 364 | case Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L2NormSquaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L2NormSquared.: |
| 365 | apply(cxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).$anonymous_type_1#`operator()`(be3b3d86c96651be).[](grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) { |
| 366 | return compute_distance_l2_norm_squaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l2_norm_squared(540677c5b0a4fe92).<double>(ctx, |
| 367 | vector1, |
| 368 | vector2); |
| 369 | }); |
| 370 | break; |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | if (literal == casted_literal) { |
| 379 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, casted_literal); |
| 380 | } |
| 381 | |
| 382 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 383 | } |
| 384 | } // namespace |
| 385 | |
| 386 | extern "C" void |
| 387 | grn_distance_init_external_libraries(void) |
| 388 | { |
| 389 | #ifdef GRN_WITH_SIMSIMD |
| 390 | grn::distance::simsimd::capabilities = simsimd_capabilities(); |
| 391 | #endif |
| 392 | } |
| 393 | |
| 394 | extern "C" void |
| 395 | grn_distance_init_from_env(void) |
| 396 | { |
| 397 | { |
| 398 | char grn_distance_simd_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!]; |
| 399 | grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_DISTANCE_SIMD", grn_distance_simd_env, GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!); |
| 400 | if (strcmp(grn_distance_simd_env, "simsimd") == 0) { |
| 401 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_simsimdcxx . . $ grn/distance/use_simsimd. = true; |
| 402 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_xsimdcxx . . $ grn/distance/use_xsimd. = false; |
| 403 | } else if (strcmp(grn_distance_simd_env, "xsimd") == 0) { |
| 404 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_simsimdcxx . . $ grn/distance/use_simsimd. = false; |
| 405 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_xsimdcxx . . $ grn/distance/use_xsimd. = true; |
| 406 | } else if (strcmp(grn_distance_simd_env, "no") == 0) { |
| 407 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_simsimdcxx . . $ grn/distance/use_simsimd. = false; |
| 408 | grncxx . . $ grn/::distancecxx . . $ grn/distance/::use_xsimdcxx . . $ grn/distance/use_xsimd. = false; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | extern "C" float |
| 414 | grn_distance_cosine(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 415 | { |
| 416 | const char *tag = "[distance][cosine]"; |
| 417 | float distance = 0.0; |
| 418 | |
| 419 | if (!validate_vectorscxx . . $ `$anonymous_namespace_lib/distance.cpp`/validate_vectors(51723934e9143375).(ctx, vector1, vector2, tag)) { |
| 420 | return distance; |
| 421 | } |
| 422 | |
| 423 | switch (vector1->headercxx . . $ _grn_obj#header..domain) { |
| 424 | case GRN_DB_FLOAT32: |
| 425 | distance = compute_distance_cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_cosine(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 426 | break; |
| 427 | case GRN_DB_FLOAT: |
| 428 | distance = compute_distance_cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_cosine(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 429 | break; |
| 430 | default: |
| 431 | // TODO: We should add support for all integer types |
| 432 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 433 | "%s unsupported type: %s", |
| 434 | tag, |
| 435 | grn_type_id_to_string_builtin(ctx, vector1->header.domain)); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | return distance; |
| 440 | } |
| 441 | |
| 442 | extern "C" grn_rc |
| 443 | grn_distance_cosine_applier(grn_ctx *ctx, grn_applier_data *data) |
| 444 | { |
| 445 | return distance_appliercxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).(ctx, data, Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::Cosinecxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#Cosine., "distance_cosine()"); |
| 446 | } |
| 447 | |
| 448 | extern "C" float |
| 449 | grn_distance_inner_product(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 450 | { |
| 451 | const char *tag = "[distance][inner-product]"; |
| 452 | float distance = 0.0; |
| 453 | |
| 454 | if (!validate_vectorscxx . . $ `$anonymous_namespace_lib/distance.cpp`/validate_vectors(51723934e9143375).(ctx, vector1, vector2, tag)) { |
| 455 | return distance; |
| 456 | } |
| 457 | |
| 458 | switch (vector1->headercxx . . $ _grn_obj#header..domain) { |
| 459 | case GRN_DB_FLOAT32: |
| 460 | distance = compute_distance_inner_productcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_inner_product(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 461 | break; |
| 462 | case GRN_DB_FLOAT: |
| 463 | distance = compute_distance_inner_productcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_inner_product(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 464 | break; |
| 465 | default: |
| 466 | // TODO: We should add support for all integer types |
| 467 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 468 | "%s unsupported type: %s", |
| 469 | tag, |
| 470 | grn_type_id_to_string_builtin(ctx, vector1->header.domain)); |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return distance; |
| 475 | } |
| 476 | |
| 477 | extern "C" grn_rc |
| 478 | grn_distance_inner_product_applier(grn_ctx *ctx, grn_applier_data *data) |
| 479 | { |
| 480 | return distance_appliercxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).(ctx, |
| 481 | data, |
| 482 | Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::InnerProductcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#InnerProduct., |
| 483 | "distance_inner_product()"); |
| 484 | } |
| 485 | |
| 486 | extern "C" float |
| 487 | grn_distance_l1_norm(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 488 | { |
| 489 | const char *tag = "[distance][l1-norm]"; |
| 490 | float distance = 0.0; |
| 491 | |
| 492 | if (!validate_vectorscxx . . $ `$anonymous_namespace_lib/distance.cpp`/validate_vectors(51723934e9143375).(ctx, vector1, vector2, tag)) { |
| 493 | return distance; |
| 494 | } |
| 495 | |
| 496 | switch (vector1->headercxx . . $ _grn_obj#header..domain) { |
| 497 | case GRN_DB_FLOAT32: |
| 498 | distance = compute_distance_l1_normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l1_norm(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 499 | break; |
| 500 | case GRN_DB_FLOAT: |
| 501 | distance = compute_distance_l1_normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l1_norm(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 502 | break; |
| 503 | default: |
| 504 | // TODO: We should add support for all integer types |
| 505 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 506 | "%s unsupported type: %s", |
| 507 | tag, |
| 508 | grn_type_id_to_string_builtin(ctx, vector1->header.domain)); |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | return distance; |
| 513 | } |
| 514 | |
| 515 | extern "C" grn_rc |
| 516 | grn_distance_l1_norm_applier(grn_ctx *ctx, grn_applier_data *data) |
| 517 | { |
| 518 | return distance_appliercxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).(ctx, data, Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L1Normcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L1Norm., "distance_l1_norm()"); |
| 519 | } |
| 520 | |
| 521 | extern "C" float |
| 522 | grn_distance_l2_norm_squared(grn_ctx *ctx, grn_obj *vector1, grn_obj *vector2) |
| 523 | { |
| 524 | const char *tag = "[distance][l2-norm-squared]"; |
| 525 | float distance = 0.0; |
| 526 | |
| 527 | if (!validate_vectorscxx . . $ `$anonymous_namespace_lib/distance.cpp`/validate_vectors(51723934e9143375).(ctx, vector1, vector2, tag)) { |
| 528 | return distance; |
| 529 | } |
| 530 | |
| 531 | switch (vector1->headercxx . . $ _grn_obj#header..domain) { |
| 532 | case GRN_DB_FLOAT32: |
| 533 | distance = compute_distance_l2_norm_squaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l2_norm_squared(540677c5b0a4fe92).<float>(ctx, vector1, vector2); |
| 534 | break; |
| 535 | case GRN_DB_FLOAT: |
| 536 | distance = compute_distance_l2_norm_squaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/compute_distance_l2_norm_squared(540677c5b0a4fe92).<double>(ctx, vector1, vector2); |
| 537 | break; |
| 538 | default: |
| 539 | // TODO: We should add support for all integer types |
| 540 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 541 | "%s unsupported type: %s", |
| 542 | tag, |
| 543 | grn_type_id_to_string_builtin(ctx, vector1->header.domain)); |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | return distance; |
| 548 | } |
| 549 | |
| 550 | extern "C" grn_rc |
| 551 | grn_distance_l2_norm_squared_applier(grn_ctx *ctx, grn_applier_data *data) |
| 552 | { |
| 553 | return distance_appliercxx . . $ `$anonymous_namespace_lib/distance.cpp`/distance_applier(7e4026ed2c9ac036).(ctx, |
| 554 | data, |
| 555 | Methodcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#::L2NormSquaredcxx . . $ `$anonymous_namespace_lib/distance.cpp`/Method#L2NormSquared., |
| 556 | "distance_l2_norm_squared()"); |
| 557 | } |