Add crash-resumable search with transactional checkpoints
This commit is contained in:
parent
f71985c0c3
commit
482989c4d2
8 changed files with 388 additions and 9 deletions
|
|
@ -43,7 +43,7 @@ Pattern parsePattern(std::string text){
|
|||
if(!p.positive)throw std::runtime_error("pattern requires at least one # cell");
|
||||
return p;
|
||||
}
|
||||
struct Config{Pattern pattern;int32_t x=0,z=0;uint64_t radius=0;int size=5,positionBatch=32,jobBatch=2048,repeats=1;uint64_t maxResults=0;size_t cacheClasses=4096;std::string devices="0",output;bool dry=false,list=false;};
|
||||
struct Config{Pattern pattern;int32_t x=0,z=0;uint64_t radius=0;int size=5,positionBatch=32,jobBatch=2048,repeats=1;uint64_t startAnchor=0,anchorCount=0;bool countSet=false;uint64_t maxResults=0;size_t cacheClasses=4096;std::string devices="0",output;bool dry=false,list=false;};
|
||||
uint64_t number(const std::string& s){size_t used=0;if(s.empty()||s[0]=='-')throw std::runtime_error("expected nonnegative integer: "+s);auto n=std::stoull(s,&used);if(used!=s.size())throw std::runtime_error("invalid integer: "+s);return n;}
|
||||
int32_t signed32(const std::string&s){size_t used=0;auto n=std::stoll(s,&used);if(used!=s.size()||n<INT32_MIN||n>INT32_MAX)throw std::runtime_error("invalid int32 coordinate");return int32_t(n);}
|
||||
uint64_t anchors(uint64_t r){return 1+2*r*(r+1);}
|
||||
|
|
@ -109,7 +109,7 @@ void worker(const Config& c,int device,Shared& shared,Metrics& result){try{
|
|||
if(shared.cancel)return;
|
||||
}
|
||||
result.start=Clock::now();
|
||||
const uint64_t end=anchors(c.radius);
|
||||
const uint64_t end=c.startAnchor+c.anchorCount;
|
||||
while(!shared.cancel){uint64_t first=shared.next.fetch_add(c.positionBatch);if(first>=end)break;
|
||||
uint32_t num=std::min<uint64_t>(c.positionBatch,end-first);std::vector<Coord> coords;std::vector<uint64_t> keys;
|
||||
std::map<uint64_t,std::vector<uint32_t>> lists;std::vector<Coord> missing;std::vector<uint64_t> missingKeys;
|
||||
|
|
@ -163,6 +163,8 @@ Ordinary candidate branch only; rejection-tail supplement is NOT included.
|
|||
--pattern-file FILE Same pattern, one row per line (dimensions 1..64)
|
||||
Requires at least one #; exclusive with --size
|
||||
--devices 0,1,... CUDA device indices, or all (default 0)
|
||||
--start-anchor N Start at this zero-based anchor index (default 0)
|
||||
--anchor-count N Scan only N anchors from start (default remaining)
|
||||
--repeat N Complete passes; fresh root cache each pass (default 1)
|
||||
--position-batch N Anchors per scheduled batch, 1..256 (default 32)
|
||||
--job-batch N Low18-root jobs per GPU launch, 1..65536 (default 2048)
|
||||
|
|
@ -186,6 +188,7 @@ int main(int argc,char**argv){try{
|
|||
else if(a=="--size"){c.size=bounded(1,6);sizeSet=true;}
|
||||
else if(a=="--pattern"||a=="--pattern-file"){if(patternSet)throw std::runtime_error("specify only one pattern");patternSet=true;
|
||||
if(a=="--pattern")patternText=v;else{std::ifstream file(v);if(!file)throw std::runtime_error("cannot read pattern: "+v);std::ostringstream text;text<<file.rdbuf();patternText=text.str();}}else if(a=="--devices")c.devices=v;else if(a=="--repeat")c.repeats=bounded(1,1000);
|
||||
else if(a=="--start-anchor")c.startAnchor=number(v);else if(a=="--anchor-count"){c.anchorCount=number(v);c.countSet=true;}
|
||||
else if(a=="--position-batch")c.positionBatch=bounded(1,256);else if(a=="--job-batch")c.jobBatch=bounded(1,65536);
|
||||
else if(a=="--cache-classes")c.cacheClasses=bounded(0,1000000);else if(a=="--max-results")c.maxResults=bounded(0,1000000);
|
||||
else if(a=="--output")c.output=v;else throw std::runtime_error("unknown option: "+a);
|
||||
|
|
@ -197,11 +200,15 @@ int main(int argc,char**argv){try{
|
|||
c.pattern=parsePattern(patternText);
|
||||
for(auto axis:{std::make_pair(c.x,c.pattern.width),std::make_pair(c.z,c.pattern.height)})
|
||||
if(int64_t(axis.first)-int64_t(c.radius)<INT32_MIN||int64_t(axis.first)+int64_t(c.radius)+axis.second-1>INT32_MAX)throw std::runtime_error("coordinates including pattern must fit int32");
|
||||
if(c.startAnchor>=anchors(c.radius))throw std::runtime_error("start anchor outside range");
|
||||
if(!c.countSet)c.anchorCount=anchors(c.radius)-c.startAnchor;
|
||||
if(!c.anchorCount||c.anchorCount>anchors(c.radius)-c.startAnchor)throw std::runtime_error("anchor count outside range");
|
||||
if(!c.output.empty()&&std::ifstream(c.output).good())throw std::runtime_error("output already exists: "+c.output);
|
||||
std::ostringstream report;report<<std::setprecision(12)<<"{\"schema_version\":1,\"mode\":\"ordinary_all_effective_seeds\",\"rejection_tail_included\":false,\"center\":["<<c.x<<","<<c.z<<"],\"radius_chunks\":"<<c.radius<<",\"square_size\":"<<(patternSet?"null":std::to_string(c.size))<<",\"anchors\":"<<anchors(c.radius)<<",\"effective_seed_bits\":48,\"dry_run\":"<<(c.dry?"true":"false");
|
||||
report<<",\"start_anchor\":"<<c.startAnchor<<",\"anchor_count\":"<<c.anchorCount;
|
||||
report<<",\"pattern_width\":"<<c.pattern.width<<",\"pattern_height\":"<<c.pattern.height<<",\"slime_cells\":"<<c.pattern.positive<<",\"non_slime_cells\":"<<c.pattern.cells.size()-c.pattern.positive<<",\"pattern\":[";
|
||||
for(size_t i=0;i<c.pattern.rows.size();++i){if(i)report<<",";report<<"\""<<c.pattern.rows[i]<<"\"";}report<<"]";
|
||||
if(c.dry){auto first=coordinate(c,0),last=coordinate(c,anchors(c.radius)-1);report<<",\"first_anchor\":["<<first.x<<","<<first.z<<"],\"last_anchor\":["<<last.x<<","<<last.z<<"]}";}
|
||||
if(c.dry){auto first=coordinate(c,c.startAnchor),last=coordinate(c,c.startAnchor+c.anchorCount-1);report<<",\"first_anchor\":["<<first.x<<","<<first.z<<"],\"last_anchor\":["<<last.x<<","<<last.z<<"]}";}
|
||||
else{
|
||||
int available;CUDA(cudaGetDeviceCount(&available));std::vector<int> devices;if(c.devices=="all"){for(int i=0;i<available;++i)devices.push_back(i);}else{
|
||||
std::stringstream ss(c.devices);std::string v;while(std::getline(ss,v,',')){auto id=number(v);if(id>=uint64_t(available))throw std::runtime_error("CUDA device unavailable: "+v);devices.push_back(id);}
|
||||
|
|
@ -209,18 +216,18 @@ int main(int argc,char**argv){try{
|
|||
if(devices.empty()||std::set<int>(devices.begin(),devices.end()).size()!=devices.size())throw std::runtime_error("device list must be nonempty and unique");
|
||||
report<<",\"position_batch\":"<<c.positionBatch<<",\"job_batch\":"<<c.jobBatch<<",\"cache_classes\":"<<c.cacheClasses<<",\"cuda_runtime_version\":"<<CUDART_VERSION<<",\"rounds\":[";
|
||||
std::vector<double> timings;uint64_t expected=0;
|
||||
for(int round=0;round<c.repeats;++round){Shared shared;shared.workerCount=devices.size();std::vector<Metrics> metrics(devices.size());std::vector<std::thread> workers;auto start=Clock::now();
|
||||
for(int round=0;round<c.repeats;++round){Shared shared;shared.next=c.startAnchor;shared.workerCount=devices.size();std::vector<Metrics> metrics(devices.size());std::vector<std::thread> workers;auto start=Clock::now();
|
||||
for(size_t i=0;i<devices.size();++i)workers.emplace_back([&,i](){worker(c,devices[i],shared,metrics[i]);++shared.finished;shared.cv.notify_all();});
|
||||
{std::unique_lock<std::mutex> lock(shared.mutex);while(shared.finished<int(devices.size())){
|
||||
if(!shared.cv.wait_for(lock,std::chrono::seconds(5),[&](){return shared.finished==int(devices.size());}))
|
||||
std::cerr<<"progress: "<<shared.done<<"/"<<anchors(c.radius)<<" anchors (round "<<round+1<<")\n";
|
||||
std::cerr<<"progress: "<<shared.done<<"/"<<c.anchorCount<<" anchors (round "<<round+1<<")\n";
|
||||
}}
|
||||
for(auto&t:workers)t.join();
|
||||
if(!shared.error.empty())throw std::runtime_error(shared.error);
|
||||
double elapsed=std::chrono::duration<double,std::milli>(Clock::now()-start).count();auto first=metrics[0].start,last=metrics[0].end;
|
||||
uint64_t visited=0,jobs=0,matches=0;std::vector<Match> sample;
|
||||
for(auto&m:metrics){first=std::min(first,m.start);last=std::max(last,m.end);visited+=m.positions;jobs+=m.jobs;if(m.matches>UINT64_MAX-matches)throw std::runtime_error("total match count exceeds uint64");matches+=m.matches;for(auto hit:m.sample)if(sample.size()<c.maxResults)sample.push_back(hit);}
|
||||
if(visited!=anchors(c.radius))throw std::runtime_error("incomplete anchor coverage");if(round&&matches!=expected)throw std::runtime_error("repeat match count mismatch");expected=matches;
|
||||
if(visited!=c.anchorCount)throw std::runtime_error("incomplete anchor coverage");if(round&&matches!=expected)throw std::runtime_error("repeat match count mismatch");expected=matches;
|
||||
double scan=std::chrono::duration<double,std::milli>(last-first).count();timings.push_back(scan);
|
||||
if(round)report<<",";report<<"{\"round\":"<<round+1<<",\"elapsed_ms\":"<<elapsed<<",\"scan_wall_ms\":"<<scan<<",\"anchors_per_second\":"<<visited/(scan/1000)<<",\"root_jobs\":"<<jobs<<",\"matches\":"<<matches<<",\"sample_truncated\":"<<(matches>sample.size()?"true":"false")<<",\"sample\":[";
|
||||
for(size_t i=0;i<sample.size();++i){if(i)report<<",";auto m=sample[i];report<<"["<<m.seed<<","<<m.x<<","<<m.z<<"]";}report<<"],\"gpus\":[";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue