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