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