-
Notifications
You must be signed in to change notification settings - Fork 0
Bp implementation #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sanya239
wants to merge
11
commits into
main
Choose a base branch
from
bp-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c4fb286
add bp_tree.h
Sanya239 b04f620
fix bp tree
Sanya239 cb355e9
add bp tree tests
Sanya239 0b44660
fix kilo-code warnings
Sanya239 8d3db20
fix suggestions
Sanya239 315c191
add bp to coverage_report.sh
Sanya239 5a7036d
fix clang format
Sanya239 1ccd405
add benchmarks
Sanya239 e78d7ce
huge refactoring
Sanya239 5a19e4e
adapt bp_tree to new rmm specifics
Sanya239 56ca74b
add bp_tree to CMakePresets.json
Sanya239 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| #pragma once | ||
|
|
||
| #include <pixie/rmm_tree.h> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include "utils.h" | ||
|
|
||
| namespace pixie { | ||
|
|
||
| /** | ||
| * @brief A tree class based on the balances parentheses (BP) | ||
| * representation | ||
| */ | ||
| class BPTree { | ||
| private: | ||
| const size_t num_bits_; | ||
| RmMTree rmm; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| public: | ||
| /** | ||
| * @brief A node class of BP tree | ||
| */ | ||
| struct Node { | ||
| size_t number; | ||
| size_t pos; | ||
|
|
||
| /** | ||
| * @brief A node class of BP tree | ||
| */ | ||
| Node(size_t node_number, size_t bp_pos) | ||
| : number(node_number), pos(bp_pos) {} | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Constructor from an external array of uint64_t | ||
| */ | ||
| explicit BPTree(const std::vector<std::uint64_t>& words, size_t tree_size) | ||
| : num_bits_(2 * tree_size), rmm(words, 2 * tree_size) {} | ||
|
|
||
| /** | ||
| * @brief Returns the root node | ||
| */ | ||
| Node root() const { return Node(0, 0); } | ||
|
|
||
| /** | ||
| * @brief Returns the size of the tree | ||
| */ | ||
| size_t size() const { return num_bits_ / 2; } | ||
|
|
||
| /** | ||
| * @brief Indicates if @p node is a leaf | ||
| */ | ||
| bool is_leaf(const Node& node) const { | ||
| return (node.pos + 2 == num_bits_) or rmm.bit(node.pos + 1) == 0; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Indicates if @p node is a root | ||
| */ | ||
| bool is_root(const Node& node) { return node.number == 0; } | ||
|
|
||
| /** | ||
| * @brief Returns the number of children of a @p node | ||
| * this method has O(d) time complexity! | ||
| * | ||
| * TODO try make this faster | ||
| */ | ||
| size_t degree(const Node& node) const { | ||
| if (is_leaf(node)) { | ||
| return 0; | ||
| } | ||
| Node child = first_child(node); | ||
| size_t child_count = 1; | ||
| while (true) { | ||
| if (is_last_child(child)) { | ||
| return child_count; | ||
| } | ||
| child = next_sibling(child); | ||
| child_count++; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns first child of a @p node | ||
| */ | ||
| Node first_child(const Node& node) const { | ||
| size_t pos = node.pos + 1; | ||
| size_t num = node.number + 1; | ||
| return Node(num, pos); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns the i-th child of @p node | ||
| * Indexing starts at 0 | ||
| * this method has O(i) time complexity! | ||
| * | ||
| * TODO try make this faster | ||
| */ | ||
| Node child(const Node& node, size_t i) const { | ||
| Node child = first_child(node); | ||
| while (i--) { | ||
| child = next_sibling(child); | ||
| } | ||
| return child; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns the parent of a @p node if @p node is not root, | ||
| * else returns root | ||
| */ | ||
| Node parent(const Node& node) const { | ||
| if (node.number == 0) { | ||
| return root(); | ||
| } | ||
| size_t pos = rmm.enclose(node.pos); | ||
| size_t num = rmm.rank1(pos); | ||
| return Node(num, pos); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Indicates if @p node is last child | ||
| */ | ||
| bool is_last_child(const Node& node) const { | ||
| size_t end = rmm.close(node.pos); | ||
|
|
||
| return end + 2 >= num_bits_ or rmm.bit(end + 1) == 0; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Returns next sibling of a @p node | ||
| */ | ||
| Node next_sibling(const Node& node) const { | ||
| size_t pos = rmm.close(node.pos) + 1; | ||
| size_t num = rmm.rank1(pos + 1) - 1; | ||
| return Node(num, pos); | ||
| } | ||
| }; | ||
|
|
||
| std::vector<uint64_t> adj_to_bp(size_t tree_size, | ||
| const std::vector<std::vector<size_t>>& adj) { | ||
| size_t bp_size = tree_size * 2; | ||
| std::vector<uint64_t> bp((bp_size + 63) / 64, 0); | ||
| std::vector<std::pair<size_t, size_t>> stack; | ||
| stack.push_back(std::make_pair(0, 0)); | ||
| size_t pos = 0; | ||
| bp[pos >> 6] = bp[pos >> 6] | (1ULL << (pos & 63)); | ||
| while (!stack.empty()) { | ||
| auto& [v, p] = stack.back(); | ||
| p++; | ||
| if (p >= adj[v].size()) { | ||
| pos++; | ||
| stack.pop_back(); | ||
| continue; | ||
| } | ||
| pos++; | ||
| bp[pos >> 6] = bp[pos >> 6] | (1ULL << (pos & 63)); | ||
| stack.push_back(std::make_pair(adj[v][p], 0)); | ||
| } | ||
| return bp; | ||
| } | ||
|
|
||
| bool operator==(const AdjListNode& a, const BPTree::Node& b) { | ||
| return a.number == b.number; | ||
| } | ||
|
|
||
| bool operator==(const BPTree::Node& b, const AdjListNode& a) { | ||
| return a.number == b.number; | ||
| } | ||
|
|
||
| } // namespace pixie | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include <benchmark/benchmark.h> | ||
| #include <pixie/bp_tree.h> | ||
| #include <pixie/utils.h> | ||
|
|
||
| #include <random> | ||
|
|
||
| using Node = pixie::BpTree::Node; | ||
| using pixie::BpTree; | ||
|
|
||
| /** | ||
| * DFS with O(1) extra memory | ||
| */ | ||
| static void BM_BpTreeDFS(benchmark::State& state) { | ||
| size_t tree_size = state.range(0); | ||
| std::mt19937_64 rng(42); | ||
|
|
||
| for (auto _ : state) { | ||
| state.PauseTiming(); | ||
| std::vector<std::vector<size_t>> adj = generate_random_tree(tree_size, rng); | ||
| adj = dfs_order(tree_size, adj); | ||
| std::vector<uint64_t> bp = adj_to_bp(tree_size, adj); | ||
| BpTree tree(bp, tree_size); | ||
|
|
||
| Node cur = tree.root(); | ||
| bool above = 1; | ||
|
|
||
| state.ResumeTiming(); | ||
|
|
||
| benchmark::DoNotOptimize(cur); | ||
|
|
||
| while (true) { | ||
| if (above) { | ||
| if (tree.is_leaf(cur)) { | ||
| above = 0; | ||
| } else { | ||
| cur = tree.first_child(cur); | ||
| } | ||
| benchmark::DoNotOptimize(cur); | ||
| } else { | ||
| if (tree.is_last_child(cur)) { | ||
| cur = tree.parent(cur); | ||
| if (tree.is_root(cur)) { | ||
| break; | ||
| } | ||
| benchmark::DoNotOptimize(cur); | ||
| } else { | ||
| cur = tree.next_sibling(cur); | ||
| above = 1; | ||
| benchmark::DoNotOptimize(cur); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(BM_BpTreeDFS) | ||
| ->ArgNames({"tree_size"}) | ||
| ->RangeMultiplier(2) | ||
| ->Range(1ull << 8, 1ull << 18) | ||
| ->Iterations(100); | ||
|
|
||
| BENCHMARK(BM_BpTreeDFS) | ||
| ->ArgNames({"tree_size"}) | ||
| ->RangeMultiplier(2) | ||
| ->Range(1ull << 18, 1ull << 26) | ||
| ->Iterations(10); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should be added to coverage/build-test workflows (but not for AVX-512).