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
|
|
@ -115,7 +115,7 @@ abs(anchor_x - x) + abs(anchor_z - z) <= radius
|
|||
- 遍历所有有效低 48 位种子的**普通候选分支**。沿用低位筛选、16 候选组 mask、区间求交、精确 Java 验证,**不包含拒绝重抽补扫**。
|
||||
- 返回的种子是 `[0,2^48)` 内的有效代表;不重复枚举高 16 位等价的 64 位世界种子。
|
||||
|
||||
这是范围基准 CLI,不提供全世界长跑的断点续跑。Ctrl+C 可终止,未完成扫描不会输出成功报告。
|
||||
这是范围基准 CLI;需要长跑和断点续跑时使用 [search](../search/README.md)。Ctrl+C 可终止,未完成扫描不会输出成功报告。
|
||||
|
||||
## 参数及输出
|
||||
|
||||
|
|
@ -168,3 +168,5 @@ make -C bench test-gpu # 需要 GPU:实际搜索与文件输出验证
|
|||
本机实测:RTX 3060 Laptop,中心 `(0,0)`,半径 512,5×5,`--position-batch 128 --repeat 3`。525,313 个锚点,三轮均有 368,935 个低位根任务、0 个命中,扫描墙钟中位数约 **3.990 秒**。原始 JSON 在 [results/local-r512-n5.json](results/local-r512-n5.json)。另有半径 64 的快速环境验证记录;其批大小为默认 32,与上述正式样例不同。
|
||||
|
||||
H100 的 `sm_90` 原生代码和 PTX 已在本机交叉编译通过,尚未在 H100 或八张物理 GPU 上执行。
|
||||
|
||||
`--start-anchor N --anchor-count M` 可只扫描按曼哈顿行顺序编号的连续锚点区间,供 `search` 分片调用。编号从 0 开始;省略 count 时扫描剩余范围。JSON 的 `anchors` 仍是整个菱形大小,`start_anchor`、`anchor_count` 表示本次扫描区间;吞吐与覆盖检查按实际扫描数量计算。
|
||||
|
|
|
|||
|
|
@ -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\":[";
|
||||
|
|
|
|||
|
|
@ -26,6 +26,16 @@ def main():
|
|||
assert r['anchors']==1+2*radius*(radius+1)
|
||||
assert r['first_anchor']==[-123,456-radius] and r['last_anchor']==[-123,456+radius]
|
||||
for bad in [[],['--radius','-1'],['--radius','1','--size','0'],['--radius','1','--size','7'],['--radius','abc'],['--radius','0','--x','2147483647'],['--radius','1','--repeat','0'],['--radius','1','--position-batch','0']]:run(*bad,'--dry-run',ok=False)
|
||||
points=[(dx,dz) for dz in range(-3,4) for dx in range(-3,4) if abs(dx)+abs(dz)<=3]
|
||||
for start in range(len(points)):
|
||||
count=min(4,len(points)-start)
|
||||
r=run('--radius',3,'--start-anchor',start,'--anchor-count',count,'--dry-run')
|
||||
assert r['start_anchor']==start and r['anchor_count']==count and r['anchors']==25
|
||||
assert r['first_anchor']==list(points[start]) and r['last_anchor']==list(points[start+count-1])
|
||||
assert run('--radius',3,'--start-anchor',24,'--dry-run')['anchor_count']==1
|
||||
for opts in [('--start-anchor',25),('--anchor-count',0),('--anchor-count',26),
|
||||
('--start-anchor',24,'--anchor-count',2),('--start-anchor',18446744073709551615)]:
|
||||
run('--radius',3,*opts,'--dry-run',ok=False)
|
||||
pattern='?##./#..?/..#?'
|
||||
r=run('--radius',0,'--pattern',pattern,'--dry-run')
|
||||
assert r['pattern']==pattern.split('/') and r['square_size'] is None
|
||||
|
|
@ -122,6 +132,14 @@ def main():
|
|||
split.extend(map(tuple,r['rounds'][0]['sample']))
|
||||
assert sorted(split)==sorted(map(tuple,diamond['rounds'][0]['sample']))
|
||||
assert diamond['anchors']==13
|
||||
partition=[]
|
||||
for start,count in [(0,4),(4,5),(9,4)]:
|
||||
r=run(*common,'--radius',2,'--start-anchor',start,'--anchor-count',count,
|
||||
'--max-results',1000,'--position-batch',3)
|
||||
assert sum(g['anchors'] for g in r['rounds'][0]['gpus'])==count
|
||||
partition.extend(map(tuple,r['rounds'][0]['sample']))
|
||||
assert sorted(partition)==sorted(split)
|
||||
|
||||
run('--radius',0,'--devices','0,0',ok=False)
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out=Path(tmp)/'report.json'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue