Fri, 06 Dec 2024 16:14:41 -0500
Add "nightly" feature and provide alternative low-performance implementations of several things when not available.
5 | 1 | |
2 | use std::iter::Chain; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
3 | use std::sync::Arc; |
0 | 4 | |
5 | use crate::types::*; | |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
6 | use crate::mapping::{Apply, Differentiable}; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
7 | use crate::iter::{Mappable, MapF, MapZ}; |
5 | 8 | use crate::sets::Cube; |
9 | use crate::loc::Loc; | |
10 | ||
0 | 11 | use super::support::*; |
12 | use super::aggregator::*; | |
13 | ||
5 | 14 | /// A structure for storing two [`SupportGenerator`]s summed/chain together. |
15 | /// | |
16 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 17 | #[derive(Debug,Clone)] |
18 | pub struct BothGenerators<A, B>( | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
19 | pub(super) Arc<A>, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
20 | pub(super) Arc<B>, |
0 | 21 | ); |
22 | ||
5 | 23 | /// A structure for a [`Support`] that can be either `A` or `B`. |
24 | /// | |
25 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 26 | #[derive(Debug,Clone)] |
27 | pub enum EitherSupport<A, B> { | |
28 | Left(A), | |
29 | Right(B), | |
30 | } | |
31 | ||
32 | // We need type alias bounds to access associate types. | |
33 | #[allow(type_alias_bounds)] | |
34 | type BothAllDataIter< | |
35 | 'a, F, | |
36 | G1 : SupportGenerator<F, N>, | |
37 | G2 : SupportGenerator<F, N>, | |
38 | const N : usize | |
39 | > = Chain< | |
40 | MapF<G1::AllDataIter<'a>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
41 | MapZ<G2::AllDataIter<'a>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
42 | >; | |
43 | ||
44 | impl<G1, G2> BothGenerators<G1, G2> { | |
5 | 45 | /// Helper for [`all_left_data`]. |
0 | 46 | #[inline] |
47 | fn map_left<F : Float, const N : usize>((d, support) : (G1::Id, G1::SupportType)) | |
48 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
49 | where G1 : SupportGenerator<F, N, Id=usize>, | |
50 | G2 : SupportGenerator<F, N, Id=usize> { | |
51 | ||
52 | let id : usize = d.into(); | |
53 | (id.into(), EitherSupport::Left(support)) | |
54 | } | |
55 | ||
5 | 56 | /// Helper for [`all_right_data`]. |
0 | 57 | #[inline] |
58 | fn map_right<F : Float, const N : usize>(n0 : &usize, (d, support) : (G2::Id, G2::SupportType)) | |
59 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
60 | where G1 : SupportGenerator<F, N, Id=usize>, | |
61 | G2 : SupportGenerator<F, N, Id=usize> { | |
62 | ||
63 | let id : usize = d.into(); | |
64 | ((n0+id).into(), EitherSupport::Right(support)) | |
65 | } | |
66 | ||
5 | 67 | /// Calls [`SupportGenerator::all_data`] on the “left” support generator. |
68 | /// | |
69 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 70 | #[inline] |
71 | pub(super) fn all_left_data<F : Float, const N : usize>(&self) | |
72 | -> MapF<G1::AllDataIter<'_>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
73 | where G1 : SupportGenerator<F, N, Id=usize>, | |
74 | G2 : SupportGenerator<F, N, Id=usize> { | |
75 | self.0.all_data().mapF(Self::map_left) | |
76 | } | |
77 | ||
5 | 78 | /// Calls [`SupportGenerator::all_data`] on the “right” support generator. |
79 | /// | |
80 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 81 | #[inline] |
82 | pub(super) fn all_right_data<F : Float, const N : usize>(&self) | |
83 | -> MapZ<G2::AllDataIter<'_>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
84 | where G1 : SupportGenerator<F, N, Id=usize>, | |
85 | G2 : SupportGenerator<F, N, Id=usize> { | |
86 | let n0 = self.0.support_count(); | |
87 | self.1.all_data().mapZ(n0, Self::map_right) | |
88 | } | |
89 | } | |
90 | ||
91 | impl<F : Float, G1, G2, const N : usize> | |
92 | SupportGenerator<F, N> | |
93 | for BothGenerators<G1, G2> | |
94 | where G1 : SupportGenerator<F, N, Id=usize>, | |
95 | G2 : SupportGenerator<F, N, Id=usize> { | |
96 | ||
97 | type Id = usize; | |
98 | type SupportType = EitherSupport<G1::SupportType, G2::SupportType>; | |
99 | type AllDataIter<'a> = BothAllDataIter<'a, F, G1, G2, N> where G1 : 'a, G2 : 'a; | |
100 | ||
101 | #[inline] | |
102 | fn support_for(&self, id : Self::Id) | |
103 | -> Self::SupportType { | |
104 | let n0 = self.0.support_count(); | |
105 | if id < n0 { | |
106 | EitherSupport::Left(self.0.support_for(id.into())) | |
107 | } else { | |
108 | EitherSupport::Right(self.1.support_for((id-n0).into())) | |
109 | } | |
110 | } | |
111 | ||
112 | #[inline] | |
113 | fn support_count(&self) -> usize { | |
114 | self.0.support_count() + self.1.support_count() | |
115 | } | |
116 | ||
117 | #[inline] | |
118 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
119 | self.all_left_data().chain(self.all_right_data()) | |
120 | } | |
121 | } | |
122 | ||
123 | impl<F: Float, S1, S2, const N : usize> Support<F, N> for EitherSupport<S1, S2> | |
124 | where S1 : Support<F, N>, | |
125 | S2 : Support<F, N> { | |
126 | ||
127 | #[inline] | |
128 | fn support_hint(&self) -> Cube<F,N> { | |
129 | match self { | |
130 | EitherSupport::Left(ref a) => a.support_hint(), | |
131 | EitherSupport::Right(ref b) => b.support_hint(), | |
132 | } | |
133 | } | |
134 | ||
135 | #[inline] | |
136 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
137 | match self { | |
138 | EitherSupport::Left(ref a) => a.in_support(x), | |
139 | EitherSupport::Right(ref b) => b.in_support(x), | |
140 | } | |
141 | } | |
142 | ||
143 | #[inline] | |
144 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
145 | match self { | |
146 | EitherSupport::Left(ref a) => a.bisection_hint(cube), | |
147 | EitherSupport::Right(ref b) => b.bisection_hint(cube), | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | impl<F : Float, A, S1, S2, const N : usize> LocalAnalysis<F, A, N> for EitherSupport<S1, S2> | |
153 | where A : Aggregator, | |
154 | S1 : LocalAnalysis<F, A, N>, | |
155 | S2 : LocalAnalysis<F, A, N>, { | |
156 | ||
157 | #[inline] | |
158 | fn local_analysis(&self, cube : &Cube<F, N>) -> A { | |
159 | match self { | |
160 | EitherSupport::Left(ref a) => a.local_analysis(cube), | |
161 | EitherSupport::Right(ref b) => b.local_analysis(cube), | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | impl<F : Float, A, S1, S2> GlobalAnalysis<F, A> for EitherSupport<S1, S2> | |
167 | where A : Aggregator, | |
168 | S1 : GlobalAnalysis<F, A>, | |
169 | S2 : GlobalAnalysis<F, A>, { | |
170 | ||
171 | #[inline] | |
172 | fn global_analysis(&self) -> A { | |
173 | match self { | |
174 | EitherSupport::Left(ref a) => a.global_analysis(), | |
175 | EitherSupport::Right(ref b) => b.global_analysis(), | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
180 | impl<F, S1, S2, X> Apply<X> for EitherSupport<S1, S2> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
181 | where S1 : Apply<X, Output=F>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
182 | S2 : Apply<X, Output=F> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
183 | type Output = F; |
0 | 184 | #[inline] |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
185 | fn apply(&self, x : X) -> F { |
0 | 186 | match self { |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
187 | EitherSupport::Left(ref a) => a.apply(x), |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
188 | EitherSupport::Right(ref b) => b.apply(x), |
0 | 189 | } |
190 | } | |
191 | } | |
192 | ||
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
193 | impl<F, S1, S2, X> Differentiable<X> for EitherSupport<S1, S2> |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
194 | where S1 : Differentiable<X, Derivative=F>, |
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
195 | S2 : Differentiable<X, Derivative=F> { |
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
196 | type Derivative = F; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
197 | #[inline] |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
198 | fn differential(&self, x : X) -> F { |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
199 | match self { |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
200 | EitherSupport::Left(ref a) => a.differential(x), |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
201 | EitherSupport::Right(ref b) => b.differential(x), |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
202 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
203 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
204 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
205 | |
0 | 206 | macro_rules! make_either_scalarop_rhs { |
207 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
208 | impl<F : Float, G1, G2> | |
209 | std::ops::$trait_assign<F> | |
210 | for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
211 | where G1 : std::ops::$trait_assign<F> + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
212 | G2 : std::ops::$trait_assign<F> + Clone, { |
0 | 213 | #[inline] |
214 | fn $fn_assign(&mut self, t : F) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
215 | Arc::make_mut(&mut self.0).$fn_assign(t); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
216 | Arc::make_mut(&mut self.1).$fn_assign(t); |
0 | 217 | } |
218 | } | |
219 | ||
220 | impl<'a, F : Float, G1, G2> | |
221 | std::ops::$trait<F> | |
222 | for &'a BothGenerators<G1, G2> | |
223 | where &'a G1 : std::ops::$trait<F,Output=G1>, | |
224 | &'a G2 : std::ops::$trait<F,Output=G2> { | |
225 | type Output = BothGenerators<G1, G2>; | |
226 | #[inline] | |
227 | fn $fn(self, t : F) -> BothGenerators<G1, G2> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
228 | BothGenerators(Arc::new(self.0.$fn(t)), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
229 | Arc::new(self.1.$fn(t))) |
0 | 230 | } |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | make_either_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
236 | make_either_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
237 | ||
238 | impl<G1, G2> std::ops::Neg for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
239 | where G1 : std::ops::Neg + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
240 | G2 : std::ops::Neg + Clone, { |
0 | 241 | type Output = BothGenerators<G1::Output, G2::Output>; |
242 | #[inline] | |
243 | fn neg(self) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
244 | BothGenerators(Arc::new(Arc::unwrap_or_clone(self.0).neg()), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
245 | Arc::new(Arc::unwrap_or_clone(self.1).neg())) |
0 | 246 | } |
247 | } | |
248 | /* | |
249 | impl<'a, G1, G2> std::ops::Neg for &'a BothGenerators<G1, G2> | |
250 | where &'a G1 : std::ops::Neg, &'a G2 : std::ops::Neg, { | |
251 | type Output = BothGenerators<<&'a G1 as std::ops::Neg>::Output, | |
252 | <&'a G2 as std::ops::Neg>::Output>; | |
253 | fn neg(self) -> Self::Output { | |
254 | BothGenerators(self.0.neg(), self.1.neg()) | |
255 | } | |
256 | } | |
257 | */ |