]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
115cba61f571298146cf66ad44c0d473f3dafb3c
[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_res res = mtbl_writer_add(mtbl,
108                                 (const uint8_t *)e.bpfen, e.bpfen_len,
109                                 (const uint8_t *)buf.data(), buf.size());
110                         assert(res == mtbl_res_success);
111                         c = Count();
112                         moves.clear();
113                 }
114         }
115         mtbl_writer_destroy(&mtbl);
116
117         shard->elems.clear();
118         shard->arena.reset(new Arena);
119 }
120
121 int main(int argc, char **argv)
122 {
123         int num_buckets = atoi(argv[argc - 2]);
124         size_t num_pos_per_subshard = atoi(argv[argc - 1]);  // 500000 is a reasonable value.
125
126         vector<ShardData> shards;
127         shards.resize(num_buckets);
128
129         for (int i = 0; i < num_buckets; ++i) {
130                 shards[i].elems.reserve(num_pos_per_subshard);
131                 shards[i].arena.reset(new Arena);
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 >= int(sizeof(bpfen))) {
154                                 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
155                 //              exit(1);
156                                 break;
157                         }
158                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
159                                 perror("fread()");
160                 //              exit(1);
161                                 break;
162                         }
163
164                         int r = getc(fp);
165                         if (r == -1) {
166                                 perror("getc()");
167                                 //exit(1);
168                                 break;
169                         }
170
171                         int opening_num, white_elo, black_elo, file_num;
172                         time_t timestamp;
173                         long start_position;
174                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
175                                 perror("fread()");
176                                 //exit(1);
177                                 break;
178                         }
179                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
180                                 perror("fread()");
181                                 //exit(1);
182                                 break;
183                         }
184                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
185                                 perror("fread()");
186                                 //exit(1);
187                                 break;
188                         }
189                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
190                                 perror("fread()");
191                                 //exit(1);
192                                 break;
193                         }
194                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
195                                 perror("fread()");
196                                 //exit(1);
197                                 break;
198                         }
199                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
200                                 perror("fread()");
201                                 //exit(1);
202                                 break;
203                         }
204
205
206                         char move[8];
207                         int l = getc(fp);
208                         if (l == -1) {
209                                 break;
210                         }
211                         if (l >= int(sizeof(move))) {
212                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
213                 //              exit(1);
214                                 break;
215                         }
216                         if (fread(&move[0], l, 1, fp) != 1) {
217                                 perror("fread()");
218                 //              exit(1);
219                                 break;
220                         }
221                         move[l] = 0;
222
223                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
224                         Element e;
225                         e.bpfen = shards[bucket].arena->alloc(bpfen_len);
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