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