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