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 int memcmp_different_len(const void *s1, size_t n1, const void *s2, size_t n2)
24 int shared_len = min(n1, n2);
25 int s = memcmp(s1, s2, shared_len);
39 enum Result { WHITE = 0, DRAW, BLACK };
41 char *bpfen; // includes prev_board_hash
43 char move[8]; // Na1xc3+
45 int opening_num, white_elo, black_elo;
50 bool operator< (const Element& other) const {
51 int s = memcmp_different_len(bpfen, bpfen_len, other.bpfen, other.bpfen_len);
57 vector<Element> elems;
58 unique_ptr<Arena> arena; // Used to allocate bpfen.
59 int num_written_subshards = 0;
62 void write_subshard(const char *basename, ShardData* shard, int bucket)
64 string buf; // Keep allocated.
66 snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
67 basename, bucket, shard->num_written_subshards++);
68 printf("Writing SSTable %s...\n", filename);
70 sort(shard->elems.begin(), shard->elems.end());
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);
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);
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);
91 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
92 if (e.timestamp != DUMMY_TIMESTAMP) {
93 c.set_first_timestamp(e.timestamp);
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);
99 if (!moves.count(e.move)) {
100 moves.insert(e.move);
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_writer_add(mtbl,
108 (const uint8_t *)e.bpfen, e.bpfen_len,
109 (const uint8_t *)buf.data(), buf.size());
114 mtbl_writer_destroy(&mtbl);
116 shard->elems.clear();
117 shard->arena.reset(new Arena);
120 int main(int argc, char **argv)
122 int num_buckets = atoi(argv[argc - 2]);
123 size_t num_pos_per_subshard = atoi(argv[argc - 1]); // 500000 is a reasonable value.
125 vector<ShardData> shards;
126 shards.resize(num_buckets);
128 for (int i = 0; i < num_buckets; ++i) {
129 shards[i].elems.reserve(num_pos_per_subshard);
130 shards[i].arena.reset(new Arena);
133 size_t num_elems = 0;
134 for (int i = 1; i < argc - 3; ++i) {
136 if (strcmp(argv[i], "-") == 0) {
139 fp = fopen(argv[i], "rb");
148 int bpfen_len = getc(fp);
149 if (bpfen_len == -1) {
152 if (bpfen_len >= int(sizeof(bpfen))) {
153 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
157 if (fread(bpfen, bpfen_len, 1, fp) != 1) {
170 int opening_num, white_elo, black_elo, file_num;
173 if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
178 if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
183 if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
188 if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) {
193 if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
198 if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
210 if (l >= int(sizeof(move))) {
211 fprintf(stderr, "Overlong move (%d bytes)\n", l);
215 if (fread(&move[0], l, 1, fp) != 1) {
222 int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
224 e.bpfen = shards[bucket].arena->alloc(bpfen_len);
225 memcpy(e.bpfen, bpfen, bpfen_len);
226 e.bpfen_len = bpfen_len;
227 strcpy(e.move, move);
228 e.result = Result(r);
229 e.opening_num = opening_num;
230 e.white_elo = white_elo;
231 e.black_elo = black_elo;
232 e.file_num = file_num;
233 e.timestamp = timestamp;
234 e.start_position = start_position;
235 shards[bucket].elems.push_back(e);
238 if (shards[bucket].elems.size() >= num_pos_per_subshard) {
239 write_subshard(argv[argc - 3], &shards[bucket], bucket);
240 shards[bucket].elems.reserve(num_pos_per_subshard);
245 printf("Read %ld elems\n", num_elems);
248 for (int i = 0; i < num_buckets; ++i) {
249 write_subshard(argv[argc - 3], &shards[i], i);