usearch 0.22.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/.github/workflows/prerelease.yml +3 -3
  2. package/.releaserc +9 -0
  3. package/.vscode/launch.json +15 -11
  4. package/.vscode/settings.json +12 -2
  5. package/.vscode/tasks.json +4 -4
  6. package/CMakeLists.txt +6 -1
  7. package/Cargo.toml +2 -2
  8. package/README.md +39 -46
  9. package/build.rs +2 -2
  10. package/c/CMakeLists.txt +48 -0
  11. package/c/README.md +34 -0
  12. package/c/lib.cpp +83 -72
  13. package/c/test.c +20 -20
  14. package/c/usearch.h +132 -32
  15. package/conanfile.py +1 -1
  16. package/cpp/CMakeLists.txt +3 -1
  17. package/cpp/README.md +16 -16
  18. package/cpp/bench.cpp +50 -126
  19. package/cpp/test.cpp +164 -77
  20. package/docs/benchmarks.md +26 -5
  21. package/docs/c/reference.rst +3 -3
  22. package/docs/compilation.md +49 -45
  23. package/docs/cpp/index.rst +2 -2
  24. package/docs/cpp/reference.rst +13 -3
  25. package/docs/index.rst +8 -8
  26. package/docs/java/reference.rst +1 -1
  27. package/golang/lib.go +275 -141
  28. package/golang/lib_test.go +105 -0
  29. package/include/usearch/index.hpp +1733 -1659
  30. package/include/usearch/index_dense.hpp +1400 -0
  31. package/include/usearch/index_plugins.hpp +1263 -0
  32. package/java/README.md +1 -1
  33. package/java/cloud/unum/usearch/Index.java +10 -10
  34. package/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp +34 -37
  35. package/java/test/IndexTest.java +1 -1
  36. package/javascript/README.md +7 -7
  37. package/javascript/lib.cpp +94 -55
  38. package/javascript/test.js +25 -11
  39. package/javascript/usearch.d.ts +87 -0
  40. package/javascript/usearch.js +2 -118
  41. package/objc/USearchObjective.mm +57 -31
  42. package/objc/include/USearchObjective.h +32 -22
  43. package/package.json +5 -3
  44. package/pyproject.toml +3 -3
  45. package/python/README.md +23 -14
  46. package/python/lib.cpp +472 -444
  47. package/python/scripts/bench.ipynb +3 -2
  48. package/python/scripts/bench.py +4 -4
  49. package/python/scripts/index_faiss.py +5 -5
  50. package/python/scripts/join.py +4 -4
  51. package/python/scripts/test.py +152 -61
  52. package/python/scripts/test_jit.py +10 -12
  53. package/python/scripts/test_sparse.py +32 -0
  54. package/python/usearch/client.py +26 -28
  55. package/python/usearch/eval.py +73 -23
  56. package/python/usearch/index.py +520 -228
  57. package/python/usearch/io.py +57 -25
  58. package/python/usearch/numba.py +4 -4
  59. package/python/usearch/server.py +10 -10
  60. package/rust/lib.cpp +22 -27
  61. package/rust/lib.hpp +11 -12
  62. package/rust/lib.rs +3 -3
  63. package/setup.py +5 -2
  64. package/simsimd/.github/workflows/prerelease.yml +73 -0
  65. package/simsimd/.github/workflows/release.yml +110 -0
  66. package/simsimd/.github/workflows/update_version.sh +3 -0
  67. package/simsimd/.releaserc +77 -0
  68. package/simsimd/.vscode/launch.json +27 -1
  69. package/simsimd/.vscode/settings.json +22 -1
  70. package/simsimd/.vscode/tasks.json +6 -0
  71. package/simsimd/CMakeLists.txt +3 -2
  72. package/simsimd/README.md +82 -46
  73. package/simsimd/bench.cxx +63 -24
  74. package/simsimd/include/simsimd/simsimd.h +88 -66
  75. package/simsimd/include/simsimd/simsimd_chem.h +126 -0
  76. package/simsimd/package.json +8 -0
  77. package/simsimd/pyproject.toml +42 -0
  78. package/simsimd/python/python.c +81 -0
  79. package/simsimd/python/run.sh +1 -0
  80. package/simsimd/python/test.c +109 -0
  81. package/simsimd/python/test.py +23 -0
  82. package/simsimd/setup.py +44 -0
  83. package/swift/Index+Sugar.swift +75 -23
  84. package/swift/README.md +26 -2
  85. package/swift/Test.swift +19 -7
  86. package/swift/USearch.swift +2 -2
  87. package/tsconfig.json +8 -0
  88. package/wasm/CMakeLists.txt +1 -0
  89. package/wasmer.toml +5 -1
  90. package/wolfram/CMakeLists.txt +1 -0
  91. package/wolfram/lib.cpp +11 -13
  92. package/golang/usearch.h +0 -9
  93. package/golang/usearch_par_test.go +0 -40
  94. package/golang/usearch_test.go +0 -82
  95. package/include/usearch/index_punned_dense.hpp +0 -871
  96. package/include/usearch/index_punned_helpers.hpp +0 -566
@@ -1,566 +0,0 @@
1
- #pragma once
2
- #include <stdlib.h> // `aligned_alloc`
3
-
4
- #include <cstring> // `std::strncmp`
5
- #include <numeric> // `std::iota`
6
- #include <thread> // `std::thread`
7
- #include <vector> // `std::vector`
8
-
9
- #include <usearch/index.hpp> // `expected_gt` and macros
10
-
11
- #if USEARCH_USE_OPENMP
12
- #include <omp.h> // `omp_get_num_threads()`
13
- #endif
14
-
15
- #if defined(USEARCH_DEFINED_LINUX)
16
- #include <sys/auxv.h> // `getauxval()`
17
- #endif
18
-
19
- #if defined(USEARCH_DEFINED_ARM)
20
- #include <arm_fp16.h> // `__fp16`
21
- #endif
22
-
23
- #if !defined(USEARCH_USE_NATIVE_F16)
24
- #if defined(__AVX512F__)
25
- #define USEARCH_USE_NATIVE_F16 1
26
- #elif defined(USEARCH_DEFINED_ARM)
27
- #define USEARCH_USE_NATIVE_F16 1
28
- #else
29
- #define USEARCH_USE_NATIVE_F16 0
30
- #include <fp16/fp16.h>
31
- #endif
32
- #else
33
- #define USEARCH_USE_NATIVE_F16 0
34
- #include <fp16/fp16.h>
35
- #endif
36
-
37
- #if !defined(USEARCH_USE_SIMSIMD)
38
- #define USEARCH_USE_SIMSIMD 0
39
- #endif
40
-
41
- #if USEARCH_USE_SIMSIMD
42
- #include <simsimd/simsimd.h>
43
- #endif
44
-
45
- namespace unum {
46
- namespace usearch {
47
-
48
- template <typename at, typename compare_at> inline at clamp(at v, at lo, at hi, compare_at comp) noexcept {
49
- return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
50
- }
51
- template <typename at> inline at clamp(at v, at lo, at hi) noexcept {
52
- return usearch::clamp(v, lo, hi, std::less<at>{});
53
- }
54
-
55
- inline bool str_equals(char const* begin, std::size_t len, char const* other_begin) noexcept {
56
- std::size_t other_len = std::strlen(other_begin);
57
- return len == other_len && std::strncmp(begin, other_begin, len) == 0;
58
- }
59
-
60
- enum class isa_t {
61
- auto_k,
62
- neon_k,
63
- sve_k,
64
- avx2_k,
65
- avx512_k,
66
- };
67
-
68
- inline char const* isa_name(isa_t isa) noexcept {
69
- switch (isa) {
70
- case isa_t::auto_k: return "auto";
71
- case isa_t::neon_k: return "neon";
72
- case isa_t::sve_k: return "sve";
73
- case isa_t::avx2_k: return "avx2";
74
- case isa_t::avx512_k: return "avx512";
75
- default: return "";
76
- }
77
- }
78
-
79
- inline bool hardware_supports(isa_t isa) noexcept {
80
- #if defined(USEARCH_DEFINED_ARM) && defined(USEARCH_DEFINED_LINUX)
81
- unsigned long capabilities = getauxval(AT_HWCAP);
82
- switch (isa) {
83
- case isa_t::neon_k: return true; // Must be supported on 64-bit Arm
84
- case isa_t::sve_k: return capabilities & HWCAP_SVE;
85
- default: return false;
86
- }
87
- #endif
88
-
89
- #if defined(USEARCH_DEFINED_X86) && defined(USEARCH_DEFINED_GCC)
90
- __builtin_cpu_init();
91
- switch (isa) {
92
- case isa_t::avx2_k: return __builtin_cpu_supports("avx2");
93
- case isa_t::avx512_k: return __builtin_cpu_supports("avx512f");
94
- default: return false;
95
- }
96
- #endif
97
-
98
- (void)isa;
99
- return false;
100
- }
101
-
102
- inline std::size_t bytes_per_scalar(scalar_kind_t accuracy) noexcept {
103
- switch (accuracy) {
104
- case scalar_kind_t::f32_k: return 4;
105
- case scalar_kind_t::f16_k: return 2;
106
- case scalar_kind_t::f64_k: return 8;
107
- case scalar_kind_t::f8_k: return 1;
108
- case scalar_kind_t::b1x8_k: return 1;
109
- default: return 0;
110
- }
111
- }
112
-
113
- inline char const* scalar_kind_name(scalar_kind_t accuracy) noexcept {
114
- switch (accuracy) {
115
- case scalar_kind_t::f32_k: return "f32";
116
- case scalar_kind_t::f16_k: return "f16";
117
- case scalar_kind_t::f64_k: return "f64";
118
- case scalar_kind_t::f8_k: return "f8";
119
- case scalar_kind_t::b1x8_k: return "b1x8";
120
- default: return "";
121
- }
122
- }
123
-
124
- inline char const* metric_kind_name(metric_kind_t metric) noexcept {
125
- switch (metric) {
126
- case metric_kind_t::unknown_k: return "unknown";
127
- case metric_kind_t::ip_k: return "ip";
128
- case metric_kind_t::cos_k: return "cos";
129
- case metric_kind_t::l2sq_k: return "l2sq";
130
- case metric_kind_t::pearson_k: return "pearson";
131
- case metric_kind_t::haversine_k: return "haversine";
132
- case metric_kind_t::jaccard_k: return "jaccard";
133
- case metric_kind_t::hamming_k: return "hamming";
134
- case metric_kind_t::tanimoto_k: return "tanimoto";
135
- case metric_kind_t::sorensen_k: return "sorensen";
136
- }
137
- return "";
138
- }
139
- inline expected_gt<scalar_kind_t> scalar_kind_from_name(char const* name, std::size_t len) {
140
- expected_gt<scalar_kind_t> parsed;
141
- if (str_equals(name, len, "f32"))
142
- parsed.result = scalar_kind_t::f32_k;
143
- else if (str_equals(name, len, "f64"))
144
- parsed.result = scalar_kind_t::f64_k;
145
- else if (str_equals(name, len, "f16"))
146
- parsed.result = scalar_kind_t::f16_k;
147
- else if (str_equals(name, len, "f8"))
148
- parsed.result = scalar_kind_t::f8_k;
149
- else
150
- parsed.failed("Unknown type, choose: f32, f16, f64, f8");
151
- return parsed;
152
- }
153
-
154
- inline expected_gt<metric_kind_t> metric_from_name(char const* name, std::size_t len) {
155
- expected_gt<metric_kind_t> parsed;
156
- if (str_equals(name, len, "l2sq") || str_equals(name, len, "euclidean_sq")) {
157
- parsed.result = metric_kind_t::l2sq_k;
158
- } else if (str_equals(name, len, "ip") || str_equals(name, len, "inner") || str_equals(name, len, "dot")) {
159
- parsed.result = metric_kind_t::ip_k;
160
- } else if (str_equals(name, len, "cos") || str_equals(name, len, "angular")) {
161
- parsed.result = metric_kind_t::cos_k;
162
- } else if (str_equals(name, len, "haversine")) {
163
- parsed.result = metric_kind_t::haversine_k;
164
- } else if (str_equals(name, len, "pearson")) {
165
- parsed.result = metric_kind_t::pearson_k;
166
- } else if (str_equals(name, len, "hamming")) {
167
- parsed.result = metric_kind_t::hamming_k;
168
- } else if (str_equals(name, len, "tanimoto")) {
169
- parsed.result = metric_kind_t::tanimoto_k;
170
- } else if (str_equals(name, len, "sorensen")) {
171
- parsed.result = metric_kind_t::sorensen_k;
172
- } else
173
- parsed.failed(
174
- "Unknown distance, choose: l2sq, ip, cos, haversine, jaccard, pearson, hamming, tanimoto, sorensen");
175
- return parsed;
176
- }
177
-
178
- using punned_distance_t = float;
179
- using punned_vector_view_t = span_gt<byte_t const>;
180
-
181
- class f8_bits_t;
182
- class f16_bits_t;
183
-
184
- #if USEARCH_USE_NATIVE_F16
185
- #if defined(USEARCH_DEFINED_ARM)
186
- using f16_native_t = __fp16;
187
- #else
188
- using f16_native_t = _Float16;
189
- #endif
190
- using f16_t = f16_native_t;
191
- #else
192
- using f16_native_t = void;
193
- using f16_t = f16_bits_t;
194
- #endif
195
-
196
- inline float f16_to_f32(std::uint16_t u16) noexcept {
197
- #if USEARCH_USE_NATIVE_F16
198
- f16_native_t f16;
199
- std::memcpy(&f16, &u16, sizeof(std::uint16_t));
200
- return float(f16);
201
- #else
202
- return fp16_ieee_to_fp32_value(u16);
203
- #endif
204
- }
205
-
206
- inline std::uint16_t f32_to_f16(float f32) noexcept {
207
- #if USEARCH_USE_NATIVE_F16
208
- f16_native_t f16 = f16_native_t(f32);
209
- std::uint16_t u16;
210
- std::memcpy(&u16, &f16, sizeof(std::uint16_t));
211
- return u16;
212
- #else
213
- return fp16_ieee_from_fp32_value(f32);
214
- #endif
215
- }
216
-
217
- class f16_bits_t {
218
- std::uint16_t uint16_{};
219
-
220
- public:
221
- inline f16_bits_t() noexcept : uint16_(0) {}
222
- inline f16_bits_t(f16_bits_t&&) = default;
223
- inline f16_bits_t& operator=(f16_bits_t&&) = default;
224
- inline f16_bits_t(f16_bits_t const&) = default;
225
- inline f16_bits_t& operator=(f16_bits_t const&) = default;
226
-
227
- inline operator float() const noexcept { return f16_to_f32(uint16_); }
228
- inline explicit operator bool() const noexcept { return f16_to_f32(uint16_) > 0.5f; }
229
-
230
- inline f16_bits_t(f8_bits_t) noexcept;
231
- inline f16_bits_t(bool v) noexcept : uint16_(f32_to_f16(v)) {}
232
- inline f16_bits_t(float v) noexcept : uint16_(f32_to_f16(v)) {}
233
- inline f16_bits_t(double v) noexcept : uint16_(f32_to_f16(v)) {}
234
-
235
- inline f16_bits_t operator+(f16_bits_t other) const noexcept { return {float(*this) + float(other)}; }
236
- inline f16_bits_t operator-(f16_bits_t other) const noexcept { return {float(*this) - float(other)}; }
237
- inline f16_bits_t operator*(f16_bits_t other) const noexcept { return {float(*this) * float(other)}; }
238
- inline f16_bits_t operator/(f16_bits_t other) const noexcept { return {float(*this) / float(other)}; }
239
- inline f16_bits_t operator+(float other) const noexcept { return {float(*this) + other}; }
240
- inline f16_bits_t operator-(float other) const noexcept { return {float(*this) - other}; }
241
- inline f16_bits_t operator*(float other) const noexcept { return {float(*this) * other}; }
242
- inline f16_bits_t operator/(float other) const noexcept { return {float(*this) / other}; }
243
- inline f16_bits_t operator+(double other) const noexcept { return {float(*this) + other}; }
244
- inline f16_bits_t operator-(double other) const noexcept { return {float(*this) - other}; }
245
- inline f16_bits_t operator*(double other) const noexcept { return {float(*this) * other}; }
246
- inline f16_bits_t operator/(double other) const noexcept { return {float(*this) / other}; }
247
-
248
- inline f16_bits_t& operator+=(float v) noexcept {
249
- uint16_ = f32_to_f16(v + f16_to_f32(uint16_));
250
- return *this;
251
- }
252
-
253
- inline f16_bits_t& operator-=(float v) noexcept {
254
- uint16_ = f32_to_f16(v - f16_to_f32(uint16_));
255
- return *this;
256
- }
257
-
258
- inline f16_bits_t& operator*=(float v) noexcept {
259
- uint16_ = f32_to_f16(v * f16_to_f32(uint16_));
260
- return *this;
261
- }
262
-
263
- inline f16_bits_t& operator/=(float v) noexcept {
264
- uint16_ = f32_to_f16(v / f16_to_f32(uint16_));
265
- return *this;
266
- }
267
- };
268
-
269
- /**
270
- * @brief Numeric type for uniformly-distributed floating point
271
- * values within [-1,1] range, quantized to integers [-100,100].
272
- */
273
- class f8_bits_t {
274
- std::int8_t int8_{};
275
-
276
- public:
277
- constexpr static float divisor_k = 100.f;
278
- constexpr static std::int8_t min_k = -100;
279
- constexpr static std::int8_t max_k = 100;
280
-
281
- inline f8_bits_t() noexcept : int8_(0) {}
282
- inline f8_bits_t(bool v) noexcept : int8_(v ? max_k : 0) {}
283
-
284
- inline f8_bits_t(f8_bits_t&&) = default;
285
- inline f8_bits_t& operator=(f8_bits_t&&) = default;
286
- inline f8_bits_t(f8_bits_t const&) = default;
287
- inline f8_bits_t& operator=(f8_bits_t const&) = default;
288
-
289
- inline operator float() const noexcept { return float(int8_) / divisor_k; }
290
- inline operator f16_t() const noexcept { return float(int8_) / divisor_k; }
291
- inline operator double() const noexcept { return double(int8_) / divisor_k; }
292
- inline explicit operator bool() const noexcept { return int8_ > (max_k / 2); }
293
- inline explicit operator std::int8_t() const noexcept { return int8_; }
294
- inline explicit operator std::int16_t() const noexcept { return int8_; }
295
- inline explicit operator std::int32_t() const noexcept { return int8_; }
296
- inline explicit operator std::int64_t() const noexcept { return int8_; }
297
-
298
- inline f8_bits_t(f16_t v)
299
- : int8_(usearch::clamp<std::int8_t>(static_cast<std::int8_t>(v * divisor_k), min_k, max_k)) {}
300
- inline f8_bits_t(float v)
301
- : int8_(usearch::clamp<std::int8_t>(static_cast<std::int8_t>(v * divisor_k), min_k, max_k)) {}
302
- inline f8_bits_t(double v)
303
- : int8_(usearch::clamp<std::int8_t>(static_cast<std::int8_t>(v * divisor_k), min_k, max_k)) {}
304
- };
305
-
306
- inline f16_bits_t::f16_bits_t(f8_bits_t v) noexcept : f16_bits_t(float(v)) {}
307
-
308
- struct uuid_t {
309
- std::uint8_t octets[16];
310
- };
311
-
312
- class executor_stl_t {
313
- std::size_t threads_count_{};
314
-
315
- public:
316
- executor_stl_t(std::size_t threads_count = 0) noexcept
317
- : threads_count_(threads_count ? threads_count : std::thread::hardware_concurrency()) {}
318
-
319
- std::size_t size() const noexcept { return threads_count_; }
320
-
321
- template <typename thread_aware_function_at>
322
- void execute_bulk(std::size_t tasks, thread_aware_function_at&& thread_aware_function) noexcept(false) {
323
- std::vector<std::thread> threads_pool;
324
- std::size_t tasks_per_thread = (tasks / threads_count_) + ((tasks % threads_count_) != 0);
325
- for (std::size_t thread_idx = 0; thread_idx != threads_count_; ++thread_idx) {
326
- threads_pool.emplace_back([=]() {
327
- for (std::size_t task_idx = thread_idx * tasks_per_thread;
328
- task_idx < (std::min)(tasks, thread_idx * tasks_per_thread + tasks_per_thread); ++task_idx)
329
- thread_aware_function(thread_idx, task_idx);
330
- });
331
- }
332
- for (std::size_t thread_idx = 0; thread_idx != threads_count_; ++thread_idx)
333
- threads_pool[thread_idx].join();
334
- }
335
-
336
- template <typename thread_aware_function_at>
337
- void execute_bulk(thread_aware_function_at&& thread_aware_function) noexcept(false) {
338
- std::vector<std::thread> threads_pool;
339
- for (std::size_t thread_idx = 0; thread_idx != threads_count_; ++thread_idx)
340
- threads_pool.emplace_back([=]() { thread_aware_function(thread_idx); });
341
- for (std::size_t thread_idx = 0; thread_idx != threads_count_; ++thread_idx)
342
- threads_pool[thread_idx].join();
343
- }
344
- };
345
-
346
- #if USEARCH_USE_OPENMP
347
-
348
- class executor_openmp_t {
349
- public:
350
- executor_openmp_t(std::size_t threads_count = 0) noexcept {
351
- omp_set_num_threads(threads_count ? threads_count : std::thread::hardware_concurrency());
352
- }
353
-
354
- std::size_t size() const noexcept { return omp_get_num_threads(); }
355
-
356
- template <typename thread_aware_function_at>
357
- void execute_bulk(std::size_t tasks, thread_aware_function_at&& thread_aware_function) noexcept(false) {
358
- #pragma omp parallel for schedule(dynamic)
359
- for (std::size_t i = 0; i != tasks; ++i)
360
- thread_aware_function(omp_get_thread_num(), i);
361
- }
362
-
363
- template <typename thread_aware_function_at>
364
- void execute_bulk(thread_aware_function_at&& thread_aware_function) noexcept(false) {
365
- #pragma omp parallel
366
- { thread_aware_function(omp_get_thread_num()); }
367
- }
368
- };
369
-
370
- using executor_default_t = executor_openmp_t;
371
-
372
- #else
373
-
374
- using executor_default_t = executor_stl_t;
375
-
376
- #endif
377
-
378
- /**
379
- * @brief Uses OS-specific APIs for aligned memory allocations.
380
- */
381
- template <typename element_at = char, std::size_t alignment_ak = 64> //
382
- class aligned_allocator_gt {
383
- public:
384
- using value_type = element_at;
385
- using size_type = std::size_t;
386
- using pointer = element_at*;
387
- using const_pointer = element_at const*;
388
- template <typename other_element_at> struct rebind {
389
- using other = aligned_allocator_gt<other_element_at>;
390
- };
391
-
392
- constexpr std::size_t alignment() const { return alignment_ak; }
393
-
394
- pointer allocate(size_type length) const {
395
- std::size_t length_bytes = alignment_ak * divide_round_up<alignment_ak>(length * sizeof(value_type));
396
- std::size_t alignment = alignment_ak;
397
- // void* result = nullptr;
398
- // int status = posix_memalign(&result, alignment, length_bytes);
399
- // return status == 0 ? (pointer)result : nullptr;
400
- #if defined(USEARCH_DEFINED_WINDOWS)
401
- return (pointer)_aligned_malloc(length_bytes, alignment);
402
- #else
403
- return (pointer)aligned_alloc(alignment, length_bytes);
404
- #endif
405
- }
406
-
407
- void deallocate(pointer begin, size_type) const {
408
- #if defined(USEARCH_DEFINED_WINDOWS)
409
- _aligned_free(begin);
410
- #else
411
- free(begin);
412
- #endif
413
- }
414
- };
415
-
416
- using aligned_allocator_t = aligned_allocator_gt<>;
417
-
418
- #if !defined(USEARCH_DEFINED_WINDOWS)
419
-
420
- /**
421
- * @brief Memory-mapping allocator designed for "alloc many, free at once" usage patterns.
422
- * Thread-safe.
423
- *
424
- * Using this memory allocator won't affect your overall speed much, as that is not the bottleneck.
425
- * However, it can drastically improve memory usage especcially for huge indexes of small vectors.
426
- */
427
- template <std::size_t alignment_ak = 1> class memory_mapping_allocator_gt {
428
-
429
- static constexpr std::size_t min_size() { return 1024 * 1024 * 4; }
430
- static constexpr std::size_t head_size() {
431
- /// Pointer to the the previous arena and the size of the current one.
432
- return divide_round_up<alignment_ak>(sizeof(byte_t*) + sizeof(std::size_t)) * alignment_ak;
433
- }
434
-
435
- std::mutex mutex_;
436
- byte_t* last_arena_ = nullptr;
437
- std::size_t last_usage_ = head_size();
438
- std::size_t last_capacity_ = min_size();
439
-
440
- void reset() noexcept {
441
- byte_t* last_arena = last_arena_;
442
- while (last_arena) {
443
- byte_t* previous_arena;
444
- std::memcpy(&previous_arena, last_arena, sizeof(byte_t*));
445
- std::size_t current_size;
446
- std::memcpy(&current_size, last_arena + sizeof(byte_t*), sizeof(std::size_t));
447
- munmap(last_arena, current_size);
448
- last_arena = previous_arena;
449
- }
450
-
451
- // Clear the references:
452
- last_arena_ = nullptr;
453
- last_usage_ = head_size();
454
- last_capacity_ = min_size();
455
- }
456
-
457
- public:
458
- using value_type = byte_t;
459
- using size_type = std::size_t;
460
- using pointer = byte_t*;
461
- using const_pointer = byte_t const*;
462
-
463
- memory_mapping_allocator_gt() = default;
464
- memory_mapping_allocator_gt(memory_mapping_allocator_gt&& other) noexcept
465
- : last_arena_(other.last_arena_), last_usage_(other.last_usage_), last_capacity_(other.last_capacity_) {}
466
- memory_mapping_allocator_gt& operator=(memory_mapping_allocator_gt&& other) noexcept {
467
- std::swap(last_arena_, other.last_arena_);
468
- std::swap(last_usage_, other.last_usage_);
469
- std::swap(last_capacity_, other.last_capacity_);
470
- return *this;
471
- }
472
-
473
- memory_mapping_allocator_gt(memory_mapping_allocator_gt const&) noexcept {}
474
- memory_mapping_allocator_gt& operator=(memory_mapping_allocator_gt const&) noexcept {
475
- reset();
476
- return *this;
477
- }
478
-
479
- ~memory_mapping_allocator_gt() noexcept { reset(); }
480
-
481
- inline byte_t* allocate(std::size_t count_bytes) noexcept {
482
- count_bytes = divide_round_up<alignment_ak>(count_bytes) * alignment_ak;
483
-
484
- std::unique_lock<std::mutex> lock(mutex_);
485
- if (!last_arena_ || (last_usage_ + count_bytes > last_capacity_)) {
486
- std::size_t new_capacity = last_capacity_ * 2;
487
- int prot = PROT_WRITE | PROT_READ;
488
- int flags = MAP_PRIVATE | MAP_ANONYMOUS;
489
- byte_t* new_arena = (byte_t*)mmap(NULL, new_capacity, prot, flags, 0, 0);
490
- std::memcpy(new_arena, &last_arena_, sizeof(byte_t*));
491
- std::memcpy(new_arena + sizeof(byte_t*), &new_capacity, sizeof(std::size_t));
492
-
493
- last_arena_ = new_arena;
494
- last_capacity_ = new_capacity;
495
- last_usage_ = head_size();
496
- }
497
-
498
- return last_arena_ + exchange(last_usage_, last_usage_ + count_bytes);
499
- }
500
-
501
- /**
502
- * @warning The very first memory de-allocation discards all the arenas!
503
- */
504
- void deallocate(byte_t*, std::size_t) noexcept { reset(); }
505
- };
506
-
507
- using memory_mapping_allocator_t = memory_mapping_allocator_gt<>;
508
-
509
- #else
510
-
511
- using memory_mapping_allocator_t = aligned_allocator_t;
512
-
513
- #endif
514
-
515
- template <typename from_scalar_at, typename to_scalar_at> struct cast_gt {
516
- inline bool operator()(byte_t const* input, std::size_t dimensions, byte_t* output) const {
517
- from_scalar_at const* typed_input = reinterpret_cast<from_scalar_at const*>(input);
518
- to_scalar_at* typed_output = reinterpret_cast<to_scalar_at*>(output);
519
- auto converter = [](from_scalar_at from) { return to_scalar_at(from); };
520
- std::transform(typed_input, typed_input + dimensions, typed_output, converter);
521
- return true;
522
- }
523
- };
524
-
525
- template <> struct cast_gt<f32_t, f32_t> {
526
- bool operator()(byte_t const*, std::size_t, byte_t*) const { return false; }
527
- };
528
-
529
- template <> struct cast_gt<f64_t, f64_t> {
530
- bool operator()(byte_t const*, std::size_t, byte_t*) const { return false; }
531
- };
532
-
533
- template <> struct cast_gt<f16_bits_t, f16_bits_t> {
534
- bool operator()(byte_t const*, std::size_t, byte_t*) const { return false; }
535
- };
536
-
537
- template <> struct cast_gt<f8_bits_t, f8_bits_t> {
538
- bool operator()(byte_t const*, std::size_t, byte_t*) const { return false; }
539
- };
540
-
541
- template <> struct cast_gt<b1x8_t, b1x8_t> {
542
- bool operator()(byte_t const*, std::size_t, byte_t*) const { return false; }
543
- };
544
-
545
- template <typename from_scalar_at> struct cast_gt<from_scalar_at, b1x8_t> {
546
- inline bool operator()(byte_t const* input, std::size_t dimensions, byte_t* output) const {
547
- from_scalar_at const* typed_input = reinterpret_cast<from_scalar_at const*>(input);
548
- unsigned char* typed_output = reinterpret_cast<unsigned char*>(output);
549
- for (std::size_t i = 0; i != dimensions; ++i)
550
- typed_output[i / CHAR_BIT] |= bool(typed_input[i]) ? (128 >> (i & (CHAR_BIT - 1))) : 0;
551
- return true;
552
- }
553
- };
554
-
555
- template <typename to_scalar_at> struct cast_gt<b1x8_t, to_scalar_at> {
556
- inline bool operator()(byte_t const* input, std::size_t dimensions, byte_t* output) const {
557
- unsigned char const* typed_input = reinterpret_cast<unsigned char const*>(input);
558
- to_scalar_at* typed_output = reinterpret_cast<to_scalar_at*>(output);
559
- for (std::size_t i = 0; i != dimensions; ++i)
560
- typed_output[i] = bool(typed_input[i / CHAR_BIT] & (128 >> (i & (CHAR_BIT - 1))));
561
- return true;
562
- }
563
- };
564
-
565
- } // namespace usearch
566
- } // namespace unum