hyperactor_mesh/
lib.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! This crate provides hyperactor's mesh abstractions.
10
11#![feature(assert_matches)]
12#![feature(exit_status_error)]
13#![feature(impl_trait_in_bindings)]
14#![feature(get_disjoint_mut_helpers)]
15#![feature(exact_size_is_empty)]
16
17pub mod actor_mesh;
18pub mod alloc;
19mod assign;
20pub mod bootstrap;
21pub mod comm;
22pub mod config;
23pub mod connect;
24pub mod logging;
25pub mod mesh;
26pub mod mesh_selection;
27mod metrics;
28pub mod proc_mesh;
29pub mod reference;
30pub mod resource;
31mod router;
32pub mod shared_cell;
33pub mod shortuuid;
34pub mod test_utils;
35mod testresource;
36pub mod v1;
37
38pub use actor_mesh::RootActorMesh;
39pub use actor_mesh::SlicedActorMesh;
40pub use bootstrap::Bootstrap;
41pub use bootstrap::bootstrap;
42pub use bootstrap::bootstrap_or_die;
43pub use comm::CommActor;
44pub use dashmap;
45pub use hyperactor_mesh_macros::sel;
46pub use mesh::Mesh;
47pub use ndslice::extent;
48pub use ndslice::sel_from_shape;
49pub use ndslice::selection;
50pub use ndslice::shape;
51pub use proc_mesh::ProcMesh;
52pub use proc_mesh::SlicedProcMesh;
53
54#[cfg(test)]
55mod tests {
56
57    #[test]
58    fn basic() {
59        use ndslice::selection::dsl;
60        use ndslice::selection::structurally_equal;
61
62        let actual = sel!(*, 0:4, *);
63        let expected = dsl::all(dsl::range(
64            ndslice::shape::Range(0, Some(4), 1),
65            dsl::all(dsl::true_()),
66        ));
67        assert!(structurally_equal(&actual, &expected));
68    }
69
70    #[cfg(FALSE)]
71    #[test]
72    fn shouldnt_compile() {
73        let _ = sel!(foobar);
74    }
75    // error: sel! parse failed: unexpected token: Ident { sym: foobar, span: #0 bytes(605..611) }
76    //   --> fbcode/monarch/hyperactor_mesh_macros/tests/basic.rs:19:13
77    //    |
78    // 19 |     let _ = sel!(foobar);
79    //    |             ^^^^^^^^^^^^ in this macro invocation
80    //   --> fbcode/monarch/hyperactor_mesh_macros/src/lib.rs:12:1
81    //    |
82    //    = note: in this expansion of `sel!`
83
84    use hyperactor_mesh_macros::sel;
85    use ndslice::assert_round_trip;
86    use ndslice::assert_structurally_eq;
87    use ndslice::selection::Selection;
88
89    macro_rules! assert_round_trip_match {
90        ($left:expr, $right:expr) => {{
91            assert_structurally_eq!($left, $right);
92            assert_round_trip!($left);
93            assert_round_trip!($right);
94        }};
95    }
96
97    #[test]
98    fn token_parser() {
99        use ndslice::selection::dsl::*;
100        use ndslice::shape;
101
102        assert_round_trip_match!(all(true_()), sel!(*));
103        assert_round_trip_match!(range(3, true_()), sel!(3));
104        assert_round_trip_match!(range(1..4, true_()), sel!(1:4));
105        assert_round_trip_match!(all(range(1..4, true_())), sel!(*, 1:4));
106        assert_round_trip_match!(range(shape::Range(0, None, 1), true_()), sel!(:));
107        assert_round_trip_match!(any(true_()), sel!(?));
108        assert_round_trip_match!(any(range(1..4, all(true_()))), sel!(?, 1:4, *));
109        assert_round_trip_match!(union(range(0, true_()), range(1, true_())), sel!(0 | 1));
110        assert_round_trip_match!(
111            intersection(range(0..4, true_()), range(2..6, true_())),
112            sel!(0:4 & 2:6)
113        );
114        assert_round_trip_match!(range(shape::Range(0, None, 1), true_()), sel!(:));
115        assert_round_trip_match!(all(true_()), sel!(*));
116        assert_round_trip_match!(any(true_()), sel!(?));
117        assert_round_trip_match!(all(all(all(true_()))), sel!(*, *, *));
118        assert_round_trip_match!(intersection(all(true_()), all(true_())), sel!(* & *));
119        assert_round_trip_match!(
120            all(all(union(
121                range(0..2, true_()),
122                range(shape::Range(6, None, 1), true_())
123            ))),
124            sel!(*, *, (:2|6:))
125        );
126        assert_round_trip_match!(
127            all(all(range(shape::Range(1, None, 2), true_()))),
128            sel!(*, *, 1::2)
129        );
130        assert_round_trip_match!(
131            range(
132                shape::Range(0, Some(1), 1),
133                any(range(shape::Range(0, Some(4), 1), true_()))
134            ),
135            sel!(0, ?, :4)
136        );
137        assert_round_trip_match!(range(shape::Range(1, Some(4), 2), true_()), sel!(1:4:2));
138        assert_round_trip_match!(range(shape::Range(0, None, 2), true_()), sel!(::2));
139        assert_round_trip_match!(
140            union(range(0..4, true_()), range(4..8, true_())),
141            sel!(0:4 | 4:8)
142        );
143        assert_round_trip_match!(
144            intersection(range(0..4, true_()), range(2..6, true_())),
145            sel!(0:4 & 2:6)
146        );
147        assert_round_trip_match!(
148            all(union(range(1..4, all(true_())), range(5..6, all(true_())))),
149            sel!(*, (1:4 | 5:6), *)
150        );
151        assert_round_trip_match!(
152            range(
153                0,
154                intersection(
155                    range(1..4, range(7, true_())),
156                    range(2..5, range(7, true_()))
157                )
158            ),
159            sel!(0, (1:4 & 2:5), 7)
160        );
161        assert_round_trip_match!(
162            all(all(union(
163                union(range(0..2, true_()), range(4..6, true_())),
164                range(shape::Range(6, None, 1), true_())
165            ))),
166            sel!(*, *, (:2 | 4:6 | 6:))
167        );
168        assert_round_trip_match!(intersection(all(true_()), all(true_())), sel!(* & *));
169        assert_round_trip_match!(union(all(true_()), all(true_())), sel!(* | *));
170        assert_round_trip_match!(
171            intersection(
172                range(0..2, true_()),
173                union(range(1, true_()), range(2, true_()))
174            ),
175            sel!(0:2 & (1 | 2))
176        );
177        assert_round_trip_match!(
178            all(all(intersection(
179                range(1..2, true_()),
180                range(2..3, true_())
181            ))),
182            sel!(*,*,(1:2&2:3))
183        );
184        assert_round_trip_match!(
185            intersection(all(all(all(true_()))), all(all(all(true_())))),
186            sel!((*,*,*) & (*,*,*))
187        );
188        assert_round_trip_match!(
189            intersection(
190                range(0, all(all(true_()))),
191                range(0, union(range(1, all(true_())), range(3, all(true_()))))
192            ),
193            sel!((0, *, *) & (0, (1 | 3), *))
194        );
195        assert_round_trip_match!(
196            intersection(
197                range(0, all(all(true_()))),
198                range(
199                    0,
200                    union(
201                        range(1, range(2..5, true_())),
202                        range(3, range(2..5, true_()))
203                    )
204                )
205            ),
206            sel!((0, *, *) & (0, (1 | 3), 2:5))
207        );
208        assert_round_trip_match!(all(true_()), sel!((*)));
209        assert_round_trip_match!(range(1..4, range(2, true_())), sel!(((1:4), 2)));
210        assert_round_trip_match!(sel!(1:4 & 5:6 | 7:8), sel!((1:4 & 5:6) | 7:8));
211        assert_round_trip_match!(
212            union(
213                intersection(all(all(true_())), all(all(true_()))),
214                all(all(true_()))
215            ),
216            sel!((*,*) & (*,*) | (*,*))
217        );
218        assert_round_trip_match!(all(true_()), sel!(*));
219        assert_round_trip_match!(sel!(((1:4))), sel!(1:4));
220        assert_round_trip_match!(sel!(*, (*)), sel!(*, *));
221        assert_round_trip_match!(
222            intersection(
223                range(0, range(1..4, true_())),
224                range(0, union(range(2, all(true_())), range(3, all(true_()))))
225            ),
226            sel!((0,1:4)&(0,(2|3),*))
227        );
228
229        //assert_round_trip_match!(true_(), sel!(foo)); // sel! macro: parse error: Parsing Error: Error { input: "foo", code: Tag }
230
231        assert_round_trip_match!(
232            sel!(0 & (0, (1|3), *)),
233            intersection(
234                range(0, true_()),
235                range(0, union(range(1, all(true_())), range(3, all(true_()))))
236            )
237        );
238        assert_round_trip_match!(
239            sel!(0 & (0, (3|1), *)),
240            intersection(
241                range(0, true_()),
242                range(0, union(range(3, all(true_())), range(1, all(true_()))))
243            )
244        );
245        assert_round_trip_match!(
246            sel!((*, *, *) & (*, *, (2 | 4))),
247            intersection(
248                all(all(all(true_()))),
249                all(all(union(range(2, true_()), range(4, true_()))))
250            )
251        );
252        assert_round_trip_match!(
253            sel!((*, *, *) & (*, *, (4 | 2))),
254            intersection(
255                all(all(all(true_()))),
256                all(all(union(range(4, true_()), range(2, true_()))))
257            )
258        );
259        assert_round_trip_match!(
260            sel!((*, (1|2)) & (*, (2|1))),
261            intersection(
262                all(union(range(1, true_()), range(2, true_()))),
263                all(union(range(2, true_()), range(1, true_())))
264            )
265        );
266        assert_round_trip_match!(
267            sel!((*, *, *) & *),
268            intersection(all(all(all(true_()))), all(true_()))
269        );
270        assert_round_trip_match!(
271            sel!(* & (*, *, *)),
272            intersection(all(true_()), all(all(all(true_()))))
273        );
274
275        assert_round_trip_match!(
276            sel!( (*, *, *) & ((*, *, *) & (*, *, *)) ),
277            intersection(
278                all(all(all(true_()))),
279                intersection(all(all(all(true_()))), all(all(all(true_()))))
280            )
281        );
282        assert_round_trip_match!(
283            sel!((1, *, *) | (0 & (0, 3, *))),
284            union(
285                range(1, all(all(true_()))),
286                intersection(range(0, true_()), range(0, range(3, all(true_()))))
287            )
288        );
289        assert_round_trip_match!(
290            sel!(((0, *)| (1, *)) & ((1, *) | (0, *))),
291            intersection(
292                union(range(0, all(true_())), range(1, all(true_()))),
293                union(range(1, all(true_())), range(0, all(true_())))
294            )
295        );
296        assert_round_trip_match!(sel!(*, 8:8), all(range(8..8, true_())));
297        assert_round_trip_match!(
298            sel!((*, 1) & (*, 8 : 8)),
299            intersection(all(range(1..2, true_())), all(range(8..8, true_())))
300        );
301        assert_round_trip_match!(
302            sel!((*, 8 : 8) | (*, 1)),
303            union(all(range(8..8, true_())), all(range(1..2, true_())))
304        );
305        assert_round_trip_match!(
306            sel!((*, 1) | (*, 2:8)),
307            union(all(range(1..2, true_())), all(range(2..8, true_())))
308        );
309        assert_round_trip_match!(
310            sel!((*, *, *) & (*, *, 2:8)),
311            intersection(all(all(all(true_()))), all(all(range(2..8, true_()))))
312        );
313    }
314}