Skip to content

Commit

Permalink
Account for randomization time in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tinloaf committed Jan 27, 2020
1 parent f15eb7d commit 45329df
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
8 changes: 7 additions & 1 deletion benchmark/bench_dst_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ using InsertZHSDSTInterface =
ygg::TreeFlags::ZTREE_RANK_TYPE<std::uint8_t>,
ygg::TreeFlags::ZTREE_USE_HASH>;
using InsertZHSDSTFixture = DSTFixture<InsertZHSDSTInterface, InsertExperiment,
true, false, false, false>;
true, false, false, false>;
BENCHMARK_DEFINE_F(InsertZHSDSTFixture, BM_DST_Insertion)
(benchmark::State & state)
{
Expand Down Expand Up @@ -128,6 +128,12 @@ BENCHMARK_DEFINE_F(InsertZRDSTFixture, BM_DST_Insertion)
c.start();
this->papi.start();
for (auto & n : this->experiment_nodes) {

// To also measure the overhead of randomly generating the ranks, we have
// to update the ranks here. For hashing-based Zip Trees, the DST does
// this automatically in insert().
n.benchmark_update_inner_ranks();

this->t.insert(n);
}
this->papi.stop();
Expand Down
6 changes: 6 additions & 0 deletions benchmark/bench_dst_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ BENCHMARK_DEFINE_F(MoveZRDSTFixture, BM_DST_Move)(benchmark::State & state)
this->fixed_nodes[i].upper = upper;
this->fixed_nodes[i].lower = lower;
this->fixed_nodes[i].value = value; // TODO don't do this?

// To also measure the overhead of randomly generating the ranks, we have
// to update the ranks here. For hashing-based Zip Trees, the DST does
// this automatically in insert().
this->fixed_nodes[i].benchmark_update_inner_ranks();

this->t.insert(this->fixed_nodes[i]);
}
this->papi.stop();
Expand Down
23 changes: 21 additions & 2 deletions src/dynamic_segment_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,17 @@ struct UseZipTree
struct InnerNodeBaseBuilder
{
template <class InnerNodeCRTP, class KeyT>
using Base =
ZTreeNodeBase<InnerNodeCRTP, Options<InnerNodeCRTP, KeyT>, Tag>;
class Base : public ZTreeNodeBase<InnerNodeCRTP,
Options<InnerNodeCRTP, KeyT>, Tag> {
public:
using MyBase = ZTreeNodeBase<InnerNodeCRTP, Options<InnerNodeCRTP, KeyT>,
Tag>;

// Export to public, so that we can update ranks
void update_rank() noexcept {
this->MyBase::update_rank();
}
};
};

template <class CRTP, class Node, class NodeTraits, class InnerNode,
Expand Down Expand Up @@ -953,6 +962,16 @@ class DynSegTreeNodeBase {
* this DynSegTreeNodeBase
*/
InnerNode end;

/* Methods to be used for benchmarking purposes only! */
template <class Dummy = TreeSelector>
std::enable_if_t<utilities::is_specialization<Dummy, UseZipTree>{}, void>
benchmark_update_inner_ranks() noexcept
{
this->start.update_rank();
this->end.update_rank();
}

/// @endcond
};

Expand Down
28 changes: 27 additions & 1 deletion src/ziptree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,33 @@ void
ZTreeRankGenerator<Node, Options, false, true>::update_rank(
Node & node) noexcept
{
(void)node;
// Re-Randomize!
auto rand_val = std::rand();
node._zt_rank.rank = 0;
while (rand_val == RAND_MAX) {
node._zt_rank.rank = static_cast<decltype(node._zt_rank.rank)>(
(node._zt_rank.rank +
static_cast<decltype(node._zt_rank.rank)>(std::log2(RAND_MAX))));
rand_val = std::rand();
}
node._zt_rank.rank = static_cast<decltype(node._zt_rank.rank)>(
__builtin_ffsl(static_cast<long int>(rand_val)));
}

template <class Node, class Options>
template <class URBG>
void
ZTreeRankGenerator<Node, Options, false, true>::update_rank(Node & node,
URBG && g) noexcept
{
// Re-Randomize!
auto rand_val = g();
node._zt_rank.rank = 0;
while (rand_val == g.max()) {
node._zt_rank.rank += static_cast<size_t>(std::log2(g.max()));
rand_val = g();
}
node._zt_rank.rank = __builtin_ffsl(static_cast<long int>(rand_val));
}

template <class Node, class Options>
Expand Down
8 changes: 7 additions & 1 deletion src/ziptree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace ygg {

// Forward
// Forwards
template <class Node, class Options = DefaultOptions, class Tag = int>
class ZTreeNodeBase;

Expand Down Expand Up @@ -61,6 +61,8 @@ class ZTreeRankGenerator<Node, Options, false, true> {
template <class URBG>
ZTreeRankGenerator(URBG && g);
static void update_rank(Node & node) noexcept;
template <class URBG>
static void update_rank(Node & node, URBG && g) noexcept;
static size_t get_rank(const Node & node) noexcept;

private:
Expand Down Expand Up @@ -111,6 +113,10 @@ class ZTreeNodeBase : public bst::BSTNodeBase<Node, Options, Tag> {
* TreeFlags::ZTREE_RANK_TYPE), you **must** call this method *before* adding
* the node to your tree, but *after* the node's hash has become valid.
*
* If you use true randomness (i.e., do not set TreeFlags::ZTREE_USE_HASH),
* the ranks of nodes will be set in the constructor. However, you can call
* update_rank() to re-randomize the rank of this node.
*
* @warning Inserting a node into the tree in the aforementioned case before
* calling this method leads to undefined behavior.
*/
Expand Down

0 comments on commit 45329df

Please sign in to comment.