Skip to content

Commit e04a192

Browse files
authored
memory mapping (#96)
* updated external/bits * allow memory mapping
1 parent 1bbe0af commit e04a192

5 files changed

Lines changed: 43 additions & 21 deletions

File tree

include/partitioned_phf.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ struct partitioned_phf //
9494
m_num_keys = builder.num_keys();
9595
m_table_size = builder.table_size();
9696
m_partitioner = builder.bucketer();
97-
m_partitions.resize(num_partitions);
97+
std::vector<partition> partitions;
98+
partitions.resize(num_partitions);
9899

99100
auto const& offsets = builder.offsets();
100101
auto const& builders = builder.builders();
@@ -104,8 +105,8 @@ struct partitioned_phf //
104105
std::vector<std::thread> threads(num_threads);
105106
auto exe = [&](uint64_t begin, uint64_t end) {
106107
for (; begin != end; ++begin) {
107-
m_partitions[begin].offset = offsets[begin];
108-
m_partitions[begin].f.build(builders[begin], config);
108+
partitions[begin].offset = offsets[begin];
109+
partitions[begin].f.build(builders[begin], config);
109110
}
110111
};
111112

@@ -123,11 +124,13 @@ struct partitioned_phf //
123124
}
124125
} else {
125126
for (uint64_t i = 0; i != num_partitions; ++i) {
126-
m_partitions[i].offset = offsets[i];
127-
m_partitions[i].f.build(builders[i], config);
127+
partitions[i].offset = offsets[i];
128+
partitions[i].f.build(builders[i], config);
128129
}
129130
}
130131

132+
m_partitions = std::move(partitions);
133+
131134
auto stop = clock_type::now();
132135

133136
return to_microseconds(stop - start);
@@ -147,7 +150,7 @@ struct partitioned_phf //
147150

148151
uint64_t num_bits_for_pilots() const {
149152
uint64_t bits = 8 * (sizeof(m_seed) + sizeof(m_num_keys) + sizeof(m_table_size) +
150-
sizeof(uint64_t) // for std::vector::size
153+
sizeof(uint64_t) // for span's size
151154
) +
152155
m_partitioner.num_bits();
153156
for (auto const& p : m_partitions) bits += 8 * sizeof(p.offset) + p.f.num_bits_for_pilots();
@@ -200,7 +203,7 @@ struct partitioned_phf //
200203
uint64_t m_num_keys;
201204
uint64_t m_table_size;
202205
range_bucketer m_partitioner;
203-
std::vector<partition> m_partitions;
206+
essentials::owning_span<partition> m_partitions;
204207
};
205208

206209
} // namespace pthash

include/utils/dense_encoders.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ struct dense_interleaved : dense_encoder {
6868
const uint64_t num_partitions, //
6969
const uint64_t num_buckets_per_partition, const uint64_t num_threads) //
7070
{
71-
m_encoders.resize(num_buckets_per_partition);
71+
std::vector<Encoder> encoders;
72+
encoders.resize(num_buckets_per_partition);
7273
if (num_threads == 1) {
7374
for (uint64_t i = 0; i != num_buckets_per_partition; ++i) {
74-
m_encoders[i].encode(begin + i * num_partitions, num_partitions);
75+
encoders[i].encode(begin + i * num_partitions, num_partitions);
7576
}
7677
} else {
7778
auto exe = [&](uint64_t beginEncoder, uint64_t endEncoder) {
7879
for (; beginEncoder != endEncoder; ++beginEncoder) {
79-
m_encoders[beginEncoder].encode(begin + beginEncoder * num_partitions,
80-
num_partitions);
80+
encoders[beginEncoder].encode(begin + beginEncoder * num_partitions,
81+
num_partitions);
8182
}
8283
};
8384

@@ -97,6 +98,7 @@ struct dense_interleaved : dense_encoder {
9798
if (t.joinable()) t.join();
9899
}
99100
}
101+
m_encoders = std::move(encoders);
100102
}
101103

102104
static std::string name() {
@@ -109,7 +111,7 @@ struct dense_interleaved : dense_encoder {
109111
}
110112

111113
uint64_t num_bits() const {
112-
uint64_t sum = 8 * sizeof(uint64_t); // for std::vector size
114+
uint64_t sum = 8 * sizeof(uint64_t); // for span' size
113115
for (auto const& e : m_encoders) sum += e.num_bits();
114116
return sum;
115117
}
@@ -130,7 +132,7 @@ struct dense_interleaved : dense_encoder {
130132
visitor.visit(t.m_encoders);
131133
}
132134

133-
std::vector<Encoder> m_encoders;
135+
essentials::owning_span<Encoder> m_encoders;
134136
};
135137

136138
typedef dense_mono<compact> C_mono;

include/utils/encoders.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ struct partitioned_compact {
5858
uint64_t num_partitions = (n + partition_size - 1) / partition_size;
5959
bits::bit_vector::builder bvb;
6060
bvb.reserve(32 * n);
61-
m_bits_per_value.reserve(num_partitions + 1);
62-
m_bits_per_value.push_back(0);
61+
std::vector<uint32_t> bits_per_value;
62+
bits_per_value.reserve(num_partitions + 1);
63+
bits_per_value.push_back(0);
6364
for (uint64_t i = 0, begin_partition = 0; i != num_partitions; ++i) {
6465
uint64_t end_partition = begin_partition + partition_size;
6566
if (end_partition > n) end_partition = n;
@@ -76,10 +77,11 @@ struct partitioned_compact {
7677
for (uint64_t k = begin_partition; k != end_partition; ++k) {
7778
bvb.append_bits(*(begin + k), num_bits);
7879
}
79-
assert(m_bits_per_value.back() + num_bits < (1ULL << 32));
80-
m_bits_per_value.push_back(m_bits_per_value.back() + num_bits);
80+
assert(bits_per_value.back() + num_bits < (1ULL << 32));
81+
bits_per_value.push_back(bits_per_value.back() + num_bits);
8182
begin_partition = end_partition;
8283
}
84+
m_bits_per_value = std::move(bits_per_value);
8385
bvb.build(m_values);
8486
}
8587

@@ -123,7 +125,7 @@ struct partitioned_compact {
123125
}
124126

125127
uint64_t m_size;
126-
std::vector<uint32_t> m_bits_per_value;
128+
essentials::owning_span<uint32_t> m_bits_per_value;
127129
bits::bit_vector m_values;
128130
};
129131

src/example.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int main() {
88

99
/* Generate 1M random 64-bit keys as input data. */
1010
static const uint64_t num_keys = 1'000'000;
11-
static const uint64_t seed = 1234567890;
11+
static const uint64_t seed = essentials::get_random_seed();
1212
std::cout << "generating input data..." << std::endl;
1313
auto keys = distinct_uints<uint64_t>(num_keys, seed);
1414
// Can also use:
@@ -78,8 +78,23 @@ int main() {
7878

7979
{
8080
/* Now reload from disk and query. */
81+
std::cout << "loading the function from disk..." << std::endl;
8182
pthash_type other;
8283
essentials::load(other, output_filename.c_str());
84+
if (check(keys.begin(), other)) std::cout << "EVERYTHING OK!" << std::endl;
85+
const uint64_t n = std::min<uint64_t>(10, keys.size());
86+
for (uint64_t i = 0; i != n; ++i) {
87+
std::cout << "f(" << keys[i] << ") = " << other(keys[i]) << '\n';
88+
assert(f(keys[i]) == other(keys[i]));
89+
}
90+
}
91+
92+
{
93+
/* Now mmap from disk and query. */
94+
std::cout << "mapping the function from disk..." << std::endl;
95+
pthash_type other;
96+
essentials::mmap(other, output_filename.c_str());
97+
if (check(keys.begin(), other)) std::cout << "EVERYTHING OK!" << std::endl;
8398
const uint64_t n = std::min<uint64_t>(10, keys.size());
8499
for (uint64_t i = 0; i != n; ++i) {
85100
std::cout << "f(" << keys[i] << ") = " << other(keys[i]) << '\n';
@@ -89,4 +104,4 @@ int main() {
89104

90105
std::remove(output_filename.c_str());
91106
return 0;
92-
}
107+
}

0 commit comments

Comments
 (0)