-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec-cpu.cpp
More file actions
4088 lines (3610 loc) · 142 KB
/
vec-cpu.cpp
File metadata and controls
4088 lines (3610 loc) · 142 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Curated by @PsyChip
* root@psychip.net
* April 2026
*
* vec-cpu - dead simple vector database (CPU mode, no GPU required)
*
* Usage: vec-cpu <name> <dim> [port]
* vec-cpu deploy [port]
* vec-cpu --deploy=name:dim[,...] [port]
*
* Creates an in-memory vector store. No CUDA dependency.
* Listens on:
* - TCP port (default 1920)
* - Windows: Named pipe \\.\pipe\vec_<name>
* - Linux: Unix socket /tmp/vec_<name>.sock
*
* VEC 2.0 binary frame protocol — see PROTOCOL-2.0.md for the byte-exact spec.
* request: F0 <2B ns_len> [ns] <CMD> <2B label_len> [label] <4B body_len> [body]
* response: <1B status> <4B body_len> [body] ; status 0=OK, 1=ERR
* CMD: 01=push 02=query 04=get 06=update 07=delete 08=label
* 09=undo 0A=save 0D=cluster 0E=distinct 0F=represent
* 10=info 11=qid 13=set_data 14=get_data
*
* Labels: filename-scheme, ≤2048 bytes, validated on write, lenient on load.
* Data: opaque blobs ≤100KB, requires a label.
* DISTINCT and REPRESENT return "not available in cpu mode".
*
* File format:
* .tensors [4B dim][4B count][4B deleted][1B fmt][count B alive][vectors][4B CRC32]
* .meta [4B count][per slot: 4B len + label bytes]
* .data [4B count][count B alive mask][per present slot: 4B len + bytes][4B CRC32]
*
* Features:
* - Read-only mode if file is not writable
* - Disk space check before save
* - File integrity check/repair (--check, --repair)
*
* Ctrl+C saves before exit.
*
* Build (Windows):
* cl /O2 /arch:AVX2 /fp:fast /EHsc vec-cpu.cpp /Fe:vec-cpu.exe ws2_32.lib mpr.lib
*
* Build (Linux):
* g++ -O3 -march=native -ffast-math vec-cpu.cpp -o vec-cpu -lpthread
*/
/* ===================================================================== */
/* Platform includes */
/* ===================================================================== */
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <dirent.h>
#include <glob.h>
#include <fcntl.h>
#include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <float.h>
/* ===================================================================== */
/* Constants */
/* ===================================================================== */
#define DEFAULT_PORT 1920
#define DEFAULT_TOP_K 50
#define MAX_TOP_K 100
#define INITIAL_CAP 4096
#define MAX_LINE (1 << 24)
#define PIPE_BUF_SIZE (1 << 20)
#define FMT_F32 0
#define FMT_F16 1
/* VEC 2.0 binary frame protocol — see PROTOCOL-2.0.md */
#define BIN_MAGIC 0xF0
#define PROTOCOL_VERSION 0x02
#define CMD_PUSH 0x01
#define CMD_QUERY 0x02 /* unified PULL+CPULL */
/* 0x03 removed — was CPULL */
#define CMD_GET 0x04 /* unified GET+MGET */
/* 0x05 removed — was MGET */
#define CMD_UPDATE 0x06
#define CMD_DELETE 0x07
#define CMD_LABEL 0x08
#define CMD_UNDO 0x09
#define CMD_SAVE 0x0A
/* 0x0B, 0x0C reserved (do not reuse) */
#define CMD_CLUSTER 0x0D
#define CMD_DISTINCT 0x0E
#define CMD_REPRESENT 0x0F
#define CMD_INFO 0x10
#define CMD_QID 0x11 /* unified PID+CPID */
/* 0x12 removed — was CPID */
#define CMD_SET_DATA 0x13
#define CMD_GET_DATA 0x14
/* response envelope */
#define RESP_OK 0x00
#define RESP_ERR 0x01
/* shape mask bits */
#define SHAPE_VECTOR 0x01
#define SHAPE_LABEL 0x02
#define SHAPE_DATA 0x04
/* GET mode */
#define GET_MODE_SINGLE 0x00
#define GET_MODE_BATCH 0x01
/* metric */
#define METRIC_L2 0x00
#define METRIC_COSINE 0x01
#ifdef _WIN32
#define strtok_r strtok_s
#endif
/* ===================================================================== */
/* Elapsed-time helper */
/* ===================================================================== */
static long long now_ms() {
#ifdef _WIN32
LARGE_INTEGER c, f;
QueryPerformanceCounter(&c);
QueryPerformanceFrequency(&f);
return (long long)((c.QuadPart * 1000LL) / f.QuadPart);
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
#endif
}
static void format_elapsed(long long ms, char *out, int outsz) {
if (ms < 1000) snprintf(out, outsz, "%lldms", ms);
else if (ms < 60000) snprintf(out, outsz, "%.1fs", ms / 1000.0);
else if (ms < 3600000) {
long long m = ms / 60000, s = (ms % 60000) / 1000;
snprintf(out, outsz, "%lldm%llds", m, s);
} else {
long long h = ms / 3600000, m = (ms % 3600000) / 60000;
snprintf(out, outsz, "%lldh%lldm", h, m);
}
}
/* ===================================================================== */
/* CRC32 */
/* ===================================================================== */
static unsigned int crc32_tab[256];
static int crc32_ready = 0;
static void crc32_init() {
for (unsigned int i = 0; i < 256; i++) {
unsigned int c = i;
for (int j = 0; j < 8; j++) c = (c >> 1) ^ ((c & 1) ? 0xEDB88320 : 0);
crc32_tab[i] = c;
}
crc32_ready = 1;
}
static unsigned int crc32_update(unsigned int crc, const void *buf, size_t len) {
if (!crc32_ready) crc32_init();
const unsigned char *p = (const unsigned char *)buf;
crc = ~crc;
for (size_t i = 0; i < len; i++) crc = crc32_tab[(crc ^ p[i]) & 0xFF] ^ (crc >> 8);
return ~crc;
}
static const char CRC_C[] = "BCDFGHJKLMNPRSTVZ"; /* 17 */
static const char CRC_V[] = "AEIOU"; /* 5 */
#define CRC_SYL (17 * 5) /* 85 per syllable, 85^4 = 52M unique */
static void crc32_word(unsigned int crc, char *out) {
unsigned int v = crc;
for (int i = 0; i < 4; i++) {
int s = v % CRC_SYL;
v /= CRC_SYL;
*out++ = CRC_C[s / 5];
*out++ = CRC_V[s % 5];
}
*out = '\0';
}
/* ===================================================================== */
/* CPU distance functions */
/* ===================================================================== */
static void cpu_l2_f32(const float *db, const float *query, float *dists, int n, int dim) {
for (int i = 0; i < n; i++) {
const float *v = db + (size_t)i * dim;
float sum = 0.0f;
for (int d = 0; d < dim; d++) { float diff = v[d] - query[d]; sum += diff * diff; }
dists[i] = sum;
}
}
static void cpu_cos_f32(const float *db, const float *query, float *dists, int n, int dim) {
float nq = 0;
for (int d = 0; d < dim; d++) nq += query[d] * query[d];
for (int i = 0; i < n; i++) {
const float *v = db + (size_t)i * dim;
float dot = 0, nv = 0;
for (int d = 0; d < dim; d++) { dot += v[d]*query[d]; nv += v[d]*v[d]; }
float denom = sqrtf(nv) * sqrtf(nq);
dists[i] = (denom > 0) ? (1.0f - dot / denom) : 1.0f;
}
}
/* ===================================================================== */
/* Globals */
/* ===================================================================== */
static char g_name[256];
static char g_filepath[512];
static int g_dim = 0;
static int g_port = DEFAULT_PORT;
static int g_topk = DEFAULT_TOP_K;
static int g_fmt = FMT_F32;
static int g_elem_size = 4;
static const char *fmt_name(int fmt) { return fmt == FMT_F16 ? "f16" : "f32"; }
static int g_count = 0;
static int g_capacity = 0;
static void *d_vectors = NULL;
static float *h_dists = NULL;
static int *h_ids = NULL;
static unsigned char *g_alive = NULL;
static int g_alive_cap = 0;
static int g_deleted = 0;
/* labels: variable-length strings indexed by slot */
static char **g_labels = NULL; /* array of pointers, NULL = no label */
static int g_labels_cap = 0;
/* data blobs: variable-length opaque payloads indexed by slot, ≤ MAX_DATA_BYTES each */
#define MAX_DATA_BYTES 102400
#define MAX_LABEL_BYTES 2048
static unsigned char **g_blobs = NULL;
static unsigned int *g_blob_lens = NULL;
static int g_blobs_cap = 0;
static int g_dirty = 0;
static int g_readonly = 0;
static volatile int g_running = 1;
#ifdef _WIN32
static HANDLE g_mutex = NULL;
#else
static char g_sockpath[512];
static int g_lockfd = -1;
#endif
/* ===================================================================== */
/* Memory management */
/* ===================================================================== */
static void gpu_realloc_if_needed(int required) {
if (required <= g_capacity) return;
int new_cap = g_capacity;
while (new_cap < required) new_cap *= 2;
void *d_new = malloc((size_t)new_cap * g_dim * g_elem_size);
if (d_vectors && g_count > 0)
memcpy(d_new, d_vectors, (size_t)g_count * g_dim * g_elem_size);
free(d_vectors);
d_vectors = d_new;
free(h_dists);
free(h_ids);
h_dists = (float *)malloc(new_cap * sizeof(float));
h_ids = (int *)malloc(new_cap * sizeof(int));
unsigned char *new_alive = (unsigned char *)realloc(g_alive, new_cap);
memset(new_alive + g_alive_cap, 1, new_cap - g_alive_cap);
g_alive = new_alive;
g_alive_cap = new_cap;
char **new_labels = (char **)realloc(g_labels, new_cap * sizeof(char *));
memset(new_labels + g_labels_cap, 0, (new_cap - g_labels_cap) * sizeof(char *));
g_labels = new_labels;
g_labels_cap = new_cap;
unsigned char **new_blobs = (unsigned char **)realloc(g_blobs, new_cap * sizeof(unsigned char *));
memset(new_blobs + g_blobs_cap, 0, (new_cap - g_blobs_cap) * sizeof(unsigned char *));
g_blobs = new_blobs;
unsigned int *new_blob_lens = (unsigned int *)realloc(g_blob_lens, new_cap * sizeof(unsigned int));
memset(new_blob_lens + g_blobs_cap, 0, (new_cap - g_blobs_cap) * sizeof(unsigned int));
g_blob_lens = new_blob_lens;
g_blobs_cap = new_cap;
g_capacity = new_cap;
}
static void gpu_init() {
g_capacity = INITIAL_CAP;
d_vectors = malloc((size_t)g_capacity * g_dim * g_elem_size);
h_dists = (float *)malloc(g_capacity * sizeof(float));
h_ids = (int *)malloc(g_capacity * sizeof(int));
g_alive = (unsigned char *)malloc(g_capacity);
memset(g_alive, 1, g_capacity);
g_alive_cap = g_capacity;
g_labels = (char **)calloc(g_capacity, sizeof(char *));
g_labels_cap = g_capacity;
g_blobs = (unsigned char **)calloc(g_capacity, sizeof(unsigned char *));
g_blob_lens = (unsigned int *)calloc(g_capacity, sizeof(unsigned int));
g_blobs_cap = g_capacity;
}
static void gpu_shutdown() {
free(d_vectors);
free(h_dists);
free(h_ids);
free(g_alive);
if (g_labels) {
for (int i = 0; i < g_labels_cap; i++) free(g_labels[i]);
free(g_labels);
}
if (g_blobs) {
for (int i = 0; i < g_blobs_cap; i++) free(g_blobs[i]);
free(g_blobs);
}
free(g_blob_lens);
}
/* ===================================================================== */
/* Vector operations */
/* ===================================================================== */
static void upload_and_store(const float *h_data, void *d_dest, int nfloats) {
memcpy(d_dest, h_data, nfloats * sizeof(float));
}
/* validate label: 0 ok, -1 invalid char, -2 too long, -3 empty.
* rejected: control chars, space, and : * ? " < > | ,
* allowed: / \ . _ - = ! ' ^ + % & ( ) [ ] { } @ # $ ~ ` ; alphanumerics, etc. */
static int validate_label(const char *label, int len) {
if (len <= 0) return -3;
if (len > MAX_LABEL_BYTES) return -2;
for (int i = 0; i < len; i++) {
unsigned char c = (unsigned char)label[i];
if (c <= 0x1F || c == 0x7F) return -1;
if (c == ' ' || c == ':' || c == '*' || c == '?' || c == '"' ||
c == '<' || c == '>' || c == '|' || c == ',') return -1;
}
return 0;
}
/* lenient label setter — used by .meta load only (matches 1.x). */
static void vec_set_label_raw(int slot, const char *label, int len) {
if (slot < 0 || slot >= g_labels_cap) return;
free(g_labels[slot]);
if (!label || len <= 0) { g_labels[slot] = NULL; return; }
if (len >= 3 && (unsigned char)label[0] == 0xEF &&
(unsigned char)label[1] == 0xBB && (unsigned char)label[2] == 0xBF) {
label += 3; len -= 3;
}
if (len <= 0) { g_labels[slot] = NULL; return; }
char *buf = (char *)malloc(len + 1);
if (!buf) { g_labels[slot] = NULL; return; }
memcpy(buf, label, len);
buf[len] = '\0';
g_labels[slot] = buf;
}
/* strict label setter — used by all wire write paths. returns 0 ok, validate_label codes on error. */
static int vec_set_label(int slot, const char *label, int len) {
if (slot < 0 || slot >= g_labels_cap) return -1;
if (!label || len <= 0) {
free(g_labels[slot]);
g_labels[slot] = NULL;
return 0;
}
int rc = validate_label(label, len);
if (rc != 0) return rc;
char *buf = (char *)malloc(len + 1);
if (!buf) return -1;
memcpy(buf, label, len);
buf[len] = '\0';
free(g_labels[slot]);
g_labels[slot] = buf;
return 0;
}
/* opaque blob setter. len=0 clears. */
static int vec_set_blob(int slot, const unsigned char *bytes, unsigned int len) {
if (slot < 0 || slot >= g_blobs_cap) return -1;
if (len > MAX_DATA_BYTES) return -2;
if (!bytes || len == 0) {
free(g_blobs[slot]);
g_blobs[slot] = NULL;
g_blob_lens[slot] = 0;
return 0;
}
unsigned char *buf = (unsigned char *)malloc(len);
if (!buf) return -1;
memcpy(buf, bytes, len);
free(g_blobs[slot]);
g_blobs[slot] = buf;
g_blob_lens[slot] = len;
return 0;
}
static int vec_push(const float *h_vec) {
gpu_realloc_if_needed(g_count + 1);
int slot = g_count;
upload_and_store(h_vec, (char *)d_vectors + (size_t)slot * g_dim * g_elem_size, g_dim);
g_count++;
g_dirty = 1;
return slot;
}
static int vec_pull(const float *h_query, int *out_ids, float *out_dists, int mode) {
int alive = g_count - g_deleted;
if (alive <= 0) return 0;
int n = g_count;
int k = (alive < g_topk) ? alive : g_topk;
if (mode == 1) cpu_cos_f32((const float *)d_vectors, h_query, h_dists, n, g_dim);
else cpu_l2_f32((const float *)d_vectors, h_query, h_dists, n, g_dim);
for (int i = 0; i < n; i++) {
h_ids[i] = i;
if (!g_alive[i]) h_dists[i] = 3.402823e+38f;
}
for (int i = 0; i < k; i++) {
int best = i;
for (int j = i + 1; j < n; j++) {
if (h_dists[j] < h_dists[best]) best = j;
}
float td = h_dists[i]; h_dists[i] = h_dists[best]; h_dists[best] = td;
int ti = h_ids[i]; h_ids[i] = h_ids[best]; h_ids[best] = ti;
}
for (int i = 0; i < k; i++) {
out_ids[i] = h_ids[i];
out_dists[i] = h_dists[i];
}
return k;
}
/* ===================================================================== */
/* Clustering (DBSCAN) */
/* ===================================================================== */
typedef int (*write_fn)(void *ctx, const char *buf, int len);
#define CLUSTER_UNVISITED -1
#define CLUSTER_NOISE -2
static void vec_cluster(float eps, int min_pts, int mode, write_fn writer, void *wctx) {
int n = g_count;
int alive = n - g_deleted;
if (alive <= 0) {
writer(wctx, "end\n", 4);
return;
}
float eps_sq = (mode == 1) ? eps : eps * eps;
int *cluster_id = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
cluster_id[i] = g_alive[i] ? CLUSTER_UNVISITED : CLUSTER_NOISE;
int *queue = (int *)malloc(n * sizeof(int));
float *dists_buf = (float *)malloc(n * sizeof(float));
int cluster = 0;
for (int i = 0; i < n; i++) {
if (cluster_id[i] != CLUSTER_UNVISITED) continue;
const float *query = (const float *)d_vectors + (size_t)i * g_dim;
if (mode == 1) cpu_cos_f32((const float *)d_vectors, query, dists_buf, n, g_dim);
else cpu_l2_f32((const float *)d_vectors, query, dists_buf, n, g_dim);
int q_head = 0, q_tail = 0;
int neighbor_count = 0;
for (int j = 0; j < n; j++) {
if (!g_alive[j]) continue;
if (dists_buf[j] <= eps_sq) {
queue[q_tail++] = j;
neighbor_count++;
}
}
if (neighbor_count < min_pts) {
cluster_id[i] = CLUSTER_NOISE;
continue;
}
cluster_id[i] = cluster;
while (q_head < q_tail) {
int j = queue[q_head++];
if (cluster_id[j] == CLUSTER_NOISE) cluster_id[j] = cluster;
if (cluster_id[j] != CLUSTER_UNVISITED) continue;
cluster_id[j] = cluster;
const float *q2 = (const float *)d_vectors + (size_t)j * g_dim;
if (mode == 1) cpu_cos_f32((const float *)d_vectors, q2, dists_buf, n, g_dim);
else cpu_l2_f32((const float *)d_vectors, q2, dists_buf, n, g_dim);
int j_neighbors = 0;
for (int k = 0; k < n; k++) {
if (!g_alive[k]) continue;
if (dists_buf[k] <= eps_sq) j_neighbors++;
}
if (j_neighbors >= min_pts) {
for (int k = 0; k < n; k++) {
if (!g_alive[k]) continue;
if (dists_buf[k] <= eps_sq && (cluster_id[k] == CLUSTER_UNVISITED || cluster_id[k] == CLUSTER_NOISE)) {
if (cluster_id[k] == CLUSTER_NOISE) cluster_id[k] = cluster;
else queue[q_tail++] = k;
}
}
}
}
cluster++;
}
char line[256];
int line_len;
for (int c = 0; c < cluster; c++) {
int first = 1;
for (int i = 0; i < n; i++) {
if (cluster_id[i] != c) continue;
const char *lbl = (i < g_labels_cap) ? g_labels[i] : NULL;
if (lbl)
line_len = snprintf(line, sizeof(line), "%s%s", first ? "" : ",", lbl);
else
line_len = snprintf(line, sizeof(line), "%s%d", first ? "" : ",", i);
writer(wctx, line, line_len);
first = 0;
}
writer(wctx, "\n", 1);
}
{
int first = 1;
int has_noise = 0;
for (int i = 0; i < n; i++) {
if (cluster_id[i] != CLUSTER_NOISE || !g_alive[i]) continue;
has_noise = 1;
const char *lbl = (i < g_labels_cap) ? g_labels[i] : NULL;
if (lbl)
line_len = snprintf(line, sizeof(line), "%s%s", first ? "" : ",", lbl);
else
line_len = snprintf(line, sizeof(line), "%s%d", first ? "" : ",", i);
writer(wctx, line, line_len);
first = 0;
}
if (has_noise) writer(wctx, "\n", 1);
}
line_len = snprintf(line, sizeof(line), "end\n");
writer(wctx, line, line_len);
free(cluster_id);
free(queue);
free(dists_buf);
}
/* ===================================================================== */
/* Persistence */
/* ===================================================================== */
static unsigned int g_loaded_crc = 0;
static unsigned int g_computed_crc = 0;
static int g_crc_ok = 0;
static long long estimate_file_size() {
long long tensors = 13LL + g_count + (long long)g_count * g_dim * g_elem_size + 4;
long long meta = 4; /* count header */
for (int i = 0; i < g_count; i++)
meta += 4 + (g_labels[i] ? (long long)strlen(g_labels[i]) : 0);
return tensors + meta;
}
static void save_to_file() {
if (g_count == 0) return;
if (g_readonly) return;
if (!g_dirty && g_crc_ok == 1) return;
long long t_save_start = now_ms();
/* disk space check */
long long needed = estimate_file_size() + (1024 * 1024);
#ifdef _WIN32
ULARGE_INTEGER free_bytes;
if (GetDiskFreeSpaceExA(NULL, &free_bytes, NULL, NULL)) {
if ((long long)free_bytes.QuadPart < needed) {
fprintf(stderr, "ERROR: not enough disk space (need %lld bytes, have %llu)\n",
needed, (unsigned long long)free_bytes.QuadPart);
return;
}
}
#else
struct statvfs st;
if (statvfs(".", &st) == 0) {
long long avail = (long long)st.f_bavail * st.f_frsize;
if (avail < needed) {
fprintf(stderr, "ERROR: not enough disk space (need %lld bytes, have %lld)\n",
needed, avail);
return;
}
}
#endif
FILE *f = fopen(g_filepath, "wb");
if (!f) { fprintf(stderr, "ERROR: cannot open %s for writing\n", g_filepath); return; }
fwrite(&g_dim, sizeof(int), 1, f);
fwrite(&g_count, sizeof(int), 1, f);
fwrite(&g_deleted, sizeof(int), 1, f);
unsigned char fmt_byte = (unsigned char)g_fmt;
fwrite(&fmt_byte, 1, 1, f);
unsigned int crc = 0;
if (g_count > 0) {
fwrite(g_alive, 1, g_count, f);
crc = crc32_update(crc, g_alive, g_count);
size_t total_bytes = (size_t)g_count * g_dim * g_elem_size;
fwrite(d_vectors, 1, total_bytes, f);
crc = crc32_update(crc, d_vectors, total_bytes);
}
fwrite(&crc, sizeof(unsigned int), 1, f);
fclose(f);
char crc_name[16];
crc32_word(crc, crc_name);
g_dirty = 0;
g_crc_ok = 1;
g_loaded_crc = crc;
g_computed_crc = crc;
{
char el[32]; format_elapsed(now_ms() - t_save_start, el, sizeof(el));
printf("saved %d vectors to %s [%s 0x%08X] (%s)\n", g_count, g_filepath, crc_name, crc, el);
}
/* save labels to .meta sidecar */
int has_labels = 0;
for (int i = 0; i < g_count; i++) { if (g_labels[i]) { has_labels = 1; break; } }
if (has_labels) {
char metapath[512];
strncpy(metapath, g_filepath, sizeof(metapath) - 1);
char *ext = strstr(metapath, ".tensors");
if (ext) strcpy(ext, ".meta");
else snprintf(metapath, sizeof(metapath), "%s.meta", g_name);
FILE *mf = fopen(metapath, "wb");
if (mf) {
fwrite(&g_count, sizeof(int), 1, mf);
for (int i = 0; i < g_count; i++) {
int slen = g_labels[i] ? (int)strlen(g_labels[i]) : 0;
fwrite(&slen, sizeof(int), 1, mf);
if (slen > 0) fwrite(g_labels[i], 1, slen, mf);
}
fclose(mf);
}
}
/* save data blobs to .data sidecar */
int has_blobs = 0;
for (int i = 0; i < g_count; i++) { if (g_blobs[i] && g_blob_lens[i] > 0) { has_blobs = 1; break; } }
if (has_blobs) {
char datapath[512];
strncpy(datapath, g_filepath, sizeof(datapath) - 1);
char *ext = strstr(datapath, ".tensors");
if (ext) strcpy(ext, ".data");
else snprintf(datapath, sizeof(datapath), "%s.data", g_name);
FILE *df = fopen(datapath, "wb");
if (df) {
unsigned char *mask = (unsigned char *)malloc(g_count);
if (mask) {
for (int i = 0; i < g_count; i++) {
mask[i] = (g_blobs[i] && g_blob_lens[i] > 0) ? 1 : 0;
}
fwrite(&g_count, sizeof(int), 1, df);
fwrite(mask, 1, g_count, df);
unsigned int dcrc = crc32_update(0, mask, g_count);
for (int i = 0; i < g_count; i++) {
if (!mask[i]) continue;
unsigned int dlen = g_blob_lens[i];
fwrite(&dlen, sizeof(unsigned int), 1, df);
fwrite(g_blobs[i], 1, dlen, df);
dcrc = crc32_update(dcrc, (const unsigned char *)&dlen, sizeof(unsigned int));
dcrc = crc32_update(dcrc, g_blobs[i], dlen);
}
fwrite(&dcrc, sizeof(unsigned int), 1, df);
free(mask);
}
fclose(df);
}
}
}
static int peek_file_header() {
FILE *f = fopen(g_filepath, "rb");
if (!f) return 0;
int file_dim;
int dummy1, dummy2;
unsigned char file_fmt;
if (fread(&file_dim, sizeof(int), 1, f) != 1 ||
fread(&dummy1, sizeof(int), 1, f) != 1 ||
fread(&dummy2, sizeof(int), 1, f) != 1 ||
fread(&file_fmt, 1, 1, f) != 1) {
fclose(f);
return 0;
}
fclose(f);
if (g_dim > 0 && file_dim != g_dim)
fprintf(stderr, "WARN: filename suggests dim=%d but header has dim=%d, using header\n", g_dim, file_dim);
if (file_fmt != g_fmt)
fprintf(stderr, "WARN: filename suggests %s but header has %s, using header\n", fmt_name(g_fmt), fmt_name(file_fmt));
g_dim = file_dim;
g_fmt = file_fmt;
g_elem_size = (g_fmt == FMT_F16) ? 2 : 4;
return 1;
}
/* g_loaded_crc, g_computed_crc, g_crc_ok moved before save_to_file */
static int load_from_file() {
FILE *f = fopen(g_filepath, "rb");
if (!f) return 0;
int file_dim, file_count, file_deleted;
unsigned char file_fmt;
if (fread(&file_dim, sizeof(int), 1, f) != 1 ||
fread(&file_count, sizeof(int), 1, f) != 1 ||
fread(&file_deleted, sizeof(int), 1, f) != 1 ||
fread(&file_fmt, 1, 1, f) != 1) {
fclose(f);
fprintf(stderr, "WARN: corrupt %s, starting fresh\n", g_filepath);
return 0;
}
unsigned int crc = 0;
if (file_count > 0) {
gpu_realloc_if_needed(file_count);
size_t mask_rd = fread(g_alive, 1, file_count, f);
if ((int)mask_rd != file_count) {
fprintf(stderr, "WARN: alive mask truncated\n");
file_count = (int)mask_rd;
}
crc = crc32_update(crc, g_alive, mask_rd);
size_t total_bytes = (size_t)file_count * g_dim * g_elem_size;
void *h_buf = malloc(total_bytes);
size_t rd = fread(h_buf, 1, total_bytes, f);
if (rd != total_bytes) {
fprintf(stderr, "WARN: data truncated\n");
file_count = (int)(rd / (g_dim * g_elem_size));
}
crc = crc32_update(crc, h_buf, rd);
memcpy(d_vectors, h_buf, (size_t)file_count * g_dim * g_elem_size);
free(h_buf);
g_count = file_count;
g_deleted = file_deleted;
}
unsigned int stored_crc = 0;
if (fread(&stored_crc, sizeof(unsigned int), 1, f) == 1) {
g_loaded_crc = stored_crc;
g_computed_crc = crc;
g_crc_ok = (stored_crc == crc);
} else {
g_loaded_crc = 0;
g_computed_crc = crc;
g_crc_ok = -1; /* no trailer - old format file */
}
fclose(f);
/* load labels from .meta sidecar */
char metapath[512];
strncpy(metapath, g_filepath, sizeof(metapath) - 1);
char *mext = strstr(metapath, ".tensors");
if (mext) strcpy(mext, ".meta");
else snprintf(metapath, sizeof(metapath), "%s.meta", g_name);
FILE *mf = fopen(metapath, "rb");
if (mf) {
int meta_count = 0;
if (fread(&meta_count, sizeof(int), 1, mf) == 1) {
int lim = (meta_count < g_count) ? meta_count : g_count;
for (int i = 0; i < lim; i++) {
int slen = 0;
if (fread(&slen, sizeof(int), 1, mf) != 1) break;
if (slen > 0 && slen < 65536) {
g_labels[i] = (char *)malloc(slen + 1);
if (fread(g_labels[i], 1, slen, mf) != (size_t)slen) { free(g_labels[i]); g_labels[i] = NULL; break; }
g_labels[i][slen] = '\0';
} else if (slen == 0) {
/* skip - no label */
} else {
break; /* corrupt */
}
}
}
fclose(mf);
}
/* load data blobs from .data sidecar */
char datapath[512];
strncpy(datapath, g_filepath, sizeof(datapath) - 1);
char *dext = strstr(datapath, ".tensors");
if (dext) strcpy(dext, ".data");
else snprintf(datapath, sizeof(datapath), "%s.data", g_name);
FILE *df = fopen(datapath, "rb");
if (df) {
int data_count = 0;
if (fread(&data_count, sizeof(int), 1, df) == 1 && data_count > 0 && data_count <= g_count) {
unsigned char *mask = (unsigned char *)malloc(data_count);
if (mask && fread(mask, 1, data_count, df) == (size_t)data_count) {
for (int i = 0; i < data_count; i++) {
if (!mask[i]) continue;
unsigned int dlen = 0;
if (fread(&dlen, sizeof(unsigned int), 1, df) != 1) break;
if (dlen == 0 || dlen > MAX_DATA_BYTES) break;
unsigned char *buf = (unsigned char *)malloc(dlen);
if (!buf) break;
if (fread(buf, 1, dlen, df) != (size_t)dlen) { free(buf); break; }
g_blobs[i] = buf;
g_blob_lens[i] = dlen;
}
}
free(mask);
}
fclose(df);
}
return 1;
}
/* ===================================================================== */
/* Protocol helpers */
/* ===================================================================== */
/* format results: label:dist or index:dist per result, comma-separated */
static void format_results(int *ids, float *dists, int k, write_fn writer, void *wctx) {
char resp[65536];
char *p = resp;
int rem = sizeof(resp) - 2;
for (int i = 0; i < k; i++) {
int w;
const char *lbl = (ids[i] < g_labels_cap) ? g_labels[ids[i]] : NULL;
if (lbl)
w = snprintf(p, rem, "%s%s:%.6f", i > 0 ? "," : "", lbl, dists[i]);
else
w = snprintf(p, rem, "%s%d:%.6f", i > 0 ? "," : "", ids[i], dists[i]);
p += w; rem -= w;
}
*p++ = '\n';
writer(wctx, resp, (int)(p - resp));
}
/* ===================================================================== */
/* Binary frame protocol */
/* ===================================================================== */
/* VEC 2.0: body_len is explicit (4B u32 after label) — no per-cmd inference. */
static int frame_data_len(unsigned char cmd, const char *data_start, int available, int label_len) {
(void)label_len;
switch (cmd) {
case CMD_PUSH:
case CMD_QUERY:
case CMD_GET:
case CMD_UPDATE:
case CMD_DELETE:
case CMD_LABEL:
case CMD_UNDO:
case CMD_SAVE:
case CMD_CLUSTER:
case CMD_DISTINCT:
case CMD_REPRESENT:
case CMD_INFO:
case CMD_QID:
case CMD_SET_DATA:
case CMD_GET_DATA:
if (available < 4) return -1;
unsigned int blen;
memcpy(&blen, data_start, 4);
return (int)blen;
default:
return -2;
}
}
/* write one vector from memory to client as raw fp32 — zero-copy from d_vectors */
static int bin_write_vec(int idx, write_fn writer, void *wctx) {
writer(wctx, (char *)d_vectors + (size_t)idx * g_dim * g_elem_size, g_dim * (int)sizeof(float));
return 0;
}
/* ===================================================================== */
/* 2.0 response envelope helpers */
/* ===================================================================== */
static void resp_ok_header(write_fn writer, void *wctx, unsigned int body_len) {
char hdr[5];
hdr[0] = (char)RESP_OK;
memcpy(hdr + 1, &body_len, 4);
writer(wctx, hdr, 5);
}
static void resp_ok_empty(write_fn writer, void *wctx) {
char hdr[5] = { (char)RESP_OK, 0, 0, 0, 0 };
writer(wctx, hdr, 5);
}
static void resp_err(write_fn writer, void *wctx, const char *msg) {
unsigned int el = (unsigned int)strlen(msg);
char hdr[5];
hdr[0] = (char)RESP_ERR;
memcpy(hdr + 1, &el, 4);
writer(wctx, hdr, 5);
if (el > 0) writer(wctx, msg, (int)el);
}
/* scratch writer — collects writer_fn output into a growable buffer. */
typedef struct {
char *buf;
unsigned int len;
unsigned int cap;
int oom;
} scratch_writer_ctx;
static void scratch_writer_init(scratch_writer_ctx *s) { s->buf = NULL; s->len = 0; s->cap = 0; s->oom = 0; }
static void scratch_writer_free(scratch_writer_ctx *s) { free(s->buf); s->buf = NULL; s->len = 0; s->cap = 0; }
static int scratch_writer_fn(void *ctx, const char *data, int n) {
scratch_writer_ctx *s = (scratch_writer_ctx *)ctx;
if (s->oom || n <= 0) return n;
unsigned int need = s->len + (unsigned int)n;
if (need > s->cap) {
unsigned int newcap = s->cap ? s->cap * 2 : 4096;
while (newcap < need) newcap *= 2;
char *nb = (char *)realloc(s->buf, newcap);
if (!nb) { s->oom = 1; return n; }
s->buf = nb; s->cap = newcap;
}
memcpy(s->buf + s->len, data, n);
s->len += (unsigned int)n;
return n;
}
/* compute body length for a record under a shape mask */
static unsigned int record_body_len(int idx, unsigned char shape, int with_distance) {
unsigned int n = 4;
if (with_distance) n += 4;
if (shape & SHAPE_LABEL) {
unsigned int ll = (idx >= 0 && idx < g_labels_cap && g_labels[idx]) ? (unsigned int)strlen(g_labels[idx]) : 0;
n += 4 + ll;
}
if (shape & SHAPE_DATA) {
unsigned int dl = (idx >= 0 && idx < g_blobs_cap && g_blobs[idx]) ? g_blob_lens[idx] : 0;
n += 4 + dl;
}
if (shape & SHAPE_VECTOR) n += g_dim * 4;
return n;
}
static char *build_result_body(const int *ids, const float *dists, int count, unsigned char shape,
int with_distance, unsigned int *out_len) {