Thu, 03 Sep 2026 11:55:09 -0500
p2powers_diff maybe fix
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
1 | use super::aggregator::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
2 | use super::bt::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
3 | use super::support::*; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
4 | use crate::parallelism::TaskBudget; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
5 | use crate::parallelism::{thread_pool, thread_pool_size}; |
| 5 | 6 | use crate::sets::Cube; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
7 | use crate::types::*; |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
8 | use num_traits::float::TotalOrder; |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
9 | use std::cmp::{Ord, Ordering, Ordering::*, PartialOrd}; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
10 | use std::collections::BinaryHeap; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
11 | use std::marker::PhantomData; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
12 | use std::sync::{Arc, Condvar, Mutex, MutexGuard}; |
| 0 | 13 | |
| 14 | /// Trait for sorting [`Aggregator`]s for [`BT`] refinement. | |
| 15 | /// | |
| 5 | 16 | /// The sorting involves two sorting keys, the “upper” and the “lower” key. Any [`BT`] nodes |
| 0 | 17 | /// with upper key less the lower key of another are discarded from the refinement process. |
| 5 | 18 | /// Nodes with the highest upper sorting key are picked for refinement. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
19 | pub trait AggregatorSorting: Sync + Send + 'static { |
| 0 | 20 | // Priority |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
21 | type Agg: Aggregator; |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
22 | /// This is temporarily a Float, after removal of NanLeast, to use [ |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
23 | /// `num_traits::float::TotalOrder`] and [`num_traits::float::Float::max`]. |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
24 | /// It should be generalised by introducing a general PseudoTotalOrder trait |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
25 | /// (because Rust's standard one is not implemented for `Float`s) |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
26 | type Sort: Float + Copy + std::fmt::Debug + Sync + Send; |
| 0 | 27 | |
| 28 | /// Returns lower sorting key | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
29 | fn sort_lower(aggregator: &Self::Agg) -> Self::Sort; |
| 0 | 30 | |
| 31 | /// Returns upper sorting key | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
32 | fn sort_upper(aggregator: &Self::Agg) -> Self::Sort; |
| 0 | 33 | |
| 5 | 34 | /// Returns a sorting key that is less than any other sorting key. |
| 0 | 35 | fn bottom() -> Self::Sort; |
| 36 | } | |
| 37 | ||
| 38 | /// An [`AggregatorSorting`] for [`Bounds`], using the upper/lower bound as the upper/lower key. | |
| 39 | /// | |
| 40 | /// See [`LowerBoundSorting`] for the opposite ordering. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
41 | pub struct UpperBoundSorting<F: Float>(PhantomData<F>); |
| 0 | 42 | |
| 43 | /// An [`AggregatorSorting`] for [`Bounds`], using the upper/lower bound as the lower/upper key. | |
| 44 | /// | |
| 45 | /// See [`UpperBoundSorting`] for the opposite ordering. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
46 | pub struct LowerBoundSorting<F: Float>(PhantomData<F>); |
| 0 | 47 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
48 | impl<F: Float> AggregatorSorting for UpperBoundSorting<F> { |
| 0 | 49 | type Agg = Bounds<F>; |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
50 | type Sort = F; |
| 0 | 51 | |
| 52 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
53 | fn sort_lower(aggregator: &Bounds<F>) -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
54 | aggregator.lower() |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
55 | } |
| 0 | 56 | |
| 57 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
58 | fn sort_upper(aggregator: &Bounds<F>) -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
59 | aggregator.upper() |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
60 | } |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
61 | |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
62 | #[inline] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
63 | fn bottom() -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
64 | F::NEG_INFINITY |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
65 | } |
| 0 | 66 | } |
| 67 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
68 | impl<F: Float> AggregatorSorting for LowerBoundSorting<F> { |
| 0 | 69 | type Agg = Bounds<F>; |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
70 | type Sort = F; |
| 0 | 71 | |
| 72 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
73 | fn sort_upper(aggregator: &Bounds<F>) -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
74 | -aggregator.lower() |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
75 | } |
| 0 | 76 | |
| 77 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
78 | fn sort_lower(aggregator: &Bounds<F>) -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
79 | -aggregator.upper() |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
80 | } |
| 0 | 81 | |
| 82 | #[inline] | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
83 | fn bottom() -> Self::Sort { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
84 | F::NEG_INFINITY |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
85 | } |
| 0 | 86 | } |
| 87 | ||
| 5 | 88 | /// Return type of [`Refiner::refine`]. |
| 89 | /// | |
| 90 | /// The parameter `R` is the result type of the refiner acting on an [`Aggregator`] of type `A`. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
91 | pub enum RefinerResult<A: Aggregator, R> { |
| 5 | 92 | /// Indicates an insufficiently refined state: the [`BT`] needs to be further refined. |
| 0 | 93 | NeedRefinement, |
| 94 | /// Indicates a certain result `R`, stop refinement immediately. | |
| 95 | Certain(R), | |
| 96 | /// Indicates an uncertain result: continue refinement until candidates have been exhausted | |
| 97 | /// or a certain result found. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
98 | Uncertain(A, R), |
| 0 | 99 | } |
| 100 | ||
| 101 | use RefinerResult::*; | |
| 102 | ||
| 5 | 103 | /// A `Refiner` is used to search a [`BT`], refining the subdivision when necessary. |
| 104 | /// | |
| 105 | /// The search is performed by [`BTSearch::search_and_refine`]. | |
| 106 | /// The `Refiner` is used to determine whether an [`Aggregator`] `A` stored in the [`BT`] is | |
| 107 | /// sufficiently refined within a [`Cube`], and in such a case, produce a desired result (e.g. | |
| 108 | /// a maximum value of a function). | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
109 | pub trait Refiner<F: Float, A, G, const N: usize>: Sync + Send + 'static |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
110 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
111 | F: Num, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
112 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
113 | G: SupportGenerator<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
114 | { |
| 5 | 115 | /// The result type of the refiner |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
116 | type Result: std::fmt::Debug + Sync + Send + 'static; |
| 5 | 117 | /// The sorting to be employed by [`BTSearch::search_and_refine`] on node aggregators |
| 118 | /// to detemrine node priority. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
119 | type Sorting: AggregatorSorting<Agg = A>; |
| 0 | 120 | |
| 5 | 121 | /// Determines whether `aggregator` is sufficiently refined within `domain`. |
| 122 | /// | |
| 123 | /// If the aggregator is sufficiently refined that the desired `Self::Result` can be produced, | |
| 124 | /// a [`RefinerResult`]`::Certain` or `Uncertain` should be returned, depending on | |
| 125 | /// the confidence of the solution. In the uncertain case an improved aggregator should also | |
| 126 | /// be included. If the result cannot be produced, `NeedRefinement` should be | |
| 127 | /// returned. | |
| 128 | /// | |
| 129 | /// For example, if the refiner is used to minimise a function presented by the `BT`, | |
| 130 | /// an `Uncertain` result can be used to return a local maximum of the function on `domain` | |
| 131 | /// The result can be claimed `Certain` if it is a global maximum. In that case the | |
| 132 | /// refinment will stop immediately. A `NeedRefinement` result indicates that the `aggregator` | |
| 133 | /// and/or `domain` are not sufficiently refined to compute a lcoal maximum of sufficient | |
| 134 | /// quality. | |
| 135 | /// | |
| 136 | /// The vector `data` stored all the data of the [`BT`] in the node corresponding to `domain`. | |
| 137 | /// The `generator` can be used to convert `data` into [`Support`]s. The parameter `step` | |
| 138 | /// counts the calls to `refine`, and can be used to stop the refinement when a maximum | |
| 139 | /// number of steps is reached. | |
| 0 | 140 | fn refine( |
| 141 | &self, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
142 | aggregator: &A, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
143 | domain: &Cube<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
144 | data: &[G::Id], |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
145 | generator: &G, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
146 | step: usize, |
| 0 | 147 | ) -> RefinerResult<A, Self::Result>; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
148 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
149 | /// Fuse two [`Self::Result`]s (needed in threaded refinement). |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
150 | fn fuse_results(r1: &mut Self::Result, r2: Self::Result); |
| 0 | 151 | } |
| 152 | ||
| 153 | /// Structure for tracking the refinement process in a [`BinaryHeap`]. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
154 | struct RefinementInfo<'a, F, D, A, S, RResult, const N: usize, const P: usize> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
155 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
156 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
157 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
158 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
159 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
160 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
161 | /// Domain of `node` |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
162 | cube: Cube<N, F>, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
163 | /// Node to be refined |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
164 | node: &'a mut Node<F, D, A, N, P>, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
165 | /// Result and improve aggregator for the [`Refiner`] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
166 | refiner_info: Option<(A, RResult)>, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
167 | /// For [`AggregatorSorting`] being used for the type system |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
168 | sorting: PhantomData<S>, |
| 0 | 169 | } |
| 170 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
171 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
172 | RefinementInfo<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
173 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
174 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
175 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
176 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
177 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
178 | { |
| 0 | 179 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
180 | fn with_aggregator<U>(&self, f: impl FnOnce(&A) -> U) -> U { |
| 0 | 181 | match self.refiner_info { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
182 | Some((ref agg, _)) => f(agg), |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
183 | None => f(&self.node.aggregator), |
| 0 | 184 | } |
| 185 | } | |
| 186 | ||
| 187 | #[inline] | |
| 188 | fn sort_lower(&self) -> S::Sort { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
189 | self.with_aggregator(S::sort_lower) |
| 0 | 190 | } |
| 191 | ||
| 192 | #[inline] | |
| 193 | fn sort_upper(&self) -> S::Sort { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
194 | self.with_aggregator(S::sort_upper) |
| 0 | 195 | } |
| 196 | } | |
| 197 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
198 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> PartialEq |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
199 | for RefinementInfo<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
200 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
201 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
202 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
203 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
204 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
205 | { |
| 0 | 206 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
207 | fn eq(&self, other: &Self) -> bool { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
208 | self.cmp(other) == Equal |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
209 | } |
| 0 | 210 | } |
| 211 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
212 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> PartialOrd |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
213 | for RefinementInfo<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
214 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
215 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
216 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
217 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
218 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
219 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
220 | #[inline] |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
221 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
222 | Some(self.cmp(other)) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
223 | } |
| 0 | 224 | } |
| 225 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
226 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> Eq |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
227 | for RefinementInfo<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
228 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
229 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
230 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
231 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
232 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
233 | { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
234 | } |
| 0 | 235 | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
236 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> Ord |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
237 | for RefinementInfo<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
238 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
239 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
240 | D: 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
241 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
242 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
243 | { |
| 0 | 244 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
245 | fn cmp(&self, other: &Self) -> Ordering { |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
246 | self.with_aggregator(|agg1| { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
247 | other.with_aggregator(|agg2| { |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
248 | match S::sort_upper(agg1).total_cmp(&S::sort_upper(agg2)) { |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
249 | Equal => S::sort_lower(agg1).total_cmp(&S::sort_lower(agg2)), |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
250 | order => order, |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
251 | } |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
252 | }) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
253 | }) |
| 0 | 254 | } |
| 255 | } | |
| 256 | ||
| 5 | 257 | /// This is a container for a [`BinaryHeap`] of [`RefinementInfo`]s together with tracking of |
| 258 | /// the greatest lower bound of the [`Aggregator`]s of the [`Node`]s therein accroding to | |
| 259 | /// chosen [`AggregatorSorting`]. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
260 | struct HeapContainer<'a, F, D, A, S, RResult, const N: usize, const P: usize> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
261 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
262 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
263 | D: 'static + Copy, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
264 | Const<P>: BranchCount<N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
265 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
266 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
267 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
268 | /// Priority queue of nodes to be refined |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
269 | heap: BinaryHeap<RefinementInfo<'a, F, D, A, S, RResult, N, P>>, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
270 | /// Maximum of node sorting lower bounds seen in the heap |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
271 | glb: S::Sort, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
272 | /// Number of insertions in the heap since previous prune |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
273 | insert_counter: usize, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
274 | /// If a result has been found by some refinment threat, it is stored here |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
275 | result: Option<RResult>, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
276 | /// Refinement step counter |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
277 | step: usize, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
278 | /// Number of threads currently processing (not sleeping) |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
279 | n_processing: usize, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
280 | /// Threshold for heap pruning |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
281 | heap_prune_threshold: usize, |
| 0 | 282 | } |
| 283 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
284 | impl<'a, F, D, A, S, RResult, const N: usize, const P: usize> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
285 | HeapContainer<'a, F, D, A, S, RResult, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
286 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
287 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
288 | D: 'static + Copy, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
289 | Const<P>: BranchCount<N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
290 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
291 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
292 | { |
| 5 | 293 | /// Push `ri` into the [`BinaryHeap`]. Do greatest lower bound maintenance. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
294 | /// |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
295 | /// Returns a boolean indicating whether the push was actually performed due to glb |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
296 | /// filtering or not. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
297 | #[inline] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
298 | fn push(&mut self, ri: RefinementInfo<'a, F, D, A, S, RResult, N, P>) -> bool { |
| 0 | 299 | if ri.sort_upper() >= self.glb { |
| 300 | let l = ri.sort_lower(); | |
| 301 | self.heap.push(ri); | |
| 302 | self.glb = self.glb.max(l); | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
303 | self.insert_counter += 1; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
304 | true |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
305 | } else { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
306 | false |
| 0 | 307 | } |
| 308 | } | |
| 309 | } | |
| 310 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
311 | impl<F: Float, D, A, const N: usize, const P: usize> Branches<F, D, A, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
312 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
313 | Const<P>: BranchCount<N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
314 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
315 | D: 'static + Copy + Send + Sync, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
316 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
317 | /// Stage all subnodes of `self` into the refinement queue `container`. |
| 0 | 318 | fn stage_refine<'a, S, RResult>( |
| 319 | &'a mut self, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
320 | domain: Cube<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
321 | container: &mut HeapContainer<'a, F, D, A, S, RResult, N, P>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
322 | ) where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
323 | S: AggregatorSorting<Agg = A>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
324 | { |
| 0 | 325 | // Insert all subnodes into the refinement heap. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
326 | for (node, cube) in self.nodes_and_cubes_mut(&domain) { |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
327 | container.push(RefinementInfo { cube, node, refiner_info: None, sorting: PhantomData }); |
| 0 | 328 | } |
| 329 | } | |
| 330 | } | |
| 331 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
332 | impl<F: Float, D, A, const N: usize, const P: usize> Node<F, D, A, N, P> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
333 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
334 | Const<P>: BranchCount<N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
335 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
336 | D: 'static + Copy + Send + Sync, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
337 | { |
| 0 | 338 | /// If `self` is a leaf node, uses the `refiner` to determine whether further subdivision |
| 339 | /// is required to get a sufficiently refined solution for the problem the refiner is used | |
| 340 | /// to solve. If the refiner returns [`RefinerResult::Certain`] result, it is returned. | |
| 341 | /// If [`RefinerResult::Uncertain`] is returned, the leaf is inserted back into the refinement | |
| 342 | /// queue `container`. If `self` is a branch, its subnodes are staged into `container` using | |
| 343 | /// [`Branches::stage_refine`]. | |
| 5 | 344 | /// |
| 345 | /// `domain`, as usual, indicates the spatial area corresponding to `self`. | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
346 | fn search_and_refine<'a, 'b, 'c, R, G>( |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
347 | self: &'a mut Self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
348 | domain: Cube<N, F>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
349 | refiner: &R, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
350 | generator: &G, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
351 | container_arc: &'c Arc<Mutex<HeapContainer<'a, F, D, A, R::Sorting, R::Result, N, P>>>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
352 | step: usize, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
353 | ) -> Result<R::Result, MutexGuard<'c, HeapContainer<'a, F, D, A, R::Sorting, R::Result, N, P>>> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
354 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
355 | R: Refiner<F, A, G, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
356 | G: SupportGenerator<N, F, Id = D>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
357 | G::SupportType: LocalAnalysis<F, A, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
358 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
359 | //drop(container); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
360 | |
| 0 | 361 | // Refine a leaf. |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
362 | let res = match self.data { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
363 | NodeOption::Leaf(ref mut v) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
364 | let res = refiner.refine(&self.aggregator, &domain, v, generator, step); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
365 | if let NeedRefinement = res { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
366 | // The refiner has deemed the leaf unsufficiently refined, so subdivide |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
367 | // it and add the new nodes into the refinement priority heap. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
368 | let mut it = v.iter(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
369 | // Only create new branches if there's anything to add. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
370 | // We insert the last item first to mix the support_hint a bit. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
371 | if let Some(&d0) = it.next_back() { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
372 | // Construct new Branches |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
373 | let support = generator.support_for(d0); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
374 | let mut b = Branches::new_with(&domain, &support); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
375 | b.insert(&domain, d0, Const::<1>, &support, TaskBudget::none()); |
| 0 | 376 | for &d in it { |
| 377 | let support = generator.support_for(d); | |
| 378 | // TODO: can we be smarter than just refining one level? | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
379 | b.insert(&domain, d, Const::<1>, &support, TaskBudget::none()); |
| 0 | 380 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
381 | // Update current node and stage refinement of new branches. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
382 | b.summarise_into(&mut self.aggregator); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
383 | // FIXME: parent aggregators are not updated and will be out-of-date, but |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
384 | // right now pointsource_algs is not really taking advantage of the |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
385 | // aggregators being updated. Moreover, insertion and aggregator refinement |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
386 | // code in `bt.rs` will overflow the stack in deeply nested trees. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
387 | // We nevertheless need to store `b` into `self` to be able to queue |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
388 | // the branches. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
389 | self.data = NodeOption::Branches(Arc::new(b)); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
390 | // This ugly match is needed to keep the compiler happy about lifetimes. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
391 | match self.data { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
392 | NodeOption::Branches(ref mut arc_b) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
393 | let mut container = container_arc.lock().unwrap(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
394 | // Safe: we just created arg_b and have a mutable exclusive |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
395 | // reference to self containing it. |
| 94 | 396 | #[cfg(nightly)] |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
397 | unsafe { Arc::get_mut_unchecked(arc_b) } |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
398 | .stage_refine(domain, &mut *container); |
| 94 | 399 | #[cfg(not(nightly))] |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
400 | Arc::get_mut(arc_b) |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
401 | .unwrap() |
|
55
7b2ee3e84c5f
Add "nightly" feature and provide alternative low-performance implementations of several things when not available.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
402 | .stage_refine(domain, &mut *container); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
403 | |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
404 | return Err(container); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
405 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
406 | _ => unreachable!("This cannot happen"), |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
407 | } |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
408 | } |
| 0 | 409 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
410 | res |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
411 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
412 | NodeOption::Branches(ref mut b) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
413 | // Insert branches into refinement priority queue. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
414 | let mut container = container_arc.lock().unwrap(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
415 | Arc::make_mut(b).stage_refine(domain, &mut *container); |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
416 | return Err(container); |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
417 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
418 | NodeOption::Uninitialised => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
419 | refiner.refine(&self.aggregator, &domain, &[], generator, step) |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
420 | } |
| 0 | 421 | }; |
| 422 | ||
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
423 | match res { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
424 | Uncertain(agg, val) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
425 | // The refiner gave an undertain result. Push a leaf back into the refinement queue |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
426 | // with the new refined aggregator and custom return value. It will be popped and |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
427 | // returned in the loop of [`BTSearch::search_and_refine`] when there are no |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
428 | // unrefined candidates that could potentially be better according to their basic |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
429 | // aggregator. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
430 | let mut container = container_arc.lock().unwrap(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
431 | container.push(RefinementInfo { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
432 | cube: domain, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
433 | node: self, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
434 | refiner_info: Some((agg, val)), |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
435 | sorting: PhantomData, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
436 | }); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
437 | Err(container) |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
438 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
439 | Certain(val) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
440 | // The refiner gave a certain result so return it to allow early termination |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
441 | Ok(val) |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
442 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
443 | NeedRefinement => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
444 | // This should only happen when we run into NodeOption::Uninitialised above. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
445 | // There's really nothing to do. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
446 | panic!("Do not know whow to refine uninitialised nodes"); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
447 | } |
| 0 | 448 | } |
| 449 | } | |
| 450 | } | |
| 451 | ||
| 5 | 452 | /// Interface trait to a refining search on a [`BT`]. |
| 453 | /// | |
| 454 | /// This can be removed and the methods implemented directly on [`BT`] once Rust's const generics | |
| 455 | /// are flexible enough to allow fixing `P=pow(2, N)`. | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
456 | pub trait BTSearch<const N: usize, F = f64>: BTImpl<N, F> |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
457 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
458 | F: Float, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
459 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
460 | /// Perform a search on on `Self`, as determined by `refiner`. |
| 5 | 461 | /// |
| 462 | /// Nodes are inserted in a priority queue and processed in the order determined by the | |
| 463 | /// [`AggregatorSorting`] [`Refiner::Sorting`]. Leaf nodes are subdivided until the refiner | |
| 464 | /// decides that a sufficiently refined leaf node has been found, as determined by either the | |
| 465 | /// refiner returning a [`RefinerResult::Certain`] result, or a previous | |
| 466 | /// [`RefinerResult::Uncertain`] result is found again at the top of the priority queue. | |
| 467 | /// | |
| 468 | /// The `generator` converts [`BTImpl::Data`] stored in the bisection tree into a [`Support`]. | |
| 0 | 469 | fn search_and_refine<'b, R, G>( |
| 470 | &'b mut self, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
471 | refiner: R, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
472 | generator: &Arc<G>, |
| 0 | 473 | ) -> Option<R::Result> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
474 | where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
475 | R: Refiner<F, Self::Agg, G, N> + Sync + Send + 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
476 | G: SupportGenerator<N, F, Id = Self::Data> + Sync + Send + 'static, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
477 | G::SupportType: LocalAnalysis<F, Self::Agg, N>; |
| 0 | 478 | } |
| 479 | ||
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
480 | fn refinement_loop<F: Float, D, A, R, G, const N: usize, const P: usize>( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
481 | wakeup: Option<Arc<Condvar>>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
482 | refiner: &R, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
483 | generator_arc: &Arc<G>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
484 | container_arc: &Arc<Mutex<HeapContainer<F, D, A, R::Sorting, R::Result, N, P>>>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
485 | ) where |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
486 | A: Aggregator, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
487 | R: Refiner<F, A, G, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
488 | G: SupportGenerator<N, F, Id = D>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
489 | G::SupportType: LocalAnalysis<F, A, N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
490 | Const<P>: BranchCount<N>, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
491 | D: 'static + Copy + Sync + Send + std::fmt::Debug, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
492 | { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
493 | let mut did_park = true; |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
494 | let mut container = container_arc.lock().unwrap(); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
495 | |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
496 | 'main: loop { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
497 | // Find a node to process |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
498 | let ri = 'get_next: loop { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
499 | if did_park { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
500 | container.n_processing += 1; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
501 | did_park = false; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
502 | } |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
503 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
504 | // Some refinement task/thread has found a result, return |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
505 | if container.result.is_some() { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
506 | container.n_processing -= 1; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
507 | break 'main; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
508 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
509 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
510 | match container.heap.pop() { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
511 | // There's work to be done. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
512 | Some(ri) => break 'get_next ri, |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
513 | // No work to be done; park if some task/thread is still processing nodes, |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
514 | // fail if not. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
515 | None => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
516 | debug_assert!(container.n_processing > 0); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
517 | container.n_processing -= 1; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
518 | if container.n_processing == 0 { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
519 | break 'main; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
520 | } else if let Some(ref c) = wakeup { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
521 | did_park = true; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
522 | container = c.wait(container).unwrap(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
523 | continue 'get_next; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
524 | } else { |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
525 | break 'main; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
526 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
527 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
528 | }; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
529 | }; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
530 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
531 | let step = container.step; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
532 | container.step += 1; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
533 | |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
534 | if let Some((_, result)) = ri.refiner_info { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
535 | // Terminate based on a “best possible” result. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
536 | container.result = Some(result); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
537 | container.n_processing -= 1; |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
538 | break 'main; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
539 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
540 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
541 | // Do priority queue maintenance |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
542 | if container.insert_counter > container.heap_prune_threshold { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
543 | // Make sure glb is good. |
|
199
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
544 | match container |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
545 | .heap |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
546 | .iter() |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
547 | .map(|ri| ri.sort_lower()) |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
548 | .reduce(num_traits::Float::max) |
|
32f5062ee477
num_traits::float::TotalOrder to Float trait bounds. Remove NaNLeast.
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
549 | { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
550 | Some(glb) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
551 | container.glb = glb; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
552 | // Prune |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
553 | container.heap.retain(|ri| ri.sort_upper() >= glb); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
554 | } |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
555 | None => container.glb = R::Sorting::bottom(), |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
556 | } |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
557 | container.insert_counter = 0; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
558 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
559 | |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
560 | // Unlock the mutex… |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
561 | drop(container); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
562 | |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
563 | // … and process the node. We may get returned an already unlocked mutex. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
564 | match Node::search_and_refine( |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
565 | ri.node, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
566 | ri.cube, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
567 | refiner, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
568 | &**generator_arc, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
569 | &container_arc, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
570 | step, |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
571 | ) { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
572 | Ok(r) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
573 | let mut container = container_arc.lock().unwrap(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
574 | // Terminate based on a certain result from the refiner |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
575 | match container.result { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
576 | Some(ref mut r_prev) => R::fuse_results(r_prev, r), |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
577 | None => container.result = Some(r), |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
578 | } |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
579 | break 'main; |
|
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
580 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
581 | Err(cnt) => { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
582 | container = cnt; |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
583 | // Wake up another thread if one is sleeping; there should be now work in the |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
584 | // queue. |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
585 | if let Some(ref c) = wakeup { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
586 | c.notify_one(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
587 | } |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
588 | } |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
589 | } |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
590 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
591 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
592 | // Make sure no task is sleeping |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
593 | if let Some(ref c) = wakeup { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
594 | c.notify_all(); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
595 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
596 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
597 | |
| 0 | 598 | // Needed to get access to a Node without a trait interface. |
| 599 | macro_rules! impl_btsearch { | |
| 600 | ($($n:literal)*) => { $( | |
| 601 | impl<'a, M, F, D, A> | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
602 | BTSearch<$n, F> |
| 0 | 603 | for BT<M,F,D,A,$n> |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
604 | where //Self : BTImpl<$n, F, Data=D,Agg=A, Depth=M>, // <== automatically deduced |
| 0 | 605 | M : Depth, |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
606 | F : Float + Send, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
607 | A : Aggregator, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
608 | D : 'static + Copy + Sync + Send + std::fmt::Debug { |
| 0 | 609 | fn search_and_refine<'b, R, G>( |
| 610 | &'b mut self, | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
611 | refiner : R, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
612 | generator : &Arc<G>, |
| 0 | 613 | ) -> Option<R::Result> |
| 614 | where R : Refiner<F, A, G, $n>, | |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
94
diff
changeset
|
615 | G : SupportGenerator< $n, F, Id=D>, |
| 0 | 616 | G::SupportType : LocalAnalysis<F, A, $n> { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
617 | let mut init_container = HeapContainer { |
| 0 | 618 | heap : BinaryHeap::new(), |
| 619 | glb : R::Sorting::bottom(), | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
620 | insert_counter : 0, |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
621 | result : None, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
622 | step : 0, |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
623 | n_processing : 0, |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
624 | // An arbitrary threshold for starting pruning of the heap |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
625 | heap_prune_threshold : 2u32.pow(16.max($n * self.depth.value())) as usize |
| 0 | 626 | }; |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
627 | init_container.push(RefinementInfo { |
| 0 | 628 | cube : self.domain, |
| 629 | node : &mut self.topnode, | |
| 630 | refiner_info : None, | |
| 631 | sorting : PhantomData, | |
| 632 | }); | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
633 | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
634 | let container_arc = Arc::new(Mutex::new(init_container)); |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
635 | if let Some(pool) = thread_pool() { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
636 | let n = thread_pool_size(); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
637 | pool.scope(|s| { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
638 | let wakeup = Arc::new(Condvar::new()); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
639 | for _ in 0..n { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
640 | let refiner_ref = &refiner; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
641 | let container_t = Arc::clone(&container_arc); |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
642 | let wakeup_t = Arc::clone(&wakeup); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
643 | s.spawn(move |_| { |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
644 | refinement_loop(Some(wakeup_t), refiner_ref, generator, |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
645 | &container_t); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
646 | }); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
647 | } |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
648 | refinement_loop(Some(wakeup), &refiner, generator, &container_arc); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
649 | }); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
650 | } else { |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
651 | refinement_loop(None, &refiner, generator, &container_arc); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
652 | } |
| 0 | 653 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
654 | match Arc::try_unwrap(container_arc) { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
655 | Ok(mtx) => mtx.into_inner().unwrap().result, |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
656 | Err(_) => panic!("Refinement threads not finished properly."), |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
657 | } |
| 0 | 658 | } |
| 659 | } | |
| 660 | )* } | |
| 661 | } | |
| 662 | ||
| 663 | impl_btsearch!(1 2 3 4); |