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;
53 bool operator< (const Element& other) const {
54 int s = memcmp_different_len(bpfen, bpfen_len, other.bpfen, other.bpfen_len);
60 vector<Element> elems;
61 int num_written_subshards = 0;
64 void write_subshard(const char *basename, ShardData* shard, int bucket)
66 string buf; // Keep allocated.
68 snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
69 basename, bucket, shard->num_written_subshards++);
70 printf("Writing SSTable %s...\n", filename);
72 sort(shard->elems.begin(), shard->elems.end());
74 mtbl_writer_options* wopt = mtbl_writer_options_init();
75 mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
76 mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
78 unordered_set<string> moves;
79 for (size_t i = 0; i < shard->elems.size(); ++i) {
80 const Element &e = shard->elems[i];
81 if (e.result == WHITE) {
82 c.set_white(c.white() + 1);
83 } else if (e.result == DRAW) {
84 c.set_draw(c.draw() + 1);
85 } else if (e.result == BLACK) {
86 c.set_black(c.black() + 1);
88 if (e.white_elo >= 100 && e.black_elo >= 100) {
89 c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
90 c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
91 c.set_num_elo(c.num_elo() + 1);
93 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
94 if (e.timestamp != DUMMY_TIMESTAMP) {
95 c.set_first_timestamp(e.timestamp);
97 c.set_opening_num(e.opening_num);
98 c.set_pgn_file_num(e.file_num);
99 c.set_pgn_start_position(e.start_position);
101 if (strlen(e.move) > 0 && !moves.count(e.move)) {
102 moves.insert(e.move);
105 if (i == shard->elems.size() - 1 ||
106 e.bpfen_len != shard->elems[i + 1].bpfen_len ||
107 memcmp(e.bpfen, shard->elems[i + 1].bpfen, e.bpfen_len) != 0) {
108 c.SerializeToString(&buf);
109 mtbl_res res = mtbl_writer_add(mtbl,
110 (const uint8_t *)e.bpfen, e.bpfen_len,
111 (const uint8_t *)buf.data(), buf.size());
112 assert(res == mtbl_res_success);
117 mtbl_writer_destroy(&mtbl);
119 shard->elems.clear();
122 int main(int argc, char **argv)
124 int num_buckets = atoi(argv[argc - 2]);
125 size_t num_pos_per_subshard = atoi(argv[argc - 1]); // 500000 is a reasonable value.
127 vector<ShardData> shards;
128 shards.resize(num_buckets);
130 for (int i = 0; i < num_buckets; ++i) {
131 shards[i].elems.reserve(num_pos_per_subshard);
134 size_t num_elems = 0;
135 for (int i = 1; i < argc - 3; ++i) {
137 if (strcmp(argv[i], "-") == 0) {
140 fp = fopen(argv[i], "rb");
149 int bpfen_len = getc(fp);
150 if (bpfen_len == -1) {
153 if (bpfen_len <= 0) {
154 fprintf(stderr, "Underlong BPFEN (%d bytes)\n", bpfen_len);
157 if (bpfen_len >= 32) {
158 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
161 if (fread(bpfen, bpfen_len, 1, fp) != 1) {
162 perror("fread(bpfen)");
172 int opening_num, white_elo, black_elo, file_num;
175 if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
176 perror("fread(white_elo)");
179 if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
180 perror("fread(black_elo)");
183 if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
184 perror("fread(opening_num)");
187 if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) {
188 perror("fread(timestamp)");
191 if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
192 perror("fread(file_num)");
195 if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
196 perror("fread(start_position)");
204 perror("getc(move_length)");
207 if (l >= int(sizeof(move))) {
208 fprintf(stderr, "Overlong move (%d bytes)\n", l);
213 } else if (fread(&move[0], l, 1, fp) != 1) {
214 perror("fread(move)");
219 int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
221 memcpy(e.bpfen, bpfen, bpfen_len);
222 e.bpfen_len = bpfen_len;
223 strcpy(e.move, move);
224 e.result = Result(r);
225 e.opening_num = opening_num;
226 e.white_elo = white_elo;
227 e.black_elo = black_elo;
228 e.file_num = file_num;
229 e.timestamp = timestamp;
230 e.start_position = start_position;
231 shards[bucket].elems.push_back(e);
234 if (shards[bucket].elems.size() >= num_pos_per_subshard) {
235 write_subshard(argv[argc - 3], &shards[bucket], bucket);
236 shards[bucket].elems.reserve(num_pos_per_subshard);
241 printf("Read %ld elems\n", num_elems);
244 for (int i = 0; i < num_buckets; ++i) {
245 write_subshard(argv[argc - 3], &shards[i], i);