1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
//! Starts a timely dataflow execution from configuration information and per-worker logic.
use crate::communication::{initialize_from, Allocator, allocator::AllocateBuilder, WorkerGuards};
use crate::dataflow::scopes::Child;
use crate::worker::Worker;
use crate::{CommunicationConfig, WorkerConfig};
/// Configures the execution of a timely dataflow computation.
#[derive(Clone, Debug)]
pub struct Config {
/// Configuration for the communication infrastructure.
pub communication: CommunicationConfig,
/// Configuration for the worker threads.
pub worker: WorkerConfig,
}
impl Config {
/// Installs options into a [getopts_dep::Options] struct that correspond
/// to the parameters in the configuration.
///
/// It is the caller's responsibility to ensure that the installed options
/// do not conflict with any other options that may exist in `opts`, or
/// that may be installed into `opts` in the future.
///
/// This method is only available if the `getopts` feature is enabled, which
/// it is by default.
#[cfg(feature = "getopts")]
pub fn install_options(opts: &mut getopts_dep::Options) {
CommunicationConfig::install_options(opts);
WorkerConfig::install_options(opts);
}
/// Instantiates a configuration based upon the parsed options in `matches`.
///
/// The `matches` object must have been constructed from a
/// [getopts_dep::Options] which contained at least the options installed by
/// [Self::install_options].
///
/// This method is only available if the `getopts` feature is enabled, which
/// it is by default.
#[cfg(feature = "getopts")]
pub fn from_matches(matches: &getopts_dep::Matches) -> Result<Config, String> {
Ok(Config {
communication: CommunicationConfig::from_matches(matches)?,
worker: WorkerConfig::from_matches(matches)?,
})
}
/// Constructs a new configuration by parsing the supplied text arguments.
///
/// Most commonly, callers supply `std::env::args()` as the iterator.
#[cfg(feature = "getopts")]
pub fn from_args<I: Iterator<Item=String>>(args: I) -> Result<Config, String> {
let mut opts = getopts_dep::Options::new();
Config::install_options(&mut opts);
let matches = opts.parse(args).map_err(|e| e.to_string())?;
Config::from_matches(&matches)
}
/// Constructs a `Config` that uses one worker thread and the
/// defaults for all other parameters.
pub fn thread() -> Config {
Config {
communication: CommunicationConfig::Thread,
worker: WorkerConfig::default(),
}
}
/// Constructs an `Config` that uses `n` worker threads and the
/// defaults for all other parameters.
pub fn process(n: usize) -> Config {
Config {
communication: CommunicationConfig::Process(n),
worker: WorkerConfig::default(),
}
}
}
/// Executes a single-threaded timely dataflow computation.
///
/// The `example` method takes a closure on a `Scope` which it executes to initialize and run a
/// timely dataflow computation on a single thread. This method is intended for use in examples,
/// rather than programs that may need to run across multiple workers.
///
/// The `example` method returns whatever the single worker returns from its closure.
/// This is often nothing, but the worker can return something about the data it saw in order to
/// test computations.
///
/// The method aggressively unwraps returned `Result<_>` types.
///
/// # Examples
///
/// The simplest example creates a stream of data and inspects it.
///
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect};
///
/// timely::example(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x));
/// });
/// ```
///
/// This next example captures the data and displays them once the computation is complete.
///
/// More precisely, the example captures a stream of events (receiving batches of data,
/// updates to input capabilities) and displays these events.
///
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect, Capture};
/// use timely::dataflow::operators::capture::Extract;
///
/// let data = timely::example(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x))
/// .capture()
/// });
///
/// // the extracted data should have data (0..10) at timestamp 0.
/// assert_eq!(data.extract()[0].1, (0..10).collect::<Vec<_>>());
/// ```
pub fn example<T, F>(func: F) -> T
where
T: Send+'static,
F: FnOnce(&mut Child<Worker<crate::communication::allocator::thread::Thread>,u64>)->T+Send+Sync+'static
{
crate::execute::execute_directly(|worker| worker.dataflow(|scope| func(scope)))
}
/// Executes a single-threaded timely dataflow computation.
///
/// The `execute_directly` constructs a `Worker` and directly executes the supplied
/// closure to construct and run a timely dataflow computation. It does not create any
/// worker threads, and simply uses the current thread of control.
///
/// The closure may return a result, which will be returned from the computation.
///
/// # Examples
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect};
///
/// // execute a timely dataflow using three worker threads.
/// timely::execute_directly(|worker| {
/// worker.dataflow::<(),_,_>(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x));
/// })
/// });
/// ```
pub fn execute_directly<T, F>(func: F) -> T
where
T: Send+'static,
F: FnOnce(&mut Worker<crate::communication::allocator::thread::Thread>)->T+Send+Sync+'static
{
let alloc = crate::communication::allocator::thread::Thread::default();
let mut worker = crate::worker::Worker::new(WorkerConfig::default(), alloc);
let result = func(&mut worker);
while worker.has_dataflows() {
worker.step_or_park(None);
}
result
}
/// Executes a timely dataflow from a configuration and per-communicator logic.
///
/// The `execute` method takes a `Configuration` and spins up some number of
/// workers threads, each of which execute the supplied closure to construct
/// and run a timely dataflow computation.
///
/// The closure may return a `T: Send+'static`. The `execute` method returns
/// immediately after initializing the timely computation with a result
/// containing a `WorkerGuards<T>` (or error information), which can be joined
/// to recover the result `T` values from the local workers.
///
/// *Note*: if the caller drops the result of `execute`, the drop code will
/// block awaiting the completion of the timely computation. If the result
/// of the method is not captured it will be dropped, which gives the experience
/// of `execute` blocking; to regain control after `execute` be sure to
/// capture the results and drop them only when the calling thread has no
/// other work to perform.
///
/// # Examples
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect};
///
/// // execute a timely dataflow using three worker threads.
/// timely::execute(timely::Config::process(3), |worker| {
/// worker.dataflow::<(),_,_>(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x));
/// })
/// }).unwrap();
/// ```
///
/// The following example demonstrates how one can extract data from a multi-worker execution.
/// In a multi-process setting, each process will only receive those records present at workers
/// in the process.
///
/// ```rust
/// use std::sync::{Arc, Mutex};
/// use timely::dataflow::operators::{ToStream, Inspect, Capture};
/// use timely::dataflow::operators::capture::Extract;
///
/// // get send and recv endpoints, wrap send to share
/// let (send, recv) = ::std::sync::mpsc::channel();
/// let send = Arc::new(Mutex::new(send));
///
/// // execute a timely dataflow using three worker threads.
/// timely::execute(timely::Config::process(3), move |worker| {
/// let send = send.lock().unwrap().clone();
/// worker.dataflow::<(),_,_>(move |scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x))
/// .capture_into(send);
/// });
/// }).unwrap();
///
/// // the extracted data should have data (0..10) thrice at timestamp 0.
/// assert_eq!(recv.extract()[0].1, (0..30).map(|x| x / 3).collect::<Vec<_>>());
/// ```
pub fn execute<T, F>(config: Config, func: F) -> Result<WorkerGuards<T>,String>
where
T:Send+'static,
F: Fn(&mut Worker<Allocator>)->T+Send+Sync+'static,
{
let (allocators, other) = config.communication.try_build()?;
execute_from(allocators, other, config.worker, func)
}
/// Executes a timely dataflow from supplied arguments and per-communicator logic.
///
/// The `execute` method takes arguments (typically `std::env::args()`) and spins up some number of
/// workers threads, each of which execute the supplied closure to construct and run a timely
/// dataflow computation.
///
/// The closure may return a `T: Send+'static`. The `execute_from_args` method
/// returns immediately after initializing the timely computation with a result
/// containing a `WorkerGuards<T>` (or error information), which can be joined
/// to recover the result `T` values from the local workers.
///
/// *Note*: if the caller drops the result of `execute_from_args`, the drop code
/// will block awaiting the completion of the timely computation.
///
/// The arguments `execute_from_args` currently understands are:
///
/// `-w, --workers`: number of per-process worker threads.
///
/// `-n, --processes`: number of processes involved in the computation.
///
/// `-p, --process`: identity of this process; from 0 to n-1.
///
/// `-h, --hostfile`: a text file whose lines are "hostname:port" in order of process identity.
/// If not specified, `localhost` will be used, with port numbers increasing from 2101 (chosen
/// arbitrarily).
///
/// This method is only available if the `getopts` feature is enabled, which
/// it is by default.
///
/// # Examples
///
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect};
///
/// // execute a timely dataflow using command line parameters
/// timely::execute_from_args(std::env::args(), |worker| {
/// worker.dataflow::<(),_,_>(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x));
/// })
/// }).unwrap();
/// ```
/// ```ignore
/// host0% cargo run -- -w 2 -n 4 -h hosts.txt -p 0
/// host1% cargo run -- -w 2 -n 4 -h hosts.txt -p 1
/// host2% cargo run -- -w 2 -n 4 -h hosts.txt -p 2
/// host3% cargo run -- -w 2 -n 4 -h hosts.txt -p 3
/// ```
/// ```ignore
/// % cat hosts.txt
/// host0:port
/// host1:port
/// host2:port
/// host3:port
/// ```
#[cfg(feature = "getopts")]
pub fn execute_from_args<I, T, F>(iter: I, func: F) -> Result<WorkerGuards<T>,String>
where I: Iterator<Item=String>,
T:Send+'static,
F: Fn(&mut Worker<Allocator>)->T+Send+Sync+'static, {
let config = Config::from_args(iter)?;
execute(config, func)
}
/// Executes a timely dataflow from supplied allocators and logging.
///
/// Refer to [`execute`](execute()) for more details.
///
/// ```rust
/// use timely::dataflow::operators::{ToStream, Inspect};
/// use timely::WorkerConfig;
///
/// // execute a timely dataflow using command line parameters
/// let (builders, other) = timely::CommunicationConfig::Process(3).try_build().unwrap();
/// timely::execute::execute_from(builders, other, WorkerConfig::default(), |worker| {
/// worker.dataflow::<(),_,_>(|scope| {
/// (0..10).to_stream(scope)
/// .inspect(|x| println!("seen: {:?}", x));
/// })
/// }).unwrap();
/// ```
pub fn execute_from<A, T, F>(
builders: Vec<A>,
others: Box<dyn ::std::any::Any+Send>,
worker_config: WorkerConfig,
func: F,
) -> Result<WorkerGuards<T>, String>
where
A: AllocateBuilder+'static,
T: Send+'static,
F: Fn(&mut Worker<<A as AllocateBuilder>::Allocator>)->T+Send+Sync+'static {
initialize_from(builders, others, move |allocator| {
let mut worker = Worker::new(worker_config.clone(), allocator);
let result = func(&mut worker);
while worker.has_dataflows() {
worker.step_or_park(None);
}
result
})
}