binmerger
eco.pgn
openings.txt
-open.mtbl
+open.mtbl.part????
+*.o
CXXFLAGS=-std=gnu++11 -O2 -g -Wall
-LDLIBS=-lmtbl
+LDLIBS=-lmtbl -lfarmhash
all: binloader binlookup binmerger
+binloader: binloader.o hash.o
+
.PHONY: clean
clean:
$(RM) binloader binlookup binmerger
//#define _GLIBCXX_PARALLEL
-// Usage: ./binloader IN1 IN2 IN3 ... OUT
+// Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS
#include <stdio.h>
#include <vector>
#include <string>
#include <string.h>
#include "count.h"
+#include "hash.h"
using namespace std;
int main(int argc, char **argv)
{
- vector<Element> elems;
+ int num_buckets = atoi(argv[argc - 1]);
- for (int i = 1; i < argc - 1; ++i) {
+ vector<vector<Element>> elems;
+ elems.resize(num_buckets);
+
+ size_t num_elems = 0;
+ for (int i = 1; i < argc - 2; ++i) {
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL) {
perror(argv[i]);
//exit(1);
break;
}
- elems.emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo});
+
+ int bucket = hash_key_to_bucket(bpfen_and_move.data(), bpfen_and_move.size(), num_buckets);
+ elems[bucket].emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo});
+ ++num_elems;
}
fclose(fp);
- printf("Read %ld elems\n", elems.size());
+ printf("Read %ld elems\n", num_elems);
}
printf("Sorting...\n");
- sort(elems.begin(), elems.end());
+ for (int i = 0; i < num_buckets; ++i) {
+ sort(elems[i].begin(), elems[i].end());
+ }
- printf("Writing SSTable...\n");
- mtbl_writer_options* wopt = mtbl_writer_options_init();
- mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
- mtbl_writer* mtbl = mtbl_writer_init(argv[argc - 1], wopt);
- Count c;
- for (int i = 0; i < elems.size(); ++i) {
- if (elems[i].result == WHITE) {
- ++c.white;
- } else if (elems[i].result == DRAW) {
- ++c.draw;
- } else if (elems[i].result == BLACK) {
- ++c.black;
- }
- c.opening_num = elems[i].opening_num;
- if (elems[i].white_elo >= 100 && elems[i].black_elo >= 100) {
- c.sum_white_elo += elems[i].white_elo;
- c.sum_black_elo += elems[i].black_elo;
- ++c.num_elo;
- }
- if (i == elems.size() - 1 || elems[i].bpfen_and_move != elems[i + 1].bpfen_and_move) {
- mtbl_writer_add(mtbl,
- (const uint8_t *)elems[i].bpfen_and_move.data(), elems[i].bpfen_and_move.size(),
- (const uint8_t *)&c, sizeof(c));
- c = Count();
+ printf("Writing SSTables...\n");
+ for (int i = 0; i < num_buckets; ++i) {
+ char filename[256];
+ snprintf(filename, sizeof(filename), "%s.part%04d", argv[argc - 2], i);
+
+ mtbl_writer_options* wopt = mtbl_writer_options_init();
+ mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
+ mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
+ Count c;
+ for (size_t j = 0; j < elems[i].size(); ++j) {
+ const Element &e = elems[i][j];
+ if (e.result == WHITE) {
+ ++c.white;
+ } else if (e.result == DRAW) {
+ ++c.draw;
+ } else if (e.result == BLACK) {
+ ++c.black;
+ }
+ c.opening_num = e.opening_num;
+ if (e.white_elo >= 100 && e.black_elo >= 100) {
+ c.sum_white_elo += e.white_elo;
+ c.sum_black_elo += e.black_elo;
+ ++c.num_elo;
+ }
+ if (j == elems[i].size() - 1 || e.bpfen_and_move != elems[i][j + 1].bpfen_and_move) {
+ mtbl_writer_add(mtbl,
+ (const uint8_t *)e.bpfen_and_move.data(), e.bpfen_and_move.size(),
+ (const uint8_t *)&c, sizeof(c));
+ c = Count();
+ }
}
+ mtbl_writer_destroy(&mtbl);
}
- mtbl_writer_destroy(&mtbl);
}
int main(int argc, char **argv)
{
- const char *hex_prefix = argv[2];
+ int num_buckets = atoi(argv[2]);
+ const char *hex_prefix = argv[3];
const int prefix_len = strlen(hex_prefix) / 2;
uint8_t *prefix = new uint8_t[prefix_len];
prefix[i] = k;
}
- mtbl_reader* mtbl = mtbl_reader_init(argv[1], NULL);
- const mtbl_source *src = mtbl_reader_source(mtbl);
- mtbl_iter *it = mtbl_source_get_prefix(src, prefix, prefix_len);
+ for (int i = 0; i < num_buckets; ++i) {
+ char filename[256];
+ snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], i);
- const uint8_t *key, *val;
- size_t len_key, len_val;
+ mtbl_reader* mtbl = mtbl_reader_init(filename, NULL);
+ const mtbl_source *src = mtbl_reader_source(mtbl);
+ mtbl_iter *it = mtbl_source_get_prefix(src, prefix, prefix_len);
- while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
- string move((char *)(key + prefix_len), len_key - prefix_len);
- const Count* c = (Count *)val;
- printf("%s %d %d %d %u %f %f %d\n", move.c_str(),
- c->white, c->draw, c->black, c->opening_num,
- float(c->sum_white_elo) / c->num_elo,
- float(c->sum_black_elo) / c->num_elo,
- c->num_elo);
+ const uint8_t *key, *val;
+ size_t len_key, len_val;
+
+ while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
+ string move((char *)(key + prefix_len), len_key - prefix_len);
+ const Count* c = (Count *)val;
+ printf("%s %d %d %d %u %f %f %d\n", move.c_str(),
+ c->white, c->draw, c->black, c->opening_num,
+ float(c->sum_white_elo) / c->num_elo,
+ float(c->sum_black_elo) / c->num_elo,
+ c->num_elo);
+ }
}
}
set -e
-rm -f part-*.bin part-*.mtbl open.mtbl.new 2>/dev/null
+rm -f part-*.bin part-*.mtbl part-*.mtbl.part???? open.mtbl.new open.mtbl.part???? open.mtbl.part????.new 2>/dev/null
for FILE in $@; do
date | tr -d "\n"
date
for FILE in part-*.bin; do
- ( ./binloader $FILE ${FILE/bin/mtbl} ) &
+ ( ./binloader $FILE ${FILE/bin/mtbl} 40 ) &
done
wait
rm -f part-*.bin
-./binmerger part-*.mtbl open.mtbl.new
-mv open.mtbl.new open.mtbl
-rm -f part-*.mtbl
+for X in $( seq 0 39 ); do
+ ( ./binmerger part-*.mtbl.part$( printf %04d $X ) open.mtbl.part$( printf %04d $X ).new ) &
+done
+wait
+
+for X in $( seq 0 39 ); do
+ mv open.mtbl.part$( printf %04d $X ).new open.mtbl.part$( printf %04d $X)
+done
+rm -f part-*.mtbl.part????
--- /dev/null
+#include <farmhash.h>
+#include <algorithm>
+#include "hash.h"
+
+using namespace std;
+
+int hash_key_to_bucket(const char* s, size_t len, int num_buckets)
+{
+ // We hash only the first 10 bytes; it should be enough to get a
+ // reasonable spread, but also mostly miss the move, so that
+ // same position + different move usually land in the same bucket.
+ len = max<size_t>(len, 10);
+ return util::Fingerprint32(s, len) % num_buckets;
+}
--- /dev/null
+#ifndef _HASH_H
+#define _HASH_H 1
+
+int hash_key_to_bucket(const char* s, size_t len, int num_buckets);
+
+#endif // !defined(_HASH_H)
my $fen = $ARGV[0];
my $pos = Position->from_fen($fen);
my $hex = unpack('H*', $pos->bitpacked_fen);
-system("./binlookup", "./open.mtbl", $hex);
+system("./binlookup", "./open.mtbl", "40", $hex);
my $fen = $cgi->param('fen');
my $pos = Position->from_fen($fen);
my $hex = unpack('H*', $pos->bitpacked_fen);
-open my $fh, "-|", "../binlookup", "../open.mtbl", $hex
+open my $fh, "-|", "../binlookup", "../open.mtbl", "40", $hex
or die "../binlookup: $!";
my $opening;