]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Clean up error messages a bit.
[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 (strlen(e.move) > 0 && !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                         if (bpfen_len <= 0) {
154                                 fprintf(stderr, "Underlong BPFEN (%d bytes)\n", bpfen_len);
155                                 exit(1);
156                         }
157                         if (bpfen_len >= 32) {
158                                 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
159                                 exit(1);
160                         }
161                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
162                                 perror("fread(bpfen)");
163                                 exit(1);
164                         }
165
166                         int r = getc(fp);
167                         if (r == -1) {
168                                 perror("getc()");
169                                 exit(1);
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(white_elo)");
177                                 exit(1);
178                         }
179                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
180                                 perror("fread(black_elo)");
181                                 exit(1);
182                         }
183                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
184                                 perror("fread(opening_num)");
185                                 exit(1);
186                         }
187                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
188                                 perror("fread(timestamp)");
189                                 exit(1);
190                         }
191                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
192                                 perror("fread(file_num)");
193                                 exit(1);
194                         }
195                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
196                                 perror("fread(start_position)");
197                                 exit(1);
198                         }
199
200
201                         char move[8];
202                         int l = getc(fp);
203                         if (l == -1) {
204                                 perror("getc(move_length)");
205                                 break;
206                         }
207                         if (l >= int(sizeof(move))) {
208                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
209                                 exit(1);
210                         }
211                         if (l == 0) {
212                                 move[0] = 0;
213                         } else if (fread(&move[0], l, 1, fp) != 1) {
214                                 perror("fread(move)");
215                                 exit(1);
216                         }
217                         move[l] = 0;
218
219                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
220                         Element e;
221                         memcpy(e.bpfen, bpfen, bpfen_len);
222                         e.bpfen_len = bpfen_len;
223                         strcpy(e.move, move);
224                         e.result = Result(r);
225                         e.opening_num = opening_num;
226                         e.white_elo = white_elo;
227                         e.black_elo = black_elo;
228                         e.file_num = file_num;
229                         e.timestamp = timestamp;
230                         e.start_position = start_position;
231                         shards[bucket].elems.push_back(e);
232                         ++num_elems;
233
234                         if (shards[bucket].elems.size() >= num_pos_per_subshard) {
235                                 write_subshard(argv[argc - 3], &shards[bucket], bucket);
236                                 shards[bucket].elems.reserve(num_pos_per_subshard);
237                         }
238                 }
239                 fclose(fp);
240
241                 printf("Read %ld elems\n", num_elems);
242         }
243
244         for (int i = 0; i < num_buckets; ++i) {
245                 write_subshard(argv[argc - 3], &shards[i], i);
246         }
247 }
248