]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Drop the arena; BPFENs are bounded by 32 bytes anyway, and the typical is something...
[remoteglot-book] / binloader.cpp
1 //#define _GLIBCXX_PARALLEL
2
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
4
5 #include <stdio.h>
6 #include <vector>
7 #include <mtbl.h>
8 #include <algorithm>
9 #include <utility>
10 #include <memory>
11 #include <string>
12 #include <unordered_set>
13 #include <string.h>
14 #include "count.pb.h"
15 #include "hash.h"
16
17 #define DUMMY_TIMESTAMP 32503680000
18
19 using namespace std;
20
21 static inline int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
22 {
23         size_t shared_len = min(n1, n2);
24         if (shared_len >= 8) {
25                 uint64_t a1 = *(const uint64_t *)s1;
26                 uint64_t a2 = *(const uint64_t *)s2;
27                 if (a1 != a2) {
28                         a1 = __builtin_bswap64(a1);
29                         a2 = __builtin_bswap64(a2);
30                         return (a1 < a2) ? -1 : 1;
31                 }
32         }
33
34         int s = memcmp(s1, s2, shared_len);
35         if (s != 0) {
36                 return s;
37         }
38
39         return n2 - n1;
40 }
41
42 enum Result { WHITE = 0, DRAW, BLACK };
43 struct Element {
44         char bpfen[32];  // includes prev_board_hash
45         int bpfen_len;
46         char move[8];   // Na1xc3+
47         Result result;
48         int opening_num, white_elo, black_elo;
49         int file_num;
50         time_t timestamp;
51         long start_position;
52
53         bool operator< (const Element& other) const {
54                 int s = memcmp_different_len(bpfen, bpfen_len, other.bpfen, other.bpfen_len);
55                 return s < 0;
56         }
57 };
58
59 struct ShardData {
60         vector<Element> elems;
61         int num_written_subshards = 0;
62 };
63
64 void write_subshard(const char *basename, ShardData* shard, int bucket)
65 {
66         string buf;  // Keep allocated.
67         char filename[256];
68         snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
69                 basename, bucket, shard->num_written_subshards++);
70         printf("Writing SSTable %s...\n", filename);
71
72         sort(shard->elems.begin(), shard->elems.end());
73
74         mtbl_writer_options* wopt = mtbl_writer_options_init();
75         mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
76         mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
77         Count c;
78         unordered_set<string> moves;
79         for (size_t i = 0; i < shard->elems.size(); ++i) {
80                 const Element &e = shard->elems[i];
81                 if (e.result == WHITE) {
82                         c.set_white(c.white() + 1);
83                 } else if (e.result == DRAW) {
84                         c.set_draw(c.draw() + 1);
85                 } else if (e.result == BLACK) {
86                         c.set_black(c.black() + 1);
87                 }
88                 if (e.white_elo >= 100 && e.black_elo >= 100) {
89                         c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
90                         c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
91                         c.set_num_elo(c.num_elo() + 1);
92                 }
93                 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
94                         if (e.timestamp != DUMMY_TIMESTAMP) {
95                                 c.set_first_timestamp(e.timestamp);
96                         }
97                         c.set_opening_num(e.opening_num);
98                         c.set_pgn_file_num(e.file_num);
99                         c.set_pgn_start_position(e.start_position);
100                 }
101                 if (!moves.count(e.move)) {
102                         moves.insert(e.move);
103                         c.add_move(e.move);
104                 }
105                 if (i == shard->elems.size() - 1 ||
106                     e.bpfen_len != shard->elems[i + 1].bpfen_len ||
107                     memcmp(e.bpfen, shard->elems[i + 1].bpfen, e.bpfen_len) != 0) {
108                         c.SerializeToString(&buf);
109                         mtbl_res res = mtbl_writer_add(mtbl,
110                                 (const uint8_t *)e.bpfen, e.bpfen_len,
111                                 (const uint8_t *)buf.data(), buf.size());
112                         assert(res == mtbl_res_success);
113                         c = Count();
114                         moves.clear();
115                 }
116         }
117         mtbl_writer_destroy(&mtbl);
118
119         shard->elems.clear();
120 }
121
122 int main(int argc, char **argv)
123 {
124         int num_buckets = atoi(argv[argc - 2]);
125         size_t num_pos_per_subshard = atoi(argv[argc - 1]);  // 500000 is a reasonable value.
126
127         vector<ShardData> shards;
128         shards.resize(num_buckets);
129
130         for (int i = 0; i < num_buckets; ++i) {
131                 shards[i].elems.reserve(num_pos_per_subshard);
132         }
133
134         size_t num_elems = 0;
135         for (int i = 1; i < argc - 3; ++i) {
136                 FILE *fp;
137                 if (strcmp(argv[i], "-") == 0) {
138                         fp = stdin;
139                 } else {
140                         fp = fopen(argv[i], "rb");
141                         if (fp == NULL) {
142                                 perror(argv[i]);
143                                 exit(1);
144                         }
145                 }
146                 for ( ;; ) {
147                         char bpfen[256];
148
149                         int bpfen_len = getc(fp);
150                         if (bpfen_len == -1) {
151                                 break;
152                         }
153                         assert(bpfen_len <= 32);
154                         if (bpfen_len >= int(sizeof(bpfen))) {
155                                 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
156                 //              exit(1);
157                                 break;
158                         }
159                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
160                                 perror("fread()");
161                 //              exit(1);
162                                 break;
163                         }
164
165                         int r = getc(fp);
166                         if (r == -1) {
167                                 perror("getc()");
168                                 //exit(1);
169                                 break;
170                         }
171
172                         int opening_num, white_elo, black_elo, file_num;
173                         time_t timestamp;
174                         long start_position;
175                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
176                                 perror("fread()");
177                                 //exit(1);
178                                 break;
179                         }
180                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
181                                 perror("fread()");
182                                 //exit(1);
183                                 break;
184                         }
185                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
186                                 perror("fread()");
187                                 //exit(1);
188                                 break;
189                         }
190                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
191                                 perror("fread()");
192                                 //exit(1);
193                                 break;
194                         }
195                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
196                                 perror("fread()");
197                                 //exit(1);
198                                 break;
199                         }
200                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
201                                 perror("fread()");
202                                 //exit(1);
203                                 break;
204                         }
205
206
207                         char move[8];
208                         int l = getc(fp);
209                         if (l == -1) {
210                                 break;
211                         }
212                         if (l >= int(sizeof(move))) {
213                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
214                 //              exit(1);
215                                 break;
216                         }
217                         if (fread(&move[0], l, 1, fp) != 1) {
218                                 perror("fread()");
219                 //              exit(1);
220                                 break;
221                         }
222                         move[l] = 0;
223
224                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
225                         Element e;
226                         memcpy(e.bpfen, bpfen, bpfen_len);
227                         e.bpfen_len = bpfen_len;
228                         strcpy(e.move, move);
229                         e.result = Result(r);
230                         e.opening_num = opening_num;
231                         e.white_elo = white_elo;
232                         e.black_elo = black_elo;
233                         e.file_num = file_num;
234                         e.timestamp = timestamp;
235                         e.start_position = start_position;
236                         shards[bucket].elems.push_back(e);
237                         ++num_elems;
238
239                         if (shards[bucket].elems.size() >= num_pos_per_subshard) {
240                                 write_subshard(argv[argc - 3], &shards[bucket], bucket);
241                                 shards[bucket].elems.reserve(num_pos_per_subshard);
242                         }
243                 }
244                 fclose(fp);
245
246                 printf("Read %ld elems\n", num_elems);
247         }
248
249         for (int i = 0; i < num_buckets; ++i) {
250                 write_subshard(argv[argc - 3], &shards[i], i);
251         }
252 }
253