Mon, 06 Jan 2025 20:29:25 -0500
More Serialize / Deserialize / Debug derives
| 5 | 1 | |
| 2 | /*! | |
| 3 | Bisection tree basics, [`BT`] type and the [`BTImpl`] trait. | |
| 4 | */ | |
| 0 | 5 | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
6 | use std::slice::IterMut; |
| 0 | 7 | use std::iter::once; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
8 | use std::sync::Arc; |
| 0 | 9 | use serde::{Serialize, Deserialize}; |
| 5 | 10 | pub(super) use nalgebra::Const; |
| 0 | 11 | use itertools::izip; |
| 12 | ||
| 13 | use crate::types::{Float, Num}; | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
14 | use crate::parallelism::{with_task_budget, TaskBudget}; |
| 0 | 15 | use crate::coefficients::pow; |
| 16 | use crate::maputil::{ | |
| 17 | array_init, | |
| 18 | map2, | |
| 19 | map2_indexed, | |
| 20 | collect_into_array_unchecked | |
| 21 | }; | |
| 5 | 22 | use crate::sets::Cube; |
| 23 | use crate::loc::Loc; | |
| 0 | 24 | use super::support::*; |
| 25 | use super::aggregator::*; | |
| 26 | ||
| 5 | 27 | /// An enum that indicates whether a [`Node`] of a [`BT`] is uninitialised, leaf, or branch. |
| 28 | /// | |
| 29 | /// For the type and const parametere, see the [module level documentation][super]. | |
| 0 | 30 | #[derive(Clone,Debug)] |
| 5 | 31 | pub(super) enum NodeOption<F : Num, D, A : Aggregator, const N : usize, const P : usize> { |
| 32 | /// Indicates an uninitilised node; may become a branch or a leaf. | |
| 0 | 33 | // TODO: Could optimise Uninitialised away by simply treat Leaf with an empty Vec as |
| 34 | // something that can be still replaced with Branches. | |
| 35 | Uninitialised, | |
| 5 | 36 | /// Indicates a leaf node containing a copy-on-write reference-counted vector |
| 37 | /// of data of type `D`. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
38 | Leaf(Vec<D>), |
| 5 | 39 | /// Indicates a branch node, cotaning a copy-on-write reference to the [`Branches`]. |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
40 | Branches(Arc<Branches<F, D, A, N, P>>), |
| 0 | 41 | } |
| 42 | ||
| 43 | /// Node of a [`BT`] bisection tree. | |
| 5 | 44 | /// |
| 45 | /// For the type and const parameteres, see the [module level documentation][super]. | |
|
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
46 | #[derive(Clone, Debug, Serialize, Deserialize)] |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
47 | #[serde(bound( |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
48 | serialize = "NodeOption<F, D, A, N, P> : Serialize, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
49 | A : Serialize,", |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
50 | deserialize = "NodeOption<F, D, A, N, P> : for<'a> Deserialize<'a>, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
51 | A : for<'a> Deserialize<'a>," |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
52 | ))] |
| 0 | 53 | pub struct Node<F : Num, D, A : Aggregator, const N : usize, const P : usize> { |
| 5 | 54 | /// The data or branches under the node. |
| 0 | 55 | pub(super) data : NodeOption<F, D, A, N, P>, |
| 56 | /// Aggregator for `data`. | |
| 57 | pub(super) aggregator : A, | |
| 58 | } | |
| 59 | ||
| 5 | 60 | /// Branching information of a [`Node`] of a [`BT`] bisection tree into `P` subnodes. |
| 61 | /// | |
| 62 | /// For the type and const parameters, see the [module level documentation][super]. | |
|
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
63 | #[derive(Clone, Debug, Serialize, Deserialize)] |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
64 | #[serde(bound( |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
65 | serialize = "[Node<F, D, A, N, P>; P] : Serialize, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
66 | Loc<F, N> : Serialize,", |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
67 | deserialize = "[Node<F, D, A, N, P>; P] : for<'a> Deserialize<'a>, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
68 | Loc<F, N> : for<'a> Deserialize<'a>," |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
69 | ))] |
| 5 | 70 | pub(super) struct Branches<F : Num, D, A : Aggregator, const N : usize, const P : usize> { |
| 0 | 71 | /// Point for subdivision of the (unstored) [`Cube`] corresponding to the node. |
| 72 | pub(super) branch_at : Loc<F, N>, | |
| 73 | /// Subnodes | |
| 74 | pub(super) nodes : [Node<F, D, A, N, P>; P], | |
| 75 | } | |
| 76 | ||
| 77 | /// Dirty workaround to broken Rust drop, see [https://github.com/rust-lang/rust/issues/58068](). | |
| 78 | impl<F : Num, D, A : Aggregator, const N : usize, const P : usize> | |
| 79 | Drop for Node<F, D, A, N, P> { | |
| 80 | fn drop(&mut self) { | |
| 81 | use NodeOption as NO; | |
| 82 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
83 | let process = |brc : Arc<Branches<F, D, A, N, P>>, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
84 | to_drop : &mut Vec<Arc<Branches<F, D, A, N, P>>>| { |
| 0 | 85 | // We only drop Branches if we have the only strong reference. |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
86 | // FIXME: update the RwLocks on Nodes. |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
87 | Arc::try_unwrap(brc).ok().map(|branches| branches.nodes.map(|mut node| { |
| 0 | 88 | if let NO::Branches(brc2) = std::mem::replace(&mut node.data, NO::Uninitialised) { |
| 89 | to_drop.push(brc2) | |
| 90 | } | |
| 91 | })); | |
| 92 | }; | |
| 93 | ||
| 94 | // We mark Self as NodeOption::Uninitialised, extracting the real contents. | |
| 95 | // If we have subprocess, we need to process them. | |
| 96 | if let NO::Branches(brc1) = std::mem::replace(&mut self.data, NO::Uninitialised) { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
97 | // We store a queue of Arc<Branches> to drop into a vector |
| 0 | 98 | let mut to_drop = Vec::new(); |
| 99 | process(brc1, &mut to_drop); | |
| 100 | ||
| 101 | // While there are any Branches in the drop queue vector, we continue the process, | |
| 102 | // pushing all internal branching nodes into the queue. | |
| 103 | while let Some(brc) = to_drop.pop() { | |
| 104 | process(brc, &mut to_drop) | |
| 105 | } | |
| 106 | } | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 5 | 110 | /// Trait for the depth of a [`BT`]. |
| 111 | /// | |
| 112 | /// This will generally be either a runtime [`DynamicDepth`] or compile-time [`Const`] depth. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
113 | pub trait Depth : 'static + Copy + Send + Sync + std::fmt::Debug { |
| 5 | 114 | /// Lower depth type. |
| 0 | 115 | type Lower : Depth; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
116 | |
| 5 | 117 | /// Returns a lower depth, if there still is one. |
| 0 | 118 | fn lower(&self) -> Option<Self::Lower>; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
119 | |
| 5 | 120 | /// Returns a lower depth or self if this is the lowest depth. |
| 0 | 121 | fn lower_or(&self) -> Self::Lower; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
122 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
123 | /// Returns the numeric value of the depth |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
124 | fn value(&self) -> u32; |
| 0 | 125 | } |
| 126 | ||
| 5 | 127 | /// Dynamic (runtime) [`Depth`] for a [`BT`]. |
| 0 | 128 | #[derive(Copy,Clone,Debug,Serialize,Deserialize)] |
| 5 | 129 | pub struct DynamicDepth( |
| 130 | /// The depth | |
| 131 | pub u8 | |
| 132 | ); | |
| 133 | ||
| 0 | 134 | impl Depth for DynamicDepth { |
| 135 | type Lower = Self; | |
| 136 | #[inline] | |
| 137 | fn lower(&self) -> Option<Self> { | |
| 138 | if self.0>0 { | |
| 139 | Some(DynamicDepth(self.0-1)) | |
| 140 | } else { | |
| 141 | None | |
| 142 | } | |
| 143 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
144 | |
| 0 | 145 | #[inline] |
| 146 | fn lower_or(&self) -> Self { | |
| 147 | DynamicDepth(if self.0>0 { self.0 - 1 } else { 0 }) | |
| 148 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
149 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
150 | #[inline] |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
151 | fn value(&self) -> u32 { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
152 | self.0 as u32 |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
153 | } |
| 0 | 154 | } |
| 155 | ||
| 156 | impl Depth for Const<0> { | |
| 157 | type Lower = Self; | |
| 158 | fn lower(&self) -> Option<Self::Lower> { None } | |
| 159 | fn lower_or(&self) -> Self::Lower { Const } | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
160 | fn value(&self) -> u32 { 0 } |
| 0 | 161 | } |
| 162 | ||
| 163 | macro_rules! impl_constdepth { | |
| 164 | ($($n:literal)*) => { $( | |
| 165 | impl Depth for Const<$n> { | |
| 166 | type Lower = Const<{$n-1}>; | |
| 167 | fn lower(&self) -> Option<Self::Lower> { Some(Const) } | |
| 168 | fn lower_or(&self) -> Self::Lower { Const } | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
169 | fn value(&self) -> u32 { $n } |
| 0 | 170 | } |
| 171 | )* }; | |
| 172 | } | |
| 173 | impl_constdepth!(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32); | |
| 174 | ||
| 5 | 175 | /// Trait for counting the branching factor of a [`BT`] of dimension `N`. |
| 176 | /// | |
| 177 | /// The const parameter `P` from the [module level documentation][super] is required to satisfy | |
| 178 | /// `Const<P> : Branchcount<N>`. | |
| 179 | /// This trait is implemented for `P=pow(2, N)` for small `N`. | |
| 0 | 180 | pub trait BranchCount<const N : usize> {} |
| 181 | macro_rules! impl_branchcount { | |
| 182 | ($($n:literal)*) => { $( | |
| 183 | impl BranchCount<$n> for Const<{pow(2, $n)}>{} | |
| 184 | )* } | |
| 185 | } | |
| 186 | impl_branchcount!(1 2 3 4 5 6 7 8); | |
| 187 | ||
| 188 | impl<F : Float, D, A, const N : usize, const P : usize> Branches<F,D,A,N,P> | |
| 189 | where Const<P> : BranchCount<N>, | |
| 190 | A : Aggregator | |
| 191 | { | |
| 5 | 192 | /// Returns the index in {0, …, `P`-1} for the branch to which the point `x` corresponds. |
| 193 | /// | |
| 194 | /// This only takes the branch subdivision point $d$ into account, so is always succesfull. | |
| 195 | /// Thus, for this point, each branch corresponds to a quadrant of $ℝ^N$ relative to $d$. | |
| 0 | 196 | fn get_node_index(&self, x : &Loc<F, N>) -> usize { |
| 197 | izip!(0..P, x.iter(), self.branch_at.iter()).map(|(i, x_i, branch_i)| | |
| 198 | if x_i > branch_i { 1<<i } else { 0 } | |
| 199 | ).sum() | |
| 200 | } | |
| 201 | ||
| 5 | 202 | /// Returns the node within `Self` containing the point `x`. |
| 203 | /// | |
| 204 | /// This only takes the branch subdivision point $d$ into account, so is always succesfull. | |
| 205 | /// Thus, for this point, each branch corresponds to a quadrant of $ℝ^N$ relative to $d$. | |
| 0 | 206 | #[inline] |
| 5 | 207 | fn get_node(&self, x : &Loc<F,N>) -> &Node<F,D,A,N,P> { |
| 0 | 208 | &self.nodes[self.get_node_index(x)] |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 5 | 212 | /// An iterator over the $P=2^N$ subcubes of a [`Cube`] subdivided at a point `d`. |
|
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
213 | #[derive(Debug, Clone)] |
| 5 | 214 | pub(super) struct SubcubeIter<'b, F : Float, const N : usize, const P : usize> { |
| 0 | 215 | domain : &'b Cube<F, N>, |
| 216 | branch_at : Loc<F, N>, | |
| 217 | index : usize, | |
| 218 | } | |
| 219 | ||
| 5 | 220 | /// Returns the `i`:th subcube of `domain` subdivided at `branch_at`. |
| 0 | 221 | #[inline] |
| 5 | 222 | fn get_subcube<F : Float, const N : usize>( |
| 223 | branch_at : &Loc<F, N>, | |
| 224 | domain : &Cube<F, N>, | |
| 225 | i : usize | |
| 226 | ) -> Cube<F, N> { | |
| 0 | 227 | map2_indexed(branch_at, domain, move |j, &branch, &[start, end]| { |
| 228 | if i & (1 << j) != 0 { | |
| 229 | [branch, end] | |
| 230 | } else { | |
| 231 | [start, branch] | |
| 232 | } | |
| 233 | }).into() | |
| 234 | } | |
| 235 | ||
| 236 | impl<'a, 'b, F : Float, const N : usize, const P : usize> Iterator | |
| 237 | for SubcubeIter<'b, F, N, P> { | |
| 238 | type Item = Cube<F, N>; | |
| 239 | #[inline] | |
| 240 | fn next(&mut self) -> Option<Self::Item> { | |
| 241 | if self.index < P { | |
| 242 | let i = self.index; | |
| 243 | self.index += 1; | |
| 244 | Some(get_subcube(&self.branch_at, self.domain, i)) | |
| 245 | } else { | |
| 246 | None | |
| 247 | } | |
| 248 | } | |
| 249 | } | |
| 250 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
251 | impl<F : Float, D, A, const N : usize, const P : usize> |
| 0 | 252 | Branches<F,D,A,N,P> |
| 253 | where Const<P> : BranchCount<N>, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
254 | A : Aggregator, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
255 | D : 'static + Copy + Send + Sync { |
| 0 | 256 | |
| 5 | 257 | /// Creates a new node branching structure, subdividing `domain` based on the |
| 258 | /// [hint][Support::support_hint] of `support`. | |
| 259 | pub(super) fn new_with<S : LocalAnalysis <F, A, N>>( | |
| 0 | 260 | domain : &Cube<F,N>, |
| 261 | support : &S | |
| 262 | ) -> Self { | |
| 263 | let hint = support.bisection_hint(domain); | |
| 264 | let branch_at = map2(&hint, domain, |h, r| { | |
| 265 | h.unwrap_or_else(|| (r[0]+r[1])/F::TWO).max(r[0]).min(r[1]) | |
| 266 | }).into(); | |
| 267 | Branches{ | |
| 268 | branch_at : branch_at, | |
| 269 | nodes : array_init(|| Node::new()), | |
| 270 | } | |
| 271 | } | |
| 272 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
273 | /// Summarises the aggregators of these branches into `agg` |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
274 | pub(super) fn summarise_into(&self, agg : &mut A) { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
275 | // We need to create an array of the aggregators clones due to the RwLock. |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
276 | agg.summarise(self.nodes.iter().map(Node::get_aggregator)); |
| 0 | 277 | } |
| 278 | ||
| 5 | 279 | /// Returns an iterator over the subcubes of `domain` subdivided at the branching point |
| 280 | /// of `self`. | |
| 0 | 281 | #[inline] |
| 5 | 282 | pub(super) fn iter_subcubes<'b>(&self, domain : &'b Cube<F, N>) |
| 0 | 283 | -> SubcubeIter<'b, F, N, P> { |
| 284 | SubcubeIter { | |
| 285 | domain : domain, | |
| 286 | branch_at : self.branch_at, | |
| 287 | index : 0, | |
| 288 | } | |
| 289 | } | |
| 290 | ||
| 5 | 291 | /* |
| 292 | /// Returns an iterator over all nodes and corresponding subcubes of `self`. | |
| 0 | 293 | #[inline] |
| 5 | 294 | pub(super) fn nodes_and_cubes<'a, 'b>(&'a self, domain : &'b Cube<F, N>) |
| 0 | 295 | -> std::iter::Zip<Iter<'a, Node<F,D,A,N,P>>, SubcubeIter<'b, F, N, P>> { |
| 296 | self.nodes.iter().zip(self.iter_subcubes(domain)) | |
| 297 | } | |
| 5 | 298 | */ |
| 0 | 299 | |
| 5 | 300 | /// Mutably iterate over all nodes and corresponding subcubes of `self`. |
| 0 | 301 | #[inline] |
| 5 | 302 | pub(super) fn nodes_and_cubes_mut<'a, 'b>(&'a mut self, domain : &'b Cube<F, N>) |
| 0 | 303 | -> std::iter::Zip<IterMut<'a, Node<F,D,A,N,P>>, SubcubeIter<'b, F, N, P>> { |
| 304 | let subcube_iter = self.iter_subcubes(domain); | |
| 305 | self.nodes.iter_mut().zip(subcube_iter) | |
| 306 | } | |
| 307 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
308 | /// Call `f` on all `(subnode, subcube)` pairs in multiple threads, if `guard` so deems. |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
309 | #[inline] |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
310 | fn recurse<'scope, 'smaller, 'refs>( |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
311 | &'smaller mut self, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
312 | domain : &'smaller Cube<F, N>, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
313 | task_budget : TaskBudget<'scope, 'refs>, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
314 | guard : impl Fn(&Node<F,D,A,N,P>, &Cube<F, N>) -> bool + Send + 'smaller, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
315 | mut f : impl for<'a> FnMut(&mut Node<F,D,A,N,P>, &Cube<F, N>, TaskBudget<'smaller, 'a>) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
316 | + Send + Copy + 'smaller |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
317 | ) where 'scope : 'smaller { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
318 | let subs = self.nodes_and_cubes_mut(domain); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
319 | task_budget.zoom(move |s| { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
320 | for (node, subcube) in subs { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
321 | if guard(node, &subcube) { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
322 | s.execute(move |new_budget| f(node, &subcube, new_budget)) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
323 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
324 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
325 | }); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
326 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
327 | |
| 0 | 328 | /// Insert data into the branch. |
| 5 | 329 | /// |
| 330 | /// The parameters are as follows: | |
| 331 | /// * `domain` is the cube corresponding to this branch. | |
| 332 | /// * `d` is the data to be inserted | |
| 333 | /// * `new_leaf_depth` is the depth relative to `self` at which the data is to be inserted. | |
| 334 | /// * `support` is the [`Support`] that is used determine with which subcubes of `domain` | |
| 335 | /// (at subdivision depth `new_leaf_depth`) the data `d` is to be associated with. | |
| 336 | /// | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
337 | pub(super) fn insert<'refs, 'scope, M : Depth, S : LocalAnalysis<F, A, N>>( |
| 0 | 338 | &mut self, |
| 339 | domain : &Cube<F,N>, | |
| 340 | d : D, | |
| 341 | new_leaf_depth : M, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
342 | support : &S, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
343 | task_budget : TaskBudget<'scope, 'refs>, |
| 0 | 344 | ) { |
| 345 | let support_hint = support.support_hint(); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
346 | self.recurse(domain, task_budget, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
347 | |_, subcube| support_hint.intersects(&subcube), |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
348 | move |node, subcube, new_budget| node.insert(subcube, d, new_leaf_depth, support, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
349 | new_budget)); |
| 0 | 350 | } |
| 351 | ||
| 5 | 352 | /// Construct a new instance of the branch for a different aggregator. |
| 353 | /// | |
| 354 | /// The `generator` is used to convert the data of type `D` of the branch into corresponding | |
| 355 | /// [`Support`]s. The `domain` is the cube corresponding to `self`. | |
| 356 | /// The type parameter `ANew´ is the new aggregator, and needs to be implemented for the | |
| 357 | /// generator's `SupportType`. | |
| 358 | pub(super) fn convert_aggregator<ANew, G>( | |
| 0 | 359 | self, |
| 360 | generator : &G, | |
| 361 | domain : &Cube<F, N> | |
| 362 | ) -> Branches<F,D,ANew,N,P> | |
| 363 | where ANew : Aggregator, | |
| 364 | G : SupportGenerator<F, N, Id=D>, | |
| 365 | G::SupportType : LocalAnalysis<F, ANew, N> { | |
| 366 | let branch_at = self.branch_at; | |
| 367 | let subcube_iter = self.iter_subcubes(domain); | |
| 368 | let new_nodes = self.nodes.into_iter().zip(subcube_iter).map(|(node, subcube)| { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
369 | Node::convert_aggregator(node, generator, &subcube) |
| 0 | 370 | }); |
| 371 | Branches { | |
| 372 | branch_at : branch_at, | |
| 373 | nodes : collect_into_array_unchecked(new_nodes), | |
| 374 | } | |
| 375 | } | |
| 376 | ||
| 5 | 377 | /// Recalculate aggregator after changes to generator. |
| 378 | /// | |
| 379 | /// The `generator` is used to convert the data of type `D` of the branch into corresponding | |
| 380 | /// [`Support`]s. The `domain` is the cube corresponding to `self`. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
381 | pub(super) fn refresh_aggregator<'refs, 'scope, G>( |
| 0 | 382 | &mut self, |
| 383 | generator : &G, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
384 | domain : &Cube<F, N>, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
385 | task_budget : TaskBudget<'scope, 'refs>, |
| 0 | 386 | ) where G : SupportGenerator<F, N, Id=D>, |
| 387 | G::SupportType : LocalAnalysis<F, A, N> { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
388 | self.recurse(domain, task_budget, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
389 | |_, _| true, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
390 | move |node, subcube, new_budget| node.refresh_aggregator(generator, subcube, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
391 | new_budget)); |
| 0 | 392 | } |
| 393 | } | |
| 394 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
395 | impl<F : Float, D, A, const N : usize, const P : usize> |
| 0 | 396 | Node<F,D,A,N,P> |
| 397 | where Const<P> : BranchCount<N>, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
398 | A : Aggregator, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
399 | D : 'static + Copy + Send + Sync { |
| 0 | 400 | |
| 5 | 401 | /// Create a new node |
| 0 | 402 | #[inline] |
| 5 | 403 | pub(super) fn new() -> Self { |
| 0 | 404 | Node { |
| 405 | data : NodeOption::Uninitialised, | |
| 406 | aggregator : A::new(), | |
| 407 | } | |
| 408 | } | |
| 409 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
410 | /* |
| 0 | 411 | /// Get leaf data |
| 412 | #[inline] | |
| 5 | 413 | pub(super) fn get_leaf_data(&self, x : &Loc<F, N>) -> Option<&Vec<D>> { |
| 0 | 414 | match self.data { |
| 415 | NodeOption::Uninitialised => None, | |
| 416 | NodeOption::Leaf(ref data) => Some(data), | |
| 417 | NodeOption::Branches(ref b) => b.get_node(x).get_leaf_data(x), | |
| 418 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
419 | }*/ |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
420 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
421 | /// Get leaf data iterator |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
422 | #[inline] |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
423 | pub(super) fn get_leaf_data_iter(&self, x : &Loc<F, N>) -> Option<std::slice::Iter<'_, D>> { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
424 | match self.data { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
425 | NodeOption::Uninitialised => None, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
426 | NodeOption::Leaf(ref data) => Some(data.iter()), |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
427 | NodeOption::Branches(ref b) => b.get_node(x).get_leaf_data_iter(x), |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
428 | } |
| 0 | 429 | } |
| 430 | ||
| 431 | /// Returns a reference to the aggregator of this node | |
| 432 | #[inline] | |
| 5 | 433 | pub(super) fn get_aggregator(&self) -> &A { |
| 0 | 434 | &self.aggregator |
| 435 | } | |
| 436 | ||
| 5 | 437 | /// Insert data under the node. |
| 438 | /// | |
| 439 | /// The parameters are as follows: | |
| 440 | /// * `domain` is the cube corresponding to this branch. | |
| 441 | /// * `d` is the data to be inserted | |
| 442 | /// * `new_leaf_depth` is the depth relative to `self` at which new leaves are created. | |
| 443 | /// * `support` is the [`Support`] that is used determine with which subcubes of `domain` | |
| 444 | /// (at subdivision depth `new_leaf_depth`) the data `d` is to be associated with. | |
| 445 | /// | |
| 446 | /// If `self` is already [`NodeOption::Leaf`], the data is inserted directly in this node. | |
| 447 | /// If `self` is a [`NodeOption::Branches`], the data is passed to branches whose subcubes | |
| 448 | /// `support` intersects. If an [`NodeOption::Uninitialised`] node is encountered, a new leaf is | |
| 449 | /// created at a minimum depth of `new_leaf_depth`. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
450 | pub(super) fn insert<'refs, 'scope, M : Depth, S : LocalAnalysis <F, A, N>>( |
| 0 | 451 | &mut self, |
| 452 | domain : &Cube<F,N>, | |
| 453 | d : D, | |
| 454 | new_leaf_depth : M, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
455 | support : &S, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
456 | task_budget : TaskBudget<'scope, 'refs>, |
| 0 | 457 | ) { |
| 458 | match &mut self.data { | |
| 459 | NodeOption::Uninitialised => { | |
| 460 | // Replace uninitialised node with a leaf or a branch | |
| 461 | self.data = match new_leaf_depth.lower() { | |
| 462 | None => { | |
| 463 | let a = support.local_analysis(&domain); | |
| 464 | self.aggregator.aggregate(once(a)); | |
| 465 | // TODO: this is currently a dirty hard-coded heuristic; | |
| 466 | // should add capacity as a parameter | |
| 467 | let mut vec = Vec::with_capacity(2*P+1); | |
| 468 | vec.push(d); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
469 | NodeOption::Leaf(vec) |
| 0 | 470 | }, |
| 471 | Some(lower) => { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
472 | let b = Arc::new({ |
| 0 | 473 | let mut b0 = Branches::new_with(domain, support); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
474 | b0.insert(domain, d, lower, support, task_budget); |
| 0 | 475 | b0 |
| 476 | }); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
477 | b.summarise_into(&mut self.aggregator); |
| 0 | 478 | NodeOption::Branches(b) |
| 479 | } | |
| 480 | } | |
| 481 | }, | |
| 482 | NodeOption::Leaf(leaf) => { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
483 | leaf.push(d); |
| 0 | 484 | let a = support.local_analysis(&domain); |
| 485 | self.aggregator.aggregate(once(a)); | |
| 486 | }, | |
| 487 | NodeOption::Branches(b) => { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
488 | // FIXME: recursion that may cause stack overflow if the tree becomes |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
489 | // very deep, e.g. due to [`BTSearch::search_and_refine`]. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
490 | let bm = Arc::make_mut(b); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
491 | bm.insert(domain, d, new_leaf_depth.lower_or(), support, task_budget); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
492 | bm.summarise_into(&mut self.aggregator); |
| 0 | 493 | }, |
| 494 | } | |
| 495 | } | |
| 496 | ||
| 5 | 497 | /// Construct a new instance of the node for a different aggregator |
| 498 | /// | |
| 499 | /// The `generator` is used to convert the data of type `D` of the node into corresponding | |
| 500 | /// [`Support`]s. The `domain` is the cube corresponding to `self`. | |
| 501 | /// The type parameter `ANew´ is the new aggregator, and needs to be implemented for the | |
| 502 | /// generator's `SupportType`. | |
| 503 | pub(super) fn convert_aggregator<ANew, G>( | |
| 0 | 504 | mut self, |
| 505 | generator : &G, | |
| 506 | domain : &Cube<F, N> | |
| 507 | ) -> Node<F,D,ANew,N,P> | |
| 508 | where ANew : Aggregator, | |
| 509 | G : SupportGenerator<F, N, Id=D>, | |
| 510 | G::SupportType : LocalAnalysis<F, ANew, N> { | |
| 511 | ||
| 512 | // The mem::replace is needed due to the [`Drop`] implementation to extract self.data. | |
| 513 | match std::mem::replace(&mut self.data, NodeOption::Uninitialised) { | |
| 514 | NodeOption::Uninitialised => Node { | |
| 515 | data : NodeOption::Uninitialised, | |
| 516 | aggregator : ANew::new(), | |
| 517 | }, | |
| 518 | NodeOption::Leaf(v) => { | |
| 519 | let mut anew = ANew::new(); | |
| 520 | anew.aggregate(v.iter().map(|d| { | |
| 521 | let support = generator.support_for(*d); | |
| 522 | support.local_analysis(&domain) | |
| 523 | })); | |
| 524 | ||
| 525 | Node { | |
| 526 | data : NodeOption::Leaf(v), | |
| 527 | aggregator : anew, | |
| 528 | } | |
| 529 | }, | |
| 530 | NodeOption::Branches(b) => { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
531 | // FIXME: recursion that may cause stack overflow if the tree becomes |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
532 | // very deep, e.g. due to [`BTSearch::search_and_refine`]. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
533 | let bnew = Arc::unwrap_or_clone(b).convert_aggregator(generator, domain); |
| 0 | 534 | let mut anew = ANew::new(); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
535 | bnew.summarise_into(&mut anew); |
| 0 | 536 | Node { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
537 | data : NodeOption::Branches(Arc::new(bnew)), |
| 0 | 538 | aggregator : anew, |
| 539 | } | |
| 540 | } | |
| 541 | } | |
| 542 | } | |
| 543 | ||
| 5 | 544 | /// Refresh aggregator after changes to generator. |
| 545 | /// | |
| 546 | /// The `generator` is used to convert the data of type `D` of the node into corresponding | |
| 547 | /// [`Support`]s. The `domain` is the cube corresponding to `self`. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
548 | pub(super) fn refresh_aggregator<'refs, 'scope, G>( |
| 0 | 549 | &mut self, |
| 550 | generator : &G, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
551 | domain : &Cube<F, N>, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
552 | task_budget : TaskBudget<'scope, 'refs>, |
| 0 | 553 | ) where G : SupportGenerator<F, N, Id=D>, |
| 554 | G::SupportType : LocalAnalysis<F, A, N> { | |
| 555 | match &mut self.data { | |
| 556 | NodeOption::Uninitialised => { }, | |
| 557 | NodeOption::Leaf(v) => { | |
| 558 | self.aggregator = A::new(); | |
| 559 | self.aggregator.aggregate(v.iter().map(|d| { | |
| 560 | generator.support_for(*d) | |
| 561 | .local_analysis(&domain) | |
| 562 | })); | |
| 563 | }, | |
| 564 | NodeOption::Branches(ref mut b) => { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
565 | // FIXME: recursion that may cause stack overflow if the tree becomes |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
566 | // very deep, e.g. due to [`BTSearch::search_and_refine`]. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
567 | let bm = Arc::make_mut(b); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
568 | bm.refresh_aggregator(generator, domain, task_budget); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
569 | bm.summarise_into(&mut self.aggregator); |
| 0 | 570 | } |
| 571 | } | |
| 572 | } | |
| 573 | } | |
| 574 | ||
| 5 | 575 | /// Helper trait for working with [`Node`]s without the knowledge of `P`. |
| 576 | /// | |
| 577 | /// This can be removed and the methods implemented directly on [`BT`] once Rust's const generics | |
| 578 | /// are flexible enough to allow fixing `P=pow(2, N)`. | |
| 0 | 579 | pub trait BTNode<F, D, A, const N : usize> |
| 580 | where F : Float, | |
| 581 | D : 'static + Copy, | |
| 582 | A : Aggregator { | |
| 583 | type Node : Clone + std::fmt::Debug; | |
| 584 | } | |
| 585 | ||
| 5 | 586 | /// Helper structure for looking up a [`Node`] without the knowledge of `P`. |
| 587 | /// | |
| 588 | /// This can be removed once Rust's const generics are flexible enough to allow fixing | |
| 589 | /// `P=pow(2, N)`. | |
| 0 | 590 | #[derive(Debug)] |
| 591 | pub struct BTNodeLookup; | |
| 592 | ||
| 5 | 593 | /// Basic interface to a [`BT`] bisection tree. |
| 594 | /// | |
| 595 | /// Further routines are provided by the [`BTSearch`][super::refine::BTSearch] trait. | |
| 0 | 596 | pub trait BTImpl<F : Float, const N : usize> : std::fmt::Debug + Clone + GlobalAnalysis<F, Self::Agg> { |
| 5 | 597 | /// The data type stored in the tree |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
598 | type Data : 'static + Copy + Send + Sync; |
| 5 | 599 | /// The depth type of the tree |
| 0 | 600 | type Depth : Depth; |
| 5 | 601 | /// The type for the [aggregate information][Aggregator] about the `Data` stored in each node |
| 602 | /// of the tree. | |
| 0 | 603 | type Agg : Aggregator; |
| 5 | 604 | /// The type of the tree with the aggregator converted to `ANew`. |
| 0 | 605 | type Converted<ANew> : BTImpl<F, N, Data=Self::Data, Agg=ANew> where ANew : Aggregator; |
| 606 | ||
| 5 | 607 | /// Insert the data `d` into the tree for `support`. |
| 608 | /// | |
| 609 | /// Every leaf node of the tree that intersects the `support` will contain a copy of | |
| 610 | /// `d`. | |
| 0 | 611 | fn insert<S : LocalAnalysis<F, Self::Agg, N>>( |
| 612 | &mut self, | |
| 613 | d : Self::Data, | |
| 614 | support : &S | |
| 615 | ); | |
| 616 | ||
| 5 | 617 | /// Construct a new instance of the tree for a different aggregator |
| 618 | /// | |
| 619 | /// The `generator` is used to convert the data of type [`Self::Data`] contained in the tree | |
| 620 | /// into corresponding [`Support`]s. | |
| 0 | 621 | fn convert_aggregator<ANew, G>(self, generator : &G) |
| 622 | -> Self::Converted<ANew> | |
| 623 | where ANew : Aggregator, | |
| 624 | G : SupportGenerator<F, N, Id=Self::Data>, | |
| 625 | G::SupportType : LocalAnalysis<F, ANew, N>; | |
| 626 | ||
| 627 | ||
| 5 | 628 | /// Refreshes the aggregator of the three after possible changes to the support generator. |
| 629 | /// | |
| 630 | /// The `generator` is used to convert the data of type [`Self::Data`] contained in the tree | |
| 631 | /// into corresponding [`Support`]s. | |
| 0 | 632 | fn refresh_aggregator<G>(&mut self, generator : &G) |
| 633 | where G : SupportGenerator<F, N, Id=Self::Data>, | |
| 634 | G::SupportType : LocalAnalysis<F, Self::Agg, N>; | |
| 635 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
636 | /// Returns an iterator over all [`Self::Data`] items at the point `x` of the domain. |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
637 | fn iter_at(&self, x : &Loc<F,N>) -> std::slice::Iter<'_, Self::Data>; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
638 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
639 | /* |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
640 | /// Returns all [`Self::Data`] items at the point `x` of the domain. |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
641 | fn data_at(&self, x : &Loc<F,N>) -> Arc<Vec<Self::Data>>; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
642 | */ |
| 0 | 643 | |
| 5 | 644 | /// Create a new tree on `domain` of indicated `depth`. |
| 0 | 645 | fn new(domain : Cube<F, N>, depth : Self::Depth) -> Self; |
| 646 | } | |
| 647 | ||
| 5 | 648 | /// The main bisection tree structure. |
| 649 | /// | |
| 650 | /// It should be accessed via the [`BTImpl`] trait to hide the `const P : usize` parameter until | |
| 651 | /// const generics are flexible enough to fix `P=pow(2, N)` and thus also get rid of | |
| 652 | /// the `BTNodeLookup : BTNode<F, D, A, N>` trait bound. | |
|
86
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
653 | #[derive(Clone,Debug,Serialize,Deserialize)] |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
654 | #[serde(bound( |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
655 | serialize = "Cube<F, N> : Serialize, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
656 | M : Serialize, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
657 | <BTNodeLookup as BTNode<F, D, A, N>>::Node : Serialize,", |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
658 | deserialize = "Cube<F, N> : for<'a> Deserialize<'a>, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
659 | M : for<'a> Deserialize<'a>, |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
660 | <BTNodeLookup as BTNode<F, D, A, N>>::Node : for<'a> Deserialize<'a>," |
|
d5b0e496b72f
More Serialize / Deserialize / Debug derives
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
661 | ))] |
| 0 | 662 | pub struct BT< |
| 663 | M : Depth, | |
| 664 | F : Float, | |
| 665 | D : 'static + Copy, | |
| 666 | A : Aggregator, | |
| 667 | const N : usize, | |
| 668 | > where BTNodeLookup : BTNode<F, D, A, N> { | |
| 5 | 669 | /// The depth of the tree (initial, before refinement) |
| 0 | 670 | pub(super) depth : M, |
| 5 | 671 | /// The domain of the toplevel node |
| 0 | 672 | pub(super) domain : Cube<F, N>, |
| 5 | 673 | /// The toplevel node of the tree |
| 0 | 674 | pub(super) topnode : <BTNodeLookup as BTNode<F, D, A, N>>::Node, |
| 675 | } | |
| 676 | ||
| 677 | macro_rules! impl_bt { | |
| 678 | ($($n:literal)*) => { $( | |
| 679 | impl<F, D, A> BTNode<F, D, A, $n> for BTNodeLookup | |
| 680 | where F : Float, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
681 | D : 'static + Copy + Send + Sync + std::fmt::Debug, |
| 0 | 682 | A : Aggregator { |
| 683 | type Node = Node<F,D,A,$n,{pow(2, $n)}>; | |
| 684 | } | |
| 685 | ||
| 686 | impl<M,F,D,A> BTImpl<F,$n> for BT<M,F,D,A,$n> | |
| 687 | where M : Depth, | |
| 688 | F : Float, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
689 | D : 'static + Copy + Send + Sync + std::fmt::Debug, |
| 0 | 690 | A : Aggregator { |
| 691 | type Data = D; | |
| 692 | type Depth = M; | |
| 693 | type Agg = A; | |
| 694 | type Converted<ANew> = BT<M,F,D,ANew,$n> where ANew : Aggregator; | |
| 695 | ||
| 696 | fn insert<S: LocalAnalysis<F, A, $n>>( | |
| 697 | &mut self, | |
| 698 | d : D, | |
| 699 | support : &S | |
| 700 | ) { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
701 | with_task_budget(|task_budget| |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
702 | self.topnode.insert( |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
703 | &self.domain, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
704 | d, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
705 | self.depth, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
706 | support, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
707 | task_budget |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
708 | ) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
709 | ) |
| 0 | 710 | } |
| 711 | ||
| 712 | fn convert_aggregator<ANew, G>(self, generator : &G) -> Self::Converted<ANew> | |
| 713 | where ANew : Aggregator, | |
| 714 | G : SupportGenerator<F, $n, Id=D>, | |
| 715 | G::SupportType : LocalAnalysis<F, ANew, $n> { | |
| 716 | let topnode = self.topnode.convert_aggregator(generator, &self.domain); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
717 | |
| 0 | 718 | BT { |
| 719 | depth : self.depth, | |
| 720 | domain : self.domain, | |
| 721 | topnode | |
| 722 | } | |
| 723 | } | |
| 724 | ||
| 725 | fn refresh_aggregator<G>(&mut self, generator : &G) | |
| 726 | where G : SupportGenerator<F, $n, Id=Self::Data>, | |
| 727 | G::SupportType : LocalAnalysis<F, Self::Agg, $n> { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
728 | with_task_budget(|task_budget| |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
729 | self.topnode.refresh_aggregator(generator, &self.domain, task_budget) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
730 | ) |
| 0 | 731 | } |
| 732 | ||
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
733 | /*fn data_at(&self, x : &Loc<F,$n>) -> Arc<Vec<D>> { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
734 | self.topnode.get_leaf_data(x).unwrap_or_else(|| Arc::new(Vec::new())) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
735 | }*/ |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
736 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
737 | fn iter_at(&self, x : &Loc<F,$n>) -> std::slice::Iter<'_, D> { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
738 | self.topnode.get_leaf_data_iter(x).unwrap_or_else(|| [].iter()) |
| 0 | 739 | } |
| 740 | ||
| 741 | fn new(domain : Cube<F, $n>, depth : M) -> Self { | |
| 742 | BT { | |
| 743 | depth : depth, | |
| 744 | domain : domain, | |
| 745 | topnode : Node::new(), | |
| 746 | } | |
| 747 | } | |
| 748 | } | |
| 749 | ||
| 750 | impl<M,F,D,A> GlobalAnalysis<F,A> for BT<M,F,D,A,$n> | |
| 751 | where M : Depth, | |
| 752 | F : Float, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
753 | D : 'static + Copy + Send + Sync + std::fmt::Debug, |
| 0 | 754 | A : Aggregator { |
| 755 | fn global_analysis(&self) -> A { | |
| 756 | self.topnode.get_aggregator().clone() | |
| 757 | } | |
| 758 | } | |
| 759 | )* } | |
| 760 | } | |
| 761 | ||
| 762 | impl_bt!(1 2 3 4); | |
| 763 |