Skip to content

Commit 621139f

Browse files
committed
make use of batch inserter in test case
1 parent 09ed81c commit 621139f

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

format.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ for folder in examples unittests include/input_reader src/input_reader; do
66
done
77

88
# Format files
9-
for file in include/tests/HashjoinTest.hpp src/tests/hashjoin_test.cpp; do
9+
for file in include/tests/HashjoinTest.hpp src/tests/hashjoin_test.cpp include/hashtables/batch_inserter.hpp; do
1010
clang-format -style=file -i $file
1111
done

include/hashtables/batch_inserter.hpp

+22-13
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
#define HASHTABLES_BATCH_INSERTER_HPP
33

44
#include "base_kht.hpp"
5+
#include "constants.hpp"
56
#include "types.hpp"
67

78
namespace kmercounter {
8-
template <size_t N>
9+
template <size_t N = HT_TESTS_BATCH_LENGTH>
910
class HTBatchInserter {
1011
public:
11-
HTBatchInserter(BaseHashtable* ht) : ht_(ht), buffer_(), buffer_size_(0) {}
12-
~HTBatchInserter() {
13-
flush();
14-
}
12+
HTBatchInserter() : HTBatchInserter(nullptr) {}
13+
HTBatchInserter(BaseHashTable* ht) : ht_(ht), buffer_(), buffer_size_(0) {}
14+
~HTBatchInserter() { flush(); }
1515

1616
// Insert one kv pair.
1717
void insert(const uint64_t key, const uint64_t value) {
@@ -22,34 +22,43 @@ class HTBatchInserter {
2222

2323
// Flush if `buffer_` is full.
2424
if (buffer_size_ >= N) {
25-
flush_();
25+
flush_buffer_();
2626
}
2727
}
2828

29-
// Flush if there's anything in the buffer.
29+
// Flush everything to the hashtable and flush the hashtable insert queue.
3030
void flush() {
3131
if (buffer_size_ > 0) {
32-
flush_();
32+
flush_buffer_();
3333
}
34+
flush_ht_();
3435
}
3536

37+
// Returns the number of elements flushed.
38+
size_t num_flushed() { return num_flushed_; }
39+
3640
private:
37-
// Flush without checking `buffer_size_`.
38-
void flush_() {
39-
InsertFindArguments kp = std::make_pair(buffer_size_, buffer_);
40-
ht->insert_batch(kp);
41+
// Flush the insertion buffer without checking `buffer_size_`.
42+
void flush_buffer_() {
43+
ht_->insert_batch(InsertFindArguments(buffer_, buffer_size_));
44+
num_flushed_ += buffer_size_;
4145
buffer_size_ = 0;
4246
}
4347

48+
// Issue a flush to the hashtable.
49+
void flush_ht_() { ht_->flush_insert_queue(); }
50+
4451
// Target hashtable.
4552
BaseHashTable* ht_;
4653
// Buffer to hold the arguments for batch insertion.
4754
__attribute__((aligned(64))) InsertFindArgument buffer_[N];
4855
// Current size of the buffer.
4956
size_t buffer_size_;
57+
// Total number of elements flushed.
58+
size_t num_flushed_;
5059

5160
// Sanity checks
5261
static_assert(N > 0);
5362
};
54-
} // namespace kmercounter
63+
} // namespace kmercounter
5564
#endif // HASHTABLES_BATCH_INSERTER_HPP

include/types.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct InsertFindArgument {
205205
uint64_t value;
206206
/// A user-provided value for the user to keep track of this operation.
207207
/// This is returned as `FindResult::id`.
208+
/// In aggregation mode, this is the "key". Don't ask why.
208209
uint64_t id;
209210
/// The id of the partition that will be handling this operation.
210211
/// Might not be used depends on the configuration/kind of operation.
@@ -221,6 +222,7 @@ struct FindResult {
221222
/// This matches the `InsertFindArgument::id`.
222223
uint64_t id;
223224
/// The value of the key of the find operation.
225+
/// This is the number of occurrences in aggregation mode.
224226
uint64_t value;
225227

226228
constexpr FindResult() = default;

unittests/hashmap_test.cpp

+22-34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <unordered_map>
1212

1313
#include "hashtable.h"
14+
#include "hashtables/batch_inserter.hpp"
1415
#include "hashtables/cas_kht.hpp"
1516
#include "hashtables/simple_kht.hpp"
1617
#include "test_lib.hpp"
@@ -47,9 +48,11 @@ class HashtableTest : public ::testing::TestWithParam<const char*> {
4748
return nullptr;
4849
}());
4950
ASSERT_NE(ht_, nullptr) << "Invalid hashtable type: " << ht_name;
51+
inserter_ = HTBatchInserter<>(ht_.get());
5052
}
5153

5254
std::unique_ptr<kmercounter::BaseHashTable> ht_;
55+
HTBatchInserter<> inserter_;
5356
};
5457

5558
/// Correctness test for insertion and lookup without prefetch.
@@ -76,19 +79,17 @@ TEST_P(HashtableTest, NO_PREFETCH_TEST) {
7679

7780
TEST_P(HashtableTest, SIMPLE_BATCH_INSERT_TEST) {
7881
// Insertion.
79-
std::array<InsertFindArgument, 2> arguments{
80-
InsertFindArgument{key : 12, value : 128},
81-
InsertFindArgument{key : 23, value : 256}};
82-
InsertFindArguments keypairs(arguments);
83-
ht_->insert_batch(keypairs);
84-
ht_->flush_insert_queue();
82+
inserter_.insert(12, 128);
83+
inserter_.insert(23, 256);
84+
inserter_.flush();
8585

8686
// Look up.
87-
arguments[0] = InsertFindArgument{key : 12, id : 123};
88-
arguments[1] = InsertFindArgument{key : 23, id : 321};
87+
std::array<InsertFindArgument, 2> arguments{
88+
InsertFindArgument{key : 12, id : 123},
89+
InsertFindArgument{key : 23, id : 321}};
8990
std::array<FindResult, HT_TESTS_BATCH_LENGTH> results{};
9091
ValuePairs valuepairs{0, results.data()};
91-
ht_->find_batch(keypairs, valuepairs);
92+
ht_->find_batch(arguments, valuepairs);
9293
ht_->flush_find_queue(valuepairs);
9394

9495
// Check for correctness.
@@ -101,24 +102,22 @@ TEST_P(HashtableTest, SIMPLE_BATCH_INSERT_TEST) {
101102

102103
TEST_P(HashtableTest, SIMPLE_BATCH_UPDATE_TEST) {
103104
// Insertion.
104-
std::array<InsertFindArgument, 2> arguments{
105-
InsertFindArgument{key : 12, value : 128, part_id : 0},
106-
InsertFindArgument{key : 23, value : 256, part_id : 0}};
107-
InsertFindArguments keypairs(arguments);
108-
ht_->insert_batch(InsertFindArguments(arguments));
105+
inserter_.insert(12, 128);
106+
inserter_.insert(23, 256);
107+
inserter_.flush();
109108

110109
// Update.
111-
arguments[0].value = 1025;
112-
arguments[1].value = 4097;
113-
ht_->insert_batch(keypairs);
114-
ht_->flush_insert_queue();
110+
inserter_.insert(12, 1025);
111+
inserter_.insert(23, 4097);
112+
inserter_.flush();
115113

116114
// Look up.
117-
arguments[0].id = 123;
118-
arguments[1].id = 321;
115+
std::array<InsertFindArgument, 2> arguments{
116+
InsertFindArgument{key : 12, id : 123},
117+
InsertFindArgument{key : 23, id : 321}};
119118
std::array<FindResult, HT_TESTS_BATCH_LENGTH> values{};
120119
ValuePairs valuepairs{0, values.data()};
121-
ht_->find_batch(keypairs, valuepairs);
120+
ht_->find_batch(arguments, valuepairs);
122121
ht_->flush_find_queue(valuepairs);
123122

124123
// Check for correctness.
@@ -141,20 +140,10 @@ TEST_P(HashtableTest, BATCH_QUERY_TEST) {
141140
const uint64_t key = i;
142141
const uint64_t value = i * i;
143142
const uint64_t id = 2 * i;
144-
arguments[k].key = key;
145-
arguments[k].value = value;
146-
arguments[k].part_id = 0;
143+
inserter_.insert(key, value);
147144
reference_map[value] = id;
148-
if (++k == HT_TESTS_BATCH_LENGTH) {
149-
ht_->insert_batch(InsertFindArguments(arguments));
150-
k = 0;
151-
}
152-
}
153-
if (k != 0) {
154-
ht_->insert_batch(InsertFindArguments(arguments, k));
155-
k = 0;
156145
}
157-
ht_->flush_insert_queue();
146+
inserter_.flush();
158147

159148
// Helper function for checking the result of the batch finds.
160149
auto check_valuepairs = [&reference_map](const ValuePairs& vp) {
@@ -175,7 +164,6 @@ TEST_P(HashtableTest, BATCH_QUERY_TEST) {
175164
for (uint64_t i = 0; i < test_size; i++) {
176165
arguments[k].key = i;
177166
arguments[k].id = 2 * i;
178-
arguments[k].part_id = 0;
179167
if (++k == HT_TESTS_BATCH_LENGTH) {
180168
ValuePairs valuepairs{0, values};
181169
ht_->find_batch(InsertFindArguments(arguments), valuepairs);

0 commit comments

Comments
 (0)