1 //#define _GLIBCXX_PARALLEL
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
12 #include <unordered_set>
17 #define DUMMY_TIMESTAMP 32503680000
21 static inline int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
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;
28 a1 = __builtin_bswap64(a1);
29 a2 = __builtin_bswap64(a2);
30 return (a1 < a2) ? -1 : 1;
34 int s = memcmp(s1, s2, shared_len);
42 enum Result { WHITE = 0, DRAW, BLACK };
44 char bpfen[32]; // includes prev_board_hash
46 char move[8]; // Na1xc3+
48 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 int num_written_subshards = 0;
65 void write_subshard(const char *basename, ShardData* shard, int bucket)
67 string buf; // Keep allocated.
69 snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
70 basename, bucket, shard->num_written_subshards++);
71 printf("Writing SSTable %s...\n", filename);
73 sort(shard->elems.begin(), shard->elems.end());
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);
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);
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);
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 (strlen(e.move) > 0 && !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();
124 int main(int argc, char **argv)
126 int num_buckets = atoi(argv[argc - 2]);
127 size_t num_pos_per_subshard = atoi(argv[argc - 1]); // 500000 is a reasonable value.
129 vector<ShardData> shards;
130 shards.resize(num_buckets);
132 for (int i = 0; i < num_buckets; ++i) {
133 shards[i].elems.reserve(num_pos_per_subshard);
136 size_t num_elems = 0;
137 for (int i = 1; i < argc - 3; ++i) {
139 if (strcmp(argv[i], "-") == 0) {
142 fp = fopen(argv[i], "rb");
151 int bpfen_len = getc(fp);
152 if (bpfen_len == -1) {
155 if (bpfen_len <= 0) {
156 fprintf(stderr, "Underlong BPFEN (%d bytes)\n", bpfen_len);
159 if (bpfen_len >= 32) {
160 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
163 if (fread(bpfen, bpfen_len, 1, fp) != 1) {
164 perror("fread(bpfen)");
174 int opening_num, white_elo, black_elo, file_num;
177 if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
178 perror("fread(white_elo)");
181 if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
182 perror("fread(black_elo)");
185 if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
186 perror("fread(opening_num)");
189 if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) {
190 perror("fread(timestamp)");
193 if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
194 perror("fread(file_num)");
197 if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
198 perror("fread(start_position)");
202 int computer = getc(fp);
203 if (computer == -1) {
204 perror("getc(computer)");
211 perror("getc(move_length)");
214 if (l >= int(sizeof(move))) {
215 fprintf(stderr, "Overlong move (%d bytes)\n", l);
220 } else if (fread(&move[0], l, 1, fp) != 1) {
221 perror("fread(move)");
226 int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
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);
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);
249 printf("Read %ld elems\n", num_elems);
252 for (int i = 0; i < num_buckets; ++i) {
253 write_subshard(argv[argc - 3], &shards[i], i);