Skip to content

Commit 07ad5b6

Browse files
committed
Testing different pruning table strategies.
1 parent e289b49 commit 07ad5b6

File tree

2 files changed

+81
-18
lines changed

2 files changed

+81
-18
lines changed

src/cpp/prunetable.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <set>
44
#include "prunetable.h"
55
#include "city.h"
6+
using namespace std ;
67
#define USECOMPRESSION
78
#ifdef USECOMPRESSION
89
#define SIGNATURE COMPSIGNATURE
@@ -91,10 +92,11 @@ ull fillworker::fillflush(prunetable &pt, int shard) {
9192
pt.fillcnt += fb.nchunks ;
9293
for (int i=0; i<fb.nchunks; i++) {
9394
ull h = fb.chunks[i] ;
94-
if (((pt.mem[h>>5] >> (2*(h&31))) & 3) == 0) {
95-
pt.mem[h>>5] += (3LL - pt.wval) << (2*(h&31)) ;
96-
if ((pt.mem[(h>>5)&-8] & 15) == 0)
97-
pt.mem[(h>>5)&-8] += 1 + pt.wbval ;
95+
if (((pt.mem[h>>HSHIFT] >> (HMUL*(h&LOWHMASK))) & PRIMARYMASK) == 0) {
96+
pt.mem[h>>HSHIFT] += ((ull)(PRIMARYMASK) - pt.wval) << (HMUL*(h&LOWHMASK)) ;
97+
h &= ~LOWCLEARMASK ;
98+
if ((pt.mem[(h>>HSHIFT)] & SECONDARYMASK) == 0)
99+
pt.mem[(h>>HSHIFT)] += 1 + pt.wbval ;
98100
r++ ;
99101
}
100102
}
@@ -261,7 +263,7 @@ prunetable::prunetable(const puzdef &pd, ull maxmem) {
261263
subshift = sh+1 ;
262264
bytesize |= bytesize >> 1 ;
263265
}
264-
size = bytesize * 4 ;
266+
size = bytesize * (8 >> PRIMARYBITS) ;
265267
shardshift = 0 ;
266268
while ((size >> shardshift) > MEMSHARDS)
267269
shardshift++ ;
@@ -333,6 +335,8 @@ void prunetable::checkextend(const puzdef &pd, int ignorelookup) {
333335
(pd.logstates <= 50 && prediction > pd.llstates))
334336
return ;
335337
if (wval == 2) {
338+
cerr << "Finish fixing up this code." << endl ;
339+
exit(10) ;
336340
ull longcnt = (size + 31) >> 5 ;
337341
if (quiet == 0)
338342
cout << "Demoting memory values " << flush ;
@@ -389,10 +393,16 @@ void prunetable::addsumdat(const puzdef &pd, string &filename) const {
389393
}
390394
string prunetable::makefilename(const puzdef &pd) const {
391395
#ifdef USECOMPRESSION
392-
string filename = "tws7-" + inputbasename + "-" ;
396+
string filename = "xws7-" ;
393397
#else
394-
string filename = "tws6-" + inputbasename + "-" ;
398+
string filename = "xws6-" ;
395399
#endif
400+
filename += (char)('a'+BLOCKSIZEBITS) ;
401+
filename += (char)('a'+PRIMARYBITS) ;
402+
filename += (char)('a'+SECONDARYBITS) ;
403+
filename += "-" ;
404+
filename += inputbasename ;
405+
filename += "-" ;
396406
if (quarter)
397407
filename += "q-" ;
398408
ull bytes = size >> 2 ;

src/cpp/prunetable.h

+64-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "workchunks.h"
55
#include "rotations.h"
66
#include "canon.h"
7+
#include <iostream>
78
/*
89
* This code supports pruning tables for arbitrary puzzles. Memory
910
* consumption is a power of two. Each table entry is two bits.
@@ -18,6 +19,28 @@
1819
* The hash table permits collisions (stores the minimum).
1920
*/
2021
const int CACHELINESIZE = 64 ;
22+
// BLOCKSIZE bits has one secondary entry and as many primary ones as fit.
23+
// All of these values are log2 values. SECONDARYBITS should be greater than
24+
// PRIMARYBITS.
25+
// Given a hash value h that's been adjusted to fit in the range, we
26+
// want to make sure the low bit is 0 if PRIMARYBITS is 1. Also, of
27+
// the low (BLOCKSIZEBITS-PRIMARYBITS), we want to make sure the value
28+
// is at least PRIMARYBITS.
29+
// different way to ensure we set the low bits: pick off the low 16,
30+
// subtract a shifted fraction of them, then add the new base.
31+
#define BLOCKSIZEBITS (6)
32+
#define PRIMARYBITS (1)
33+
#define SECONDARYBITS (2)
34+
//
35+
#define PRIMARYMASK ((1<<(1<<PRIMARYBITS))-1)
36+
#define SECONDARYMASK ((1<<(1<<SECONDARYBITS))-1)
37+
#define HSHIFT (6 - PRIMARYBITS)
38+
#define HMUL (1+PRIMARYBITS)
39+
#define LOWHMASK ((1<<HSHIFT)-1)
40+
#define LOWHMIN (1<<(SECONDARYBITS-PRIMARYBITS)) ;
41+
#define LOWCHECK ((1<<(BLOCKSIZEBITS-PRIMARYBITS)) - (1<<(SECONDARYBITS-PRIMARYBITS)))
42+
#define LOWCLEARMASK ((1 << (BLOCKSIZEBITS-PRIMARYBITS)) - 1)
43+
//
2144
const int COMPSIGNATURE = 23 ; // start and end of data files
2245
const int UNCOMPSIGNATURE = 24 ; // start and end of data files
2346
#define UNKNOWNPUZZLE "unknownpuzzle"
@@ -85,20 +108,45 @@ struct prunetable {
85108
void checkextend(const puzdef &pd, int ignorelookups=0) ;
86109
int lookuph(ull h) const {
87110
h = indexhash(h) ;
88-
int v = 3 & (mem[h >> 5] >> ((h & 31) * 2)) ;
89-
if (v == 3)
90-
return (mem[(h >> 5) & ~7] & 15) - 1 ;
91-
else
111+
int v = PRIMARYMASK & (mem[h >> HSHIFT] >> ((h & LOWHMASK) * HMUL)) ;
112+
if (v == PRIMARYMASK) {
113+
h &= ~LOWCLEARMASK ;
114+
return (mem[h >> HSHIFT] & SECONDARYMASK) - 1 ;
115+
} else
116+
#if PRIMARYBITS == 1
92117
return 2 - v + baseval ;
118+
#else
119+
return 2 + baseval ;
120+
#endif
93121
}
94122
void prefetch(ull h) const {
95-
__builtin_prefetch(mem+((indexhash(h)) >> 5)) ;
123+
__builtin_prefetch(mem+((indexhash(h)) >> HSHIFT)) ;
96124
}
97125
ull indexhash(ull lowb) const {
98126
ull h = lowb ;
99127
h -= h >> subshift ;
100-
h >>= memshift ;
101-
h ^= 0xff & ((((h & 0xfe) - 2) >> 8) & (lowb | 2)) ;
128+
const ull BS = BLOCKSIZEBITS - SECONDARYBITS ;
129+
const ull BP = BLOCKSIZEBITS - PRIMARYBITS ;
130+
h = (h - ((h & ((1LL << (memshift + BP)) - 1)) >> BS) -
131+
(h & 1) + (1LL << (memshift + SECONDARYBITS - PRIMARYBITS))) >> memshift ;
132+
if ((h & LOWCHECK) == 0) {
133+
std::cout << "Fail with h " << std::hex << h << std::dec << std::endl << std::flush ;
134+
h = lowb ;
135+
std::cout << "Started with " << std::hex << lowb << std::dec << std::endl << std::flush ;
136+
h -= h >> subshift ;
137+
std::cout << "Next was " << std::hex << h << std::dec << std::endl << std::flush ;
138+
std::cout << "Sub1 " << std::hex <<
139+
((h & ((1LL << (memshift + BP)) - 1)) >> BS)
140+
<< std::dec << std::endl << std::flush ;
141+
std::cout << "Sub2 " << std::hex <<
142+
(h&1)
143+
<< std::dec << std::endl << std::flush ;
144+
std::cout << "Add " << std::hex <<
145+
(1LL << (memshift + SECONDARYBITS - PRIMARYBITS))
146+
<< std::dec << std::endl << std::flush ;
147+
exit(10) ;
148+
}
149+
// std::cout << "ih " << std::hex << h << std::dec << std::endl ;
102150
return h ;
103151
}
104152
ull indexhash(int n, const setval sv) const {
@@ -112,11 +160,16 @@ struct prunetable {
112160
} else {
113161
h = indexhash(totsize, sv) ;
114162
}
115-
int v = 3 & (mem[h >> 5] >> ((h & 31) * 2)) ;
116-
if (v == 3)
117-
return (mem[(h >> 5) & ~7] & 15) - 1 ;
118-
else
163+
int v = PRIMARYMASK & (mem[h >> HSHIFT] >> ((h & LOWHMASK) * HMUL)) ;
164+
if (v == PRIMARYMASK) {
165+
h &= ~LOWCLEARMASK ;
166+
return (mem[h >> HSHIFT] & SECONDARYMASK) - 1 ;
167+
} else
168+
#if PRIMARYBITS == 1
119169
return 2 - v + baseval ;
170+
#else
171+
return 2 + baseval ;
172+
#endif
120173
}
121174
void addlookups(ull lookups) {
122175
lookupcnt += lookups ;

0 commit comments

Comments
 (0)