Skip to content

Commit

Permalink
Split the prefetch from the decision, so we can pipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
rokicki committed Aug 12, 2023
1 parent a6471bf commit 11c2d73
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 70 deletions.
33 changes: 21 additions & 12 deletions src/cpp/prunetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,27 @@ struct prunetable {
prunetable& operator=(prunetable &&) noexcept = delete ;
void filltable(const puzdef &pd, int d) ;
void checkextend(const puzdef &pd, int ignorelookups=0) ;
int lookuph(ull h) const {
int lookuph(ull h) const { // deprecate this
h = indexhash(h) ;
int v = 3 & (mem[h >> 5] >> ((h & 31) * 2)) ;
if (v == 3)
return (mem[(h >> 5) & ~7] & 15) - 1 ;
else
return 2 - v + baseval ;
}
void prefetch(ull h) const {
__builtin_prefetch(mem+((indexhash(h)) >> 5)) ;
int lookuphindexed(ull h) const {
int v = 3 & (mem[h >> 5] >> ((h & 31) * 2)) ;
if (v == 3)
return (mem[(h >> 5) & ~7] & 15) - 1 ;
else
return 2 - v + baseval ;
}
void prefetch(ull h) const { // deprecate this
__builtin_prefetch(mem+(indexhash(h) >> 5)) ;
}
ull prefetchindexed(ull h) const {
__builtin_prefetch(mem+(h >> 5)) ;
return h ;
}
ull indexhash(ull lowb) const {
ull h = lowb ;
Expand All @@ -108,19 +119,17 @@ struct prunetable {
ull indexhash(int n, const setval sv) const {
return indexhash(fasthash(n, sv)) ;
}
int lookup(const setval sv, setval *looktmp) const {
ull h ;
ull gethashforlookup(const setval sv, setval *looktmp) const {
if ((int)pdp->rotgroup.size() > 1) {
slowmodm2(*pdp, sv, *looktmp) ;
h = indexhash(totsize, *looktmp) ;
return indexhash(totsize, *looktmp) ;
} else {
h = indexhash(totsize, sv) ;
return indexhash(totsize, sv) ;
}
int v = 3 & (mem[h >> 5] >> ((h & 31) * 2)) ;
if (v == 3)
return (mem[(h >> 5) & ~7] & 15) - 1 ;
else
return 2 - v + baseval ;
}
int lookup(const setval sv, setval *looktmp) const {
ull h = gethashforlookup(sv, looktmp) ;
return lookuphindexed(h) ;
}
void addlookups(ull lookups) {
lookupcnt += lookups ;
Expand Down
127 changes: 69 additions & 58 deletions src/cpp/solve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,68 +67,79 @@ int solveworker::possibsolution(const puzdef &pd, int sp) {
return 0 ;
}
int solveworker::solveiter(const puzdef &pd, prunetable &pt, int togo, int sp, int st) {
int v, mi, m ;
ull mask, skipbase ;
ull h = innersetup(pt, sp) ;
while (1) {
lookups++ ;
v = pt.lookup(posns[sp], looktmp) ;
if (v > togo + 1) {
v = -1 ;
} else if (v > togo) {
v = 0 ;
} else if (v == 0 && togo == 1 && didprepass && pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
} else if (v == 0 && togo > 0 && noearlysolutions && pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
} else if (togo == 0) {
v = possibsolution(pd, sp) ;
} else {
mask = canonmask[st] ;
skipbase = 0 ;
mi = -1 ;
goto downstack ;
}
upstack:
if (solvestates.size() == 0)
int v = innerfetch(pd, pt, togo, sp, st, h) ;
if (v != 3)
return v ;
{
auto &ss = solvestates[solvestates.size()-1] ;
togo++ ;
sp-- ;
st = ss.st ;
mi = ss.mi ;
mask = ss.mask ;
skipbase = ss.skipbase ;
}
solvestates.pop_back() ;
if (v == 1)
goto upstack ;
if (!quarter && v == -1) {
m = randomstart ? randomized[togo][mi] : mi ;
if (pd.moves[m].base < 64)
skipbase |= 1LL << pd.moves[m].base ;
}
downstack:
mi++ ;
if (mi >= (int)pd.moves.size()) {
v = 0 ;
goto upstack ;
}
h = innersetup(pt, sp) ;
}
}
ull solveworker::innersetup(prunetable &pt, int sp) {
lookups++ ;
return pt.prefetchindexed(pt.gethashforlookup(posns[sp], looktmp)) ;
}
int solveworker::innerfetch(const puzdef &pd, prunetable &pt, int &togo, int &sp, int &st, ull h) {
int v = pt.lookuphindexed(h) ;
int m, mi ;
ull mask, skipbase ;
if (v > togo + 1) {
v = -1 ;
} else if (v > togo) {
v = 0 ;
} else if (v == 0 && togo == 1 && didprepass && pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
} else if (v == 0 && togo > 0 && noearlysolutions && pd.comparepos(posns[sp], pd.solved) == 0) {
v = 0 ;
} else if (togo == 0) {
v = possibsolution(pd, sp) ;
} else {
mask = canonmask[st] ;
skipbase = 0 ;
mi = -1 ;
goto downstack ;
}
upstack:
if (solvestates.size() == 0)
return v ;
{
auto &ss = solvestates[solvestates.size()-1] ;
togo++ ;
sp-- ;
st = ss.st ;
mi = ss.mi ;
mask = ss.mask ;
skipbase = ss.skipbase ;
}
solvestates.pop_back() ;
if (v == 1)
goto upstack ;
if (!quarter && v == -1) {
m = randomstart ? randomized[togo][mi] : mi ;
const moove &mv = pd.moves[m] ;
if (!quarter && mv.base < 64 && ((skipbase >> mv.base) & 1))
goto downstack ;
if ((mask >> mv.cs) & 1)
goto downstack ;
pd.mul(posns[sp], mv.pos, posns[sp+1]) ;
if (!pd.legalstate(posns[sp+1]))
goto downstack ;
movehist[sp] = m ;
solvestates.push_back({st, mi, mask, skipbase}) ;
togo-- ;
sp++ ;
st = canonnext[st][mv.cs] ;
if (pd.moves[m].base < 64)
skipbase |= 1LL << pd.moves[m].base ;
}
downstack:
mi++ ;
if (mi >= (int)pd.moves.size()) {
v = 0 ;
goto upstack ;
}
m = randomstart ? randomized[togo][mi] : mi ;
const moove &mv = pd.moves[m] ;
if (!quarter && mv.base < 64 && ((skipbase >> mv.base) & 1))
goto downstack ;
if ((mask >> mv.cs) & 1)
goto downstack ;
pd.mul(posns[sp], mv.pos, posns[sp+1]) ;
if (!pd.legalstate(posns[sp+1]))
goto downstack ;
movehist[sp] = m ;
solvestates.push_back({st, mi, mask, skipbase}) ;
togo-- ;
sp++ ;
st = canonnext[st][mv.cs] ;
return 3 ;
}
int solveworker::solvestart(const puzdef &pd, prunetable &pt, int w) {
ull initmoves = workchunks[w] ;
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/solve.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct solveworker {
char padding[256] ; // kill false sharing
void init(const puzdef &pd, int d_, int id_, const setval &p) ;
int solveiter(const puzdef &pd, prunetable &pt, int togo, int sp, int st) ;
ull innersetup(prunetable &pt, int sp) ;
int innerfetch(const puzdef &pd, prunetable &pt, int &togo, int &sp, int &st, ull h) ;
int possibsolution(const puzdef &pd, int sp) ;
int solvestart(const puzdef &pd, prunetable &pt, int w) ;
void dowork(const puzdef &pd, prunetable &pt) ;
Expand Down

0 comments on commit 11c2d73

Please sign in to comment.