File size: 3,818 Bytes
81dfd31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cuda_runtime.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <vector>
using B=uint8_t;
#define C(x) do{auto e=(x);if(e){fprintf(stderr,"%s at %d\n",cudaGetErrorString(e),__LINE__);exit(2);}}while(0)
struct Meta{int h,w,a,b,k,reachable;};
struct Case {Meta m;std::vector<B> g,expect;};
#define bit(x,n) (((x)>>(n))&1)
__global__ void step(const Meta* meta,const B* grid,const B* src,B* dst,int* alive,int stride) {
 int i=blockIdx.x*blockDim.x+threadIdx.x, lane=blockIdx.y;
 const Meta M=meta[lane]; if(i>=M.h*M.w)return;
 int j=i+lane*stride,y=i/M.w,x=i%M.w;
 int a0=y?src[j-M.w]:0, a1=x+1<M.w?src[j+1]:0,
     a2=y+1<M.h?src[j+M.w]:0,a3=x?src[j-1]:0, z=src[j];
 int open=grid[j],start=i==M.a;
 int seen=bit(z,0),tip=bit(z,5),path=bit(z,6);
 int p0=bit(z,1),p1=bit(z,2),p2=bit(z,3),p3=bit(z,4);
 int s0=bit(a0,0),s1=bit(a1,0),s2=bit(a2,0),s3=bit(a3,0);
 int t0=bit(a0,5),t1=bit(a1,5),t2=bit(a2,5),t3=bit(a3,5);
 int pc0=bit(a0,3),pc1=bit(a1,4),pc2=bit(a2,1),pc3=bit(a3,2);
 #include "gates.h"
 dst[j]=next_seen|(next_parent0<<1)|(next_parent1<<2)|(next_parent2<<3)|(next_parent3<<4)|(next_tip<<5)|(next_path<<6);
 if(next_tip || (next_seen!=seen))atomicOr(alive,1<<lane);
}
int main(){
 cudaDeviceProp dp{};C(cudaGetDeviceProperties(&dp,0));C(cudaFree(nullptr)); puts(dp.name);
 std::ifstream f("cases.bin",std::ios::binary);int count;f.read((char*)&count,4);if(!f||count!=61)return 3;
 std::vector<Case> cases(count);int stride=0;
 for(auto &z:cases){f.read((char*)&z.m,sizeof(Meta));int n=z.m.h*z.m.w;if(!f||n<1||n>11000)return 3;stride=std::max(stride,n);z.g.resize(n);z.expect.resize(n);f.read((char*)z.g.data(),n);f.read((char*)z.expect.data(),n);}
 if(!f)return 3;
 B *a,*b,*grid;Meta* meta;int* alive;size_t bytes=2*stride;
 C(cudaMalloc(&a,bytes));C(cudaMalloc(&b,bytes));C(cudaMalloc(&grid,bytes));C(cudaMalloc(&meta,2*sizeof(Meta)));C(cudaMalloc(&alive,sizeof(int)));
 for(int lanes: {1,2}){
  std::vector<double> samples;int checks=0;int launched=0;
  for(int trial=0;trial<6;trial++){
   double sec=0;checks=0;launched=0;
   for(int k=0;k<count;k+=lanes){
    int m=std::min(lanes,count-k);
    std::vector<B> init(m*stride,0),g(m*stride,0),out(m*stride,0);Meta d[2];
    for(int l=0;l<m;l++){auto& z=cases[k+l];d[l]=z.m;int off=l*stride;std::copy(z.g.begin(),z.g.end(),g.begin()+off);
     if(z.g[z.m.a]&&z.g[z.m.b])init[off+z.m.b]=1|(z.m.a==z.m.b?64:0);
     else {std::fill(g.begin()+off,g.begin()+off+stride,0);}}
    auto begin=std::chrono::steady_clock::now();
    C(cudaMemcpy(grid,g.data(),m*stride,cudaMemcpyHostToDevice));
    C(cudaMemcpy(a,init.data(),m*stride,cudaMemcpyHostToDevice));
    C(cudaMemcpy(meta,d,m*sizeof(Meta),cudaMemcpyHostToDevice));
    int rounds=0, act=1;
    for(;act&&rounds<2*stride+16;){
     for(int j=0;j<16;j++){
      C(cudaMemsetAsync(alive,0,sizeof(int)));
      step<<<dim3((stride+127)/128,m),128>>>(meta,grid,a,b,alive,stride);
      std::swap(a,b);rounds++;
     }
     C(cudaGetLastError());
     C(cudaMemcpy(&act,alive,sizeof(int),cudaMemcpyDeviceToHost));
    }
    if(act)return 5;
    C(cudaMemcpy(out.data(),a,m*stride,cudaMemcpyDeviceToHost));
    sec+=std::chrono::duration<double>(std::chrono::steady_clock::now()-begin).count();launched+=rounds;
    for(int l=0;l<m;l++){
     auto& z=cases[k+l]; for(size_t j=0;j<z.expect.size();j++) if(bit(out[l*stride+j],6)!=z.expect[j]){fprintf(stderr,"path mismatch case %d cell %zu\n",k+l,j);return 4;}
     checks++;
    }
   }
   if(trial) samples.push_back(sec);
  }
  std::sort(samples.begin(),samples.end());
  printf("LANES=%d CHECKS=%d MEDIAN_SEC=%.6f MIN=%.6f MAX=%.6f LAUNCH_ROUNDS=%d STATE_BYTES_PER_CELL=1 LIVE_DEVICE_BYTES=%zu\n",lanes,checks,samples[2],samples[0],samples[4],launched,3*bytes+2*sizeof(Meta)+sizeof(int));
 }
}