hyperactor/
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//! Hyperactor is an actor system intended for managing large scale compute.
10//!
11//! # Actor data model
12//!
13//! Hyperactor is designed to support large scale (millions of nodes)
14//! machine learning workloads where actor topologies communicate through
15//! high fanout multicast messaging.
16//!
17//! Supporting this scale requires us to impose additional structure
18//! at the level of the framework, so that we can efficiently refer to
19//! _gangs_ of actors that implement the same worker runtimes.
20//!
21//! Similarly, Hyperactor must gang-schedule actors in order to support
22//! collective communicaton between actors.
23//!
24//! Hyperactor is organized into a hierarchy, wherein parents manage
25//! the lifecycle of their children:
26//!
27//! * Each _world_ represents a fixed number of _procs_, scheduled as
28//!   a gang.
29//! * Each _proc_ represents a single actor runtime instance, and hosts
30//!   zero or more actors.
31//! * Actors are _spawned_ into worlds, and assigned a global name.
32//!   Actors spawned in this way are assigned a local PID (pid) of 0.
33//!   Actors in turn can spawn local actors. These inherit the global pid
34//!   of their parent, but receive a unique pid.
35//!
36//! Actors that share a name within a world are called a _gang_.
37//!
38//! This scheme confers several benefits:
39//!
40//! * Routing of messages can be performed by prefix. For example, we
41//!   can route a message to an actor based on the world the actor belongs
42//!   to; from there, we can identify the _proc_ of the actor and send
43//!   the message to it, which can then in turn be routed locally.
44//!
45//! * We can represent gangs of actors in a uniform and compact way.
46//!   This is the basis on which we implement efficient multicasting
47//!   within the system.
48//!
49//!
50//! | Entity    | Identifier                |
51//! |-----------|---------------------------|
52//! | World     | `worldid`                 |
53//! | Proc      | `worldid[rank]`           |
54//! | Actor     | `worldid[rank].name[pid]` |
55//! | Gang      | `worldid.name`            |
56
57#![feature(anonymous_lifetime_in_impl_trait)]
58#![feature(assert_matches)]
59#![feature(associated_type_defaults)]
60#![feature(box_patterns)]
61#![feature(btree_cursors)]
62#![feature(error_reporter)]
63#![feature(impl_trait_in_assoc_type)]
64#![feature(never_type)]
65#![feature(panic_update_hook)]
66#![feature(type_alias_impl_trait)]
67#![feature(trait_alias)]
68#![deny(missing_docs)]
69
70pub mod accum;
71pub mod actor;
72pub mod channel;
73pub mod checkpoint;
74pub mod clock;
75pub mod config;
76pub mod context;
77pub mod host;
78mod init;
79pub mod mailbox;
80pub mod message;
81pub mod metrics;
82mod ordering;
83pub mod panic_handler;
84pub mod proc;
85pub mod reference;
86mod signal_handler;
87pub mod simnet;
88mod stdio_redirect;
89pub mod supervision;
90pub mod sync;
91/// Test utilities
92pub mod test_utils;
93pub mod time;
94
95pub use actor::Actor;
96pub use actor::ActorHandle;
97pub use actor::Handler;
98pub use actor::RemoteHandles;
99pub use actor::RemoteSpawn;
100// Re-export public dependencies of hyperactor_macros codegen.
101#[doc(hidden)]
102pub use anyhow;
103#[doc(hidden)]
104pub use async_trait;
105#[doc(inline)]
106pub use hyperactor_macros::Bind;
107#[doc(inline)]
108pub use hyperactor_macros::HandleClient;
109#[doc(inline)]
110pub use hyperactor_macros::Handler;
111#[doc(inline)]
112pub use hyperactor_macros::RefClient;
113#[doc(inline)]
114pub use hyperactor_macros::Unbind;
115#[doc(inline)]
116pub use hyperactor_macros::behavior;
117#[doc(inline)]
118pub use hyperactor_macros::export;
119#[doc(inline)]
120pub use hyperactor_macros::forward;
121#[doc(inline)]
122pub use hyperactor_macros::instrument;
123#[doc(inline)]
124pub use hyperactor_macros::instrument_infallible;
125pub use hyperactor_macros::observe_async;
126pub use hyperactor_macros::observe_result;
127pub use hyperactor_telemetry::declare_static_counter;
128pub use hyperactor_telemetry::declare_static_gauge;
129pub use hyperactor_telemetry::declare_static_histogram;
130pub use hyperactor_telemetry::declare_static_timer;
131pub use hyperactor_telemetry::key_value;
132pub use hyperactor_telemetry::kv_pairs;
133#[doc(inline)]
134pub use init::initialize;
135#[doc(inline)]
136pub use init::initialize_with_current_runtime;
137#[doc(inline)]
138pub use init::initialize_with_log_prefix;
139#[doc(hidden)]
140pub use inventory; // For remote! macro
141pub use mailbox::Data;
142pub use mailbox::Mailbox;
143pub use mailbox::Message;
144pub use mailbox::OncePortHandle;
145pub use mailbox::PortHandle;
146pub use mailbox::RemoteMessage;
147// Re-exported to support opentelemetry in hyperactor_macros codegen.
148#[doc(hidden)]
149pub use opentelemetry;
150#[doc(hidden)]
151pub use paste::paste;
152pub use proc::Context;
153pub use proc::Instance;
154pub use proc::Proc;
155pub use reference::ActorId;
156pub use reference::ActorRef;
157pub use reference::GangId;
158pub use reference::GangRef;
159pub use reference::OncePortRef;
160pub use reference::PortId;
161pub use reference::PortRef;
162pub use reference::ProcId;
163pub use reference::WorldId;
164// Re-exported to support tracing in hyperactor_macros codegen.
165#[doc(hidden)]
166pub use serde_json;
167#[doc(inline)]
168pub use signal_handler::SignalCleanupGuard;
169#[doc(inline)]
170pub use signal_handler::SignalDisposition;
171#[doc(inline)]
172pub use signal_handler::query_signal_disposition;
173#[doc(inline)]
174pub use signal_handler::register_signal_cleanup;
175#[doc(inline)]
176pub use signal_handler::register_signal_cleanup_scoped;
177#[doc(inline)]
178pub use signal_handler::sigpipe_disposition;
179#[doc(inline)]
180pub use signal_handler::unregister_signal_cleanup;
181// Re-exported to support tracing in hyperactor_macros codegen.
182#[doc(hidden)]
183pub use tracing;
184#[doc(hidden)]
185pub use typeuri; // For declare_attrs! macro
186
187mod private {
188    /// Public trait in a private module for sealing traits within this crate:
189    /// [Sealed trait pattern](https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed).
190    pub trait Sealed {}
191
192    // These two implement context capabilities:
193    impl<A: crate::Actor> Sealed for crate::proc::Instance<A> {}
194    impl<A: crate::Actor> Sealed for &crate::proc::Instance<A> {}
195    impl<A: crate::Actor> Sealed for crate::proc::Context<'_, A> {}
196    impl<A: crate::Actor> Sealed for &crate::proc::Context<'_, A> {}
197    impl Sealed for crate::mailbox::Mailbox {}
198    impl Sealed for &crate::mailbox::Mailbox {}
199}