1 //#define _GLIBCXX_PARALLEL
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
12 #include <unordered_set>
18 #define DUMMY_TIMESTAMP 32503680000
22 static inline int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
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;
29 a1 = __builtin_bswap64(a1);
30 a2 = __builtin_bswap64(a2);
31 return (a1 < a2) ? -1 : 1;
35 int s = memcmp(s1, s2, shared_len);
43 enum Result { WHITE = 0, DRAW, BLACK };
45 char *bpfen; // includes prev_board_hash
47 char move[8]; // Na1xc3+
49 int opening_num, white_elo, black_elo;
54 bool operator< (const Element& other) const {
55 int s = memcmp_different_len(bpfen, bpfen_len, other.bpfen, other.bpfen_len);
61 vector<Element> elems;
62 unique_ptr<Arena> arena; // Used to allocate bpfen.
63 int num_written_subshards = 0;
66 void write_subshard(const char *basename, ShardData* shard, int bucket)
68 string buf; // Keep allocated.
70 snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
71 basename, bucket, shard->num_written_subshards++);
72 printf("Writing SSTable %s...\n", filename);
74 sort(shard->elems.begin(), shard->elems.end());
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);
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);
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);
95 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
96 if (e.timestamp != DUMMY_TIMESTAMP) {
97 c.set_first_timestamp(e.timestamp);
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);
103 if (!moves.count(e.move)) {
104 moves.insert(e.move);
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);
119 mtbl_writer_destroy(&mtbl);
121 shard->elems.clear();
122 shard->arena.reset(new Arena);
125 int main(int argc, char **argv)
127 int num_buckets = atoi(argv[argc - 2]);
128 size_t num_pos_per_subshard = atoi(argv[argc - 1]); // 500000 is a reasonable value.
130 vector<ShardData> shards;
131 shards.resize(num_buckets);
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);
138 size_t num_elems = 0;
139 for (int i = 1; i < argc - 3; ++i) {
141 if (strcmp(argv[i], "-") == 0) {
144 fp = fopen(argv[i], "rb");
153 int bpfen_len = getc(fp);
154 if (bpfen_len == -1) {
157 if (bpfen_len >= int(sizeof(bpfen))) {
158 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
162 if (fread(bpfen, bpfen_len, 1, fp) != 1) {
175 int opening_num, white_elo, black_elo, file_num;
178 if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
183 if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
188 if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
193 if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) {
198 if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
203 if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
215 if (l >= int(sizeof(move))) {
216 fprintf(stderr, "Overlong move (%d bytes)\n", l);
220 if (fread(&move[0], l, 1, fp) != 1) {
227 int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
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);
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);
250 printf("Read %ld elems\n", num_elems);
253 for (int i = 0; i < num_buckets; ++i) {
254 write_subshard(argv[argc - 3], &shards[i], i);