Skip to content

Parallel walks #162

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

Merged
merged 44 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c605a2a
init benchmark for parallel walk
orxfun Jun 4, 2025
041840e
impl Send and Sync for Node
orxfun Jun 5, 2025
0b9131c
walk_par and walk_with_par methods are defined
orxfun Jun 6, 2025
1366d5a
introduce NodePtrCon wrapper
orxfun Jun 6, 2025
68d52ff
AncestorsIterPtr implements Send and Sync
orxfun Jun 6, 2025
8a2af08
paths_par is implemented
orxfun Jun 6, 2025
0038ee9
AncestorsIterPtr implements clone
orxfun Jun 6, 2025
46252fc
bench features fixed
orxfun Jun 6, 2025
c5f8212
paths iterator benchmark is introduced
orxfun Jun 6, 2025
1c02d73
paths_par example extended
orxfun Jun 6, 2025
74987bd
paths_with_par is implemented and documented
orxfun Jun 6, 2025
33bd4bf
paths_with_par where bounds relaxed
orxfun Jun 6, 2025
c0c4b95
leaves_par is implemented
orxfun Jun 6, 2025
35836d5
leaves_with_par is implemented and documented
orxfun Jun 6, 2025
5dfacc8
ancestors_par is implemented and documented
orxfun Jun 6, 2025
7bf36a9
fix in documentations
orxfun Jun 6, 2025
ea51bd5
children_par is implemented and documented
orxfun Jun 6, 2025
ce4e184
clean up
orxfun Jun 6, 2025
69867a2
children iterator benchmark
orxfun Jun 6, 2025
f2b7075
walk_iterator benchmark is defined
orxfun Jun 6, 2025
c3a0374
documentation fix
orxfun Jun 6, 2025
cdaa8dd
NodePtrCon is removed, NodePtr is used instead
orxfun Jun 6, 2025
9137464
walks example is created
orxfun Jun 6, 2025
4d25fb9
ancestors made self exclusive
orxfun Jun 6, 2025
66fc6df
upgrade orx-selfref-col dependency
orxfun Jun 6, 2025
6d9aa96
special iterators example is implemented
orxfun Jun 6, 2025
ca11e77
examples linked for walks and special iterators
orxfun Jun 6, 2025
09c5838
custom_walk is implemented
orxfun Jun 6, 2025
79eb88e
lifetime simplifications
orxfun Jun 6, 2025
9959321
CustomWalkIterPtr
orxfun Jun 6, 2025
9e4159c
custom_walk_par is implemented
orxfun Jun 6, 2025
e980865
missing documentation added
orxfun Jun 6, 2025
34e0ad9
clean up
orxfun Jun 6, 2025
2a8e4e3
manual iteration documentation is revised
orxfun Jun 6, 2025
538d4d8
parallelization support section is added
orxfun Jun 6, 2025
ddd67e2
parallelization section is updated
orxfun Jun 6, 2025
0c236ca
clippy fixes
orxfun Jun 6, 2025
8a29eb0
clippy fixes
orxfun Jun 6, 2025
a13abc9
version number incremented
orxfun Jun 6, 2025
1a6f64b
custom_walk_mut is implemented
orxfun Jun 6, 2025
7c0d32e
clippy fix
orxfun Jun 6, 2025
654594c
lifetimes fixed
orxfun Jun 7, 2025
29ca6e9
documentation revised
orxfun Jun 7, 2025
b42beab
definition revised
orxfun Jun 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "orx-tree"
version = "1.6.0"
version = "1.7.0"
edition = "2024"
authors = ["orxfun <[email protected]>"]
description = "A beautiful tree 🌳 with convenient and efficient growth, mutation and traversal features with support for parallel computation."
description = "A beautiful tree 🌳 with convenient, efficient, parallelizable growth, mutation and features."
license = "MIT OR Apache-2.0"
repository = "https://github.com/orxfun/orx-tree/"
keywords = ["tree", "data-structures", "traversal", "traverse", "binarytree"]
Expand All @@ -16,7 +16,7 @@ orx-pinned-vec = "3.16.0"
orx-self-or = "1.2.0"
serde = { version = "1.0.219", optional = true, default-features = false }
orx-split-vec = { version = "3.17.0", default-features = false }
orx-selfref-col = { version = "2.9.0", default-features = false }
orx-selfref-col = { version = "2.10.0", default-features = false }
orx-concurrent-iter = { version = "2.1.0", default-features = false }
orx-parallel = { version = "2.1.0", default-features = false, optional = true }

Expand All @@ -34,5 +34,5 @@ default = ["orx-parallel"]
serde = ["dep:serde"]

[[bench]]
name = "parallelization_ref"
name = "walk_iterator"
harness = false
135 changes: 84 additions & 51 deletions README.md

Large diffs are not rendered by default.

110 changes: 110 additions & 0 deletions benches/children_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
#[cfg(feature = "orx-parallel")]
use orx_parallel::ParIter;
use orx_tree::*;

fn build_tree(n: usize) -> DynTree<String> {
let mut tree = DynTree::new(0.to_string());
let mut dfs = Traversal.dfs().over_nodes();
while tree.len() < n {
let root = tree.root();
let x: Vec<_> = root.leaves_with(&mut dfs).map(|x| x.idx()).collect();
for idx in x.iter() {
let count = tree.len();
let mut node = tree.node_mut(idx);
let num_children = 4;
for j in 0..num_children {
node.push_child((count + j).to_string());
}
}
}
tree
}

fn fibonacci(n: u64) -> u64 {
let mut a = 0;
let mut b = 1;
for _ in 0..n {
let c = a + b;
a = b;
b = c;
}
a
}

fn compute_subtree_value(subtree: &DynNode<String>) -> u64 {
subtree
.walk::<Dfs>()
.map(|x| x.parse::<u64>().unwrap())
.map(|x| fibonacci(x % 1000))
.sum()
}

fn children(tree: &DynTree<String>) -> u64 {
tree.root()
.children()
.map(|x| compute_subtree_value(&x))
.sum()
}

#[cfg(feature = "orx-parallel")]
fn children_par(tree: &DynTree<String>) -> u64 {
tree.root()
.children_par()
.map(|x| compute_subtree_value(&x))
.sum()
}

#[cfg(feature = "orx-parallel")]
fn children_par_2threads(tree: &DynTree<String>) -> u64 {
tree.root()
.children_par()
.num_threads(2)
.map(|x| compute_subtree_value(&x))
.sum()
}

fn bench(c: &mut Criterion) {
let treatments = vec![1_024 * 64];

let mut group = c.benchmark_group("children_iterator");

for n in &treatments {
let tree = build_tree(*n);

let expected = children(&tree);

group.bench_with_input(BenchmarkId::new("NodeRef::children()", n), n, |b, _| {
let result = children(&tree);
assert_eq!(result, expected);
b.iter(|| children(&tree))
});

#[cfg(feature = "orx-parallel")]
group.bench_with_input(
BenchmarkId::new("NNodeRef::children_par()", n),
n,
|b, _| {
let result = children_par(&tree);
assert_eq!(result, expected);
b.iter(|| children_par(&tree))
},
);

#[cfg(feature = "orx-parallel")]
group.bench_with_input(
BenchmarkId::new("NodeRef::children_par().num_threads(2)", n),
n,
|b, _| {
let result = children_par_2threads(&tree);
assert_eq!(result, expected);
b.iter(|| children_par_2threads(&tree))
},
);
}

group.finish();
}

criterion_group!(benches, bench);
criterion_main!(benches);
2 changes: 2 additions & 0 deletions benches/parallelization_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn tree_into_bfs(mut tree: DynTree<String>) -> i64 {
.sum()
}

#[cfg(feature = "orx-parallel")]
fn tree_into_par_x(tree: DynTree<String>) -> i64 {
tree.into_par()
.map(|x| x.parse::<usize>().unwrap())
Expand Down Expand Up @@ -107,6 +108,7 @@ fn bench(c: &mut Criterion) {
},
);

#[cfg(feature = "orx-parallel")]
group.bench_with_input(
BenchmarkId::new("Tree::into_par_x() - orx-parallel", n),
n,
Expand Down
2 changes: 2 additions & 0 deletions benches/parallelization_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn tree_bfs(tree: &DynTree<String>) -> i64 {
.sum()
}

#[cfg(feature = "orx-parallel")]
fn tree_par_x(tree: &DynTree<String>) -> i64 {
tree.par()
.map(|x| x.parse::<usize>().unwrap())
Expand Down Expand Up @@ -107,6 +108,7 @@ fn bench(c: &mut Criterion) {
},
);

#[cfg(feature = "orx-parallel")]
group.bench_with_input(
BenchmarkId::new("Tree::par_x() - orx-parallel", n),
n,
Expand Down
98 changes: 98 additions & 0 deletions benches/paths_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use orx_iterable::{IntoCloningIterable, Iterable};
#[cfg(feature = "orx-parallel")]
use orx_parallel::ParIter;
use orx_tree::*;

fn build_tree(n: usize) -> DynTree<String> {
let mut tree = DynTree::new(0.to_string());
let mut dfs = Traversal.dfs().over_nodes();
while tree.len() < n {
let root = tree.root();
let x: Vec<_> = root.leaves_with(&mut dfs).map(|x| x.idx()).collect();
for idx in x.iter() {
let count = tree.len();
let mut node = tree.node_mut(idx);
let num_children = 4;
for j in 0..num_children {
node.push_child((count + j).to_string());
}
}
}
tree
}

fn fibonacci(n: i64) -> i64 {
let mut a = 0;
let mut b = 1;
for _ in 0..n {
let c = a + b;
a = b;
b = c;
}
a
}

fn path_value<'a>(path: impl IntoIterator<Item = &'a String>) -> i64 {
path.into_iter()
.map(|x| x.parse::<i64>().unwrap())
.map(|x| x % 500)
.map(fibonacci)
.max()
.unwrap()
}

fn paths<T: Traverser>(tree: &DynTree<String>) -> Vec<String> {
let root = tree.root();
root.paths::<T>()
.map(|x| x.into_iterable())
.max_by_key(|x| path_value(x.iter()))
.map(|x| x.iter().cloned().collect())
.unwrap()
}

#[cfg(feature = "orx-parallel")]
fn paths_par<T: Traverser>(tree: &DynTree<String>) -> Vec<String> {
let root = tree.root();
root.paths_par::<T>()
.map(|x| x.into_iterable())
.max_by_key(|x| path_value(x.iter()))
.map(|x| x.iter().cloned().collect())
.unwrap()
}

type TRAVERSER = Dfs;

fn bench(c: &mut Criterion) {
let treatments = vec![1_024 * 64];

let mut group = c.benchmark_group("paths_iterator");

for n in &treatments {
let data = build_tree(*n);

let expected = paths::<TRAVERSER>(&data);

group.bench_with_input(BenchmarkId::new("NodeRef::paths::<T>()", n), n, |b, _| {
let result = paths::<TRAVERSER>(&data);
assert_eq!(path_value(&result), path_value(&expected));
b.iter(|| paths::<TRAVERSER>(&data))
});

#[cfg(feature = "orx-parallel")]
group.bench_with_input(
BenchmarkId::new("NodeRef::paths_par::<T>()", n),
n,
|b, _| {
let result = paths_par::<TRAVERSER>(&data);
assert_eq!(path_value(&result), path_value(&expected));
b.iter(|| paths_par::<TRAVERSER>(&data))
},
);
}

group.finish();
}

criterion_group!(benches, bench);
criterion_main!(benches);
Loading