]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Speed up binloader by ~10-15%.
[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 inline int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
23 {
24         size_t shared_len = min(n1, n2);
25         if (shared_len >= 8) {
26                 uint64_t a1 = *(const uint64_t *)s1;
27                 uint64_t a2 = *(const uint64_t *)s2;
28                 if (a1 != a2) {
29                         a1 = __builtin_bswap64(a1);
30                         a2 = __builtin_bswap64(a2);
31                         return (a1 < a2) ? -1 : 1;
32                 }
33         }
34
35         int s = memcmp(s1, s2, shared_len);
36         if (s != 0) {
37                 return s;
38         }
39
40         return n2 - n1;
41 }
42
43 enum Result { WHITE = 0, DRAW, BLACK };
44 struct Element {
45         char *bpfen;  // includes prev_board_hash
46         int bpfen_len;
47         char move[8];   // Na1xc3+
48         Result result;
49         int opening_num, white_elo, black_elo;
50         int file_num;
51         time_t timestamp;
52         long start_position;
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         unique_ptr<Arena> arena;  // Used to allocate bpfen.
63         int num_written_subshards = 0;
64 };
65
66 void write_subshard(const char *basename, ShardData* shard, int bucket)
67 {
68         string buf;  // Keep allocated.
69         char filename[256];
70         snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
71                 basename, bucket, shard->num_written_subshards++);
72         printf("Writing SSTable %s...\n", filename);
73
74         sort(shard->elems.begin(), shard->elems.end());
75
76         mtbl_writer_options* wopt = mtbl_writer_options_init();
77         mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
78         mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
79         Count c;
80         unordered_set<string> moves;
81         for (size_t i = 0; i < shard->elems.size(); ++i) {
82                 const Element &e = shard->elems[i];
83                 if (e.result == WHITE) {
84                         c.set_white(c.white() + 1);
85                 } else if (e.result == DRAW) {
86                         c.set_draw(c.draw() + 1);
87                 } else if (e.result == BLACK) {
88                         c.set_black(c.black() + 1);
89                 }
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 (!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         shard->arena.reset(new Arena);
123 }
124
125 int main(int argc, char **argv)
126 {
127         int num_buckets = atoi(argv[argc - 2]);
128         size_t num_pos_per_subshard = atoi(argv[argc - 1]);  // 500000 is a reasonable value.
129
130         vector<ShardData> shards;
131         shards.resize(num_buckets);
132
133         for (int i = 0; i < num_buckets; ++i) {
134                 shards[i].elems.reserve(num_pos_per_subshard);
135                 shards[i].arena.reset(new Arena);
136         }
137
138         size_t num_elems = 0;
139         for (int i = 1; i < argc - 3; ++i) {
140                 FILE *fp;
141                 if (strcmp(argv[i], "-") == 0) {
142                         fp = stdin;
143                 } else {
144                         fp = fopen(argv[i], "rb");
145                         if (fp == NULL) {
146                                 perror(argv[i]);
147                                 exit(1);
148                         }
149                 }
150                 for ( ;; ) {
151                         char bpfen[256];
152
153                         int bpfen_len = getc(fp);
154                         if (bpfen_len == -1) {
155                                 break;
156                         }
157                         if (bpfen_len >= int(sizeof(bpfen))) {
158                                 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
159                 //              exit(1);
160                                 break;
161                         }
162                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
163                                 perror("fread()");
164                 //              exit(1);
165                                 break;
166                         }
167
168                         int r = getc(fp);
169                         if (r == -1) {
170                                 perror("getc()");
171                                 //exit(1);
172                                 break;
173                         }
174
175                         int opening_num, white_elo, black_elo, file_num;
176                         time_t timestamp;
177                         long start_position;
178                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
179                                 perror("fread()");
180                                 //exit(1);
181                                 break;
182                         }
183                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
184                                 perror("fread()");
185                                 //exit(1);
186                                 break;
187                         }
188                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
189                                 perror("fread()");
190                                 //exit(1);
191                                 break;
192                         }
193                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
194                                 perror("fread()");
195                                 //exit(1);
196                                 break;
197                         }
198                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
199                                 perror("fread()");
200                                 //exit(1);
201                                 break;
202                         }
203                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
204                                 perror("fread()");
205                                 //exit(1);
206                                 break;
207                         }
208
209
210                         char move[8];
211                         int l = getc(fp);
212                         if (l == -1) {
213                                 break;
214                         }
215                         if (l >= int(sizeof(move))) {
216                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
217                 //              exit(1);
218                                 break;
219                         }
220                         if (fread(&move[0], l, 1, fp) != 1) {
221                                 perror("fread()");
222                 //              exit(1);
223                                 break;
224                         }
225                         move[l] = 0;
226
227                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
228                         Element e;
229                         e.bpfen = shards[bucket].arena->alloc(bpfen_len);
230                         memcpy(e.bpfen, bpfen, bpfen_len);
231                         e.bpfen_len = bpfen_len;
232                         strcpy(e.move, move);
233                         e.result = Result(r);
234                         e.opening_num = opening_num;
235                         e.white_elo = white_elo;
236                         e.black_elo = black_elo;
237                         e.file_num = file_num;
238                         e.timestamp = timestamp;
239                         e.start_position = start_position;
240                         shards[bucket].elems.push_back(e);
241                         ++num_elems;
242
243                         if (shards[bucket].elems.size() >= num_pos_per_subshard) {
244                                 write_subshard(argv[argc - 3], &shards[bucket], bucket);
245                                 shards[bucket].elems.reserve(num_pos_per_subshard);
246                         }
247                 }
248                 fclose(fp);
249
250                 printf("Read %ld elems\n", num_elems);
251         }
252
253         for (int i = 0; i < num_buckets; ++i) {
254                 write_subshard(argv[argc - 3], &shards[i], i);
255         }
256 }
257