summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 3373ed81e09f29feb62a72526d42718fd7b31b57 (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Parallel sequential iterator.
//!
//! This crate implements an extension trait [`ParallelIterator`] adding parallel sequential
//! mapping to the standard [`Iterator`] trait.
//!
//! # Example
//!
//! ```
//! use std::time::Duration;
//! use parseq::ParallelIterator;
//!
//! let mut iter = [3,2,1]
//!   .into_iter()
//!   .map_parallel(|i| {
//!     // Insert heavy computation here ...
//!     std::thread::sleep(Duration::from_millis(100*i));
//!     2*i
//!   });
//!
//! assert_eq!(iter.next(), Some(6));
//! assert_eq!(iter.next(), Some(4));
//! assert_eq!(iter.next(), Some(2));
//! assert_eq!(iter.next(), None);
//! ```
//!
//! See the `examples` directory for a real world example.
//!
//! # Features
//!
//! * Parseq utilizes a configurable number of worker threads
//! * Parseq preserves the order of the original iterator
//! * Parseq is lazy in the sense that it doesn't consume from the original iterator before [`next`](`Iterator::next`) is called for the first time
//! * Parseq doesn't [`fuse`](`Iterator::fuse`) the original iterator
//! * Parseq uses constant space: linear in the number of threads and the size of the buffer, not in the length of the possibly infinite original iterator
//! * Parseq propagates panics from the given closure
//!
//! # Alternatives
//!
//! If you don't care about the order of the returned iterator you'll probably want to use
//! [Rayon](https://crates.io/crates/rayon) instead. If you do care about the order, take a look at
//! [Pariter](https://crates.io/crates/pariter). The latter provides more functionality than this
//! crate and predates it.

#![forbid(unsafe_code)]
#![warn(missing_docs)]

use std::{collections::HashMap, iter::FusedIterator, num::NonZeroUsize};

use crossbeam_channel::{Receiver, Select, Sender, TryRecvError};

/// An extension trait adding parallel sequential mapping to the standard [`Iterator`] trait.
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub trait ParallelIterator {
    /// Creates an iterator which applies a given closure to each element in parallel.
    ///
    /// This function is a multi-threaded equivalent of [`Iterator::map`]. It uses up to
    /// [`available_parallelism`](std::thread::available_parallelism) threads and buffers a finite
    /// number of items. Use [`map_parallel_limit`](ParallelIterator) if you want to set
    /// parallelism and space limits.
    ///
    /// The returned iterator
    ///
    /// * preserves the order of the original iterator
    /// * is lazy in the sense that it doesn't consume from the original iterator before [`next`](`Iterator::next`) is called for the first time
    /// * doesn't [`fuse`](`Iterator::fuse`) the original iterator
    /// * uses constant space: linear in `threads` and `buffer_size`, not in the length of the possibly infinite original iterator
    /// * propagates panics from the given closure
    ///
    /// # Example
    ///
    /// ```
    /// use std::time::Duration;
    /// use parseq::ParallelIterator;
    ///
    /// let mut iter = [3,2,1]
    ///   .into_iter()
    ///   .map_parallel(|i| {
    ///     // Insert heavy computation here ...
    ///     std::thread::sleep(Duration::from_millis(100*i));
    ///     2*i
    ///   });
    ///
    /// assert_eq!(iter.next(), Some(6));
    /// assert_eq!(iter.next(), Some(4));
    /// assert_eq!(iter.next(), Some(2));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn map_parallel<B, F>(self, f: F) -> ParallelMap<Self, B>
    where
        Self: Iterator + Sized,
        Self::Item: Send + 'static,
        F: FnMut(Self::Item) -> B + Send + Clone + 'static,
        B: Send + 'static,
    {
        let threads = std::thread::available_parallelism()
            .map(NonZeroUsize::get)
            .unwrap_or(1);
        let buffer_size = threads.saturating_mul(16);
        self.map_parallel_limit(threads, buffer_size, f)
    }

    /// Creates an iterator which applies a given closure to each element in parallel.
    ///
    /// This function is a multi-threaded equivalent of [`Iterator::map`]. It uses up to the given
    /// number of `threads` and buffers up to `buffer_size` items. If `threads` is zero, up to
    /// [`available_parallelism`](std::thread::available_parallelism) threads are used instead. The
    /// `buffer_size` should be greater than the number of threads. A `buffer_size < 2` effectively
    /// results in single-threaded processing.
    ///
    /// The returned iterator
    ///
    /// * preserves the order of the original iterator
    /// * is lazy in the sense that it doesn't consume from the original iterator before [`next`](`Iterator::next`) is called for the first time
    /// * doesn't [`fuse`](`Iterator::fuse`) the original iterator
    /// * uses constant space: linear in `threads` and `buffer_size`, not in the length of the possibly infinite original iterator
    /// * propagates panics from the given closure
    ///
    /// # Example
    ///
    /// ```
    /// use std::time::Duration;
    /// use parseq::ParallelIterator;
    ///
    /// let mut iter = [3,2,1]
    ///   .into_iter()
    ///   .map_parallel_limit(2, 16, |i| {
    ///     std::thread::sleep(Duration::from_millis(100*i));
    ///     2*i
    ///   });
    ///
    /// assert_eq!(iter.next(), Some(6));
    /// assert_eq!(iter.next(), Some(4));
    /// assert_eq!(iter.next(), Some(2));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn map_parallel_limit<B, F>(
        self,
        threads: usize,
        buffer_size: usize,
        f: F,
    ) -> ParallelMap<Self, B>
    where
        Self: Iterator + Sized,
        Self::Item: Send + 'static,
        F: FnMut(Self::Item) -> B + Send + Clone + 'static,
        B: Send + 'static,
    {
        ParallelMap::new(self, threads, buffer_size, f)
    }
}

impl<I> ParallelIterator for I where I: Iterator {}

/// An iterator that maps the elements of another iterator in parallel.
///
/// This struct is created by the [`map_parallel`](ParallelIterator::map_parallel) method on the
/// [`ParallelIterator`] trait.
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ParallelMap<I, B>
where
    I: Iterator,
{
    /// Wrapped iterator.
    iter: I,

    /// Maximum number of in-flight input and output items.
    buffer_size: usize,

    /// Input sender.
    in_tx: Sender<(usize, I::Item)>,

    /// Index of the next input.
    in_i: usize,

    /// Output receiver.
    out_rx: Receiver<(usize, B)>,

    /// Index of the next output.
    out_i: usize,

    /// Worker thread panic receiver.
    panic_rx: Receiver<()>,

    /// Output buffer.
    buf: HashMap<usize, Option<B>>,
}

impl<I, B> ParallelMap<I, B>
where
    I: Iterator,
{
    /// Returns the number of in-flight items: queued input items and buffered output items.
    fn inflight(&self) -> usize {
        if self.in_i >= self.out_i {
            self.in_i - self.out_i
        } else {
            (self.in_i + 1) + (usize::MAX - self.out_i)
        }
    }
}

impl<I, B> ParallelMap<I, B>
where
    I: Iterator,
    I::Item: Send + 'static,
    B: Send + 'static,
{
    fn new<F>(iter: I, threads: usize, buffer_size: usize, f: F) -> Self
    where
        F: FnMut(I::Item) -> B + Send + Clone + 'static,
    {
        let threads = if threads > 0 {
            threads
        } else {
            std::thread::available_parallelism()
                .map(NonZeroUsize::get)
                .unwrap_or(1)
        };
        let buffer_size = if buffer_size > 0 { buffer_size } else { 1 };

        let (in_tx, in_rx) = crossbeam_channel::bounded(buffer_size);
        let (out_tx, out_rx) = crossbeam_channel::bounded(buffer_size);
        let (panic_tx, panic_rx) = crossbeam_channel::bounded(threads);

        for _ in 0..threads {
            let in_rx = in_rx.clone();
            let out_tx = out_tx.clone();
            let panic_tx = panic_tx.clone();
            let mut f = f.clone();

            std::thread::spawn(move || {
                let _foo = Canary::new(|| {
                    panic_tx.send(()).ok(); // avoid nested panic
                });
                for (i, item) in in_rx.into_iter() {
                    out_tx.send((i, (f)(item))).ok(); // fails iff. ParallelMap was dropped
                }
            });
        }

        ParallelMap {
            iter,
            buffer_size,
            in_tx,
            in_i: 0,
            out_rx,
            out_i: 0,
            panic_rx,
            buf: HashMap::new(),
        }
    }
}

impl<I, B> Iterator for ParallelMap<I, B>
where
    I: Iterator,
{
    type Item = B;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            // Send input to workers.
            while self.inflight() < self.buffer_size {
                if let Some(item) = self.iter.next() {
                    self.in_tx.send((self.in_i, item)).unwrap();
                } else {
                    self.buf.insert(self.in_i, None);
                }
                self.in_i = self.in_i.wrapping_add(1);
            }

            // Return requested item from buffer, if available.
            if let Some(item) = self.buf.remove(&self.out_i) {
                self.out_i = self.out_i.wrapping_add(1);
                return item;
            }

            // Wait for new output from workers.
            let mut sel = Select::new();
            sel.recv(&self.out_rx);
            let panic_received = sel.recv(&self.panic_rx);

            if sel.ready() == panic_received && self.panic_rx.try_recv().is_ok() {
                panic!("worker thread panicked");
            }

            // Receive output from workers.
            loop {
                match self.out_rx.try_recv() {
                    Ok((i, item)) => {
                        self.buf.insert(i, Some(item));
                    }
                    Err(TryRecvError::Empty) => break,
                    Err(TryRecvError::Disconnected) => break,
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.iter.size_hint();
        let inflight = self.inflight();
        (
            lower.saturating_add(inflight),
            upper.and_then(|i| i.checked_add(inflight)),
        )
    }
}

impl<I, B> FusedIterator for ParallelMap<I, B> where I: FusedIterator {}

impl<I, B> ExactSizeIterator for ParallelMap<I, B>
where
    I: ExactSizeIterator,
{
    fn len(&self) -> usize {
        self.iter.len() + self.inflight()
    }
}

/// Calls a given closure when the thread unwinds due to a panic.
struct Canary<F: FnMut()> {
    f: F,
}

impl<F: FnMut()> Canary<F> {
    /// Creates a canary with the given closure.
    ///
    /// The closure shouldn't panic. Otherwise the process will be aborted.
    fn new(f: F) -> Self {
        Canary { f }
    }
}

impl<F: FnMut()> Drop for Canary<F> {
    fn drop(&mut self) {
        if std::thread::panicking() {
            (self.f)();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{Arc, Mutex},
        time::Duration,
    };

    use super::*;

    #[test]
    fn map_empty_iterator() {
        assert!(std::iter::empty()
            .map_parallel_limit(5, 7, |i: i32| 2 * i)
            .eq(std::iter::empty()));
    }

    #[test]
    fn map_unit_iterator() {
        assert!(std::iter::once(1)
            .map_parallel_limit(5, 7, |i| 2 * i)
            .eq(std::iter::once(2)));
    }

    #[test]
    fn map_with_multiple_threads() {
        assert!((0..100)
            .map_parallel_limit(5, 7, |i| {
                std::thread::sleep(Duration::from_millis((i % 3) * 10));
                2 * i
            })
            .eq((0..100).map(|i| 2 * i)));
    }

    #[test]
    fn map_with_single_thread() {
        assert!((0..100)
            .map_parallel_limit(1, 7, |i| {
                std::thread::sleep(Duration::from_millis((i % 3) * 10));
                2 * i
            })
            .eq((0..100).map(|i| 2 * i)));
    }

    #[test]
    fn map_with_zero_threads() {
        assert!((0..100)
            .map_parallel_limit(0, 7, |i| {
                std::thread::sleep(Duration::from_millis((i % 3) * 10));
                2 * i
            })
            .eq((0..100).map(|i| 2 * i)));
    }

    #[test]
    fn map_with_zero_buffer_size() {
        assert!((0..100)
            .map_parallel_limit(5, 0, |i| {
                std::thread::sleep(Duration::from_millis((i % 3) * 10));
                2 * i
            })
            .eq((0..100).map(|i| 2 * i)));
    }

    #[test]
    fn map_does_not_fuse() {
        let mut i = 0;
        let mut iter = std::iter::from_fn(move || {
            i += 1;
            if i == 2 {
                None
            } else {
                Some(i)
            }
        })
        .take(3)
        .map_parallel_limit(5, 7, |i| i);

        assert_eq!(iter.next(), Some(1));
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next(), Some(3));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn map_is_lazy() {
        let _iter = (0..10).map_parallel_limit(5, 7, |_| panic!("eager evaluation"));
    }

    #[test]
    #[should_panic]
    #[ntest::timeout(1000)]
    fn map_propagates_panic() {
        let _ = (0..100)
            .map_parallel_limit(5, 7, |i| {
                if i == 13 {
                    panic!("boom");
                } else {
                    i
                }
            })
            .collect::<Vec<_>>();
    }

    #[test]
    fn canary_positive() {
        let (tx, rx) = crossbeam_channel::bounded(1);
        std::thread::spawn(move || {
            let _canary = Canary::new(|| tx.send(()).unwrap());
            panic!("boom");
        });
        assert_eq!(rx.recv_timeout(Duration::from_secs(1)), Ok(()));
    }

    #[test]
    fn canary_negative() {
        let mut panicked = false;
        let canary = Canary::new(|| {
            panicked = true;
        });
        drop(canary);
        assert!(!panicked);
    }

    #[test]
    fn map_wraps_item_indices() {
        let mut map = ParallelMap::new(0..100, 5, 7, |i| {
            std::thread::sleep(Duration::from_millis((i % 3) * 10));
            2 * i
        });

        // Fast forward
        map.in_i = usize::MAX - 13;
        map.out_i = usize::MAX - 13;

        assert!(map.eq((0..100).map(|i| 2 * i)));
    }

    #[test]
    fn inflight() {
        let inflight = |i, j| {
            let mut map = ParallelMap::new(std::iter::empty(), 5, 7, |x: i32| x);
            map.in_i = i;
            map.out_i = j;
            map.inflight()
        };

        assert_eq!(inflight(0, 0), 0);
        assert_eq!(inflight(usize::MAX, 0), usize::MAX);
        assert_eq!(inflight(usize::MAX, usize::MAX), 0);
        assert_eq!(inflight(0, usize::MAX), 1);
        assert_eq!(inflight(17, 13), 4);
        assert_eq!(inflight(13, usize::MAX - 17), 31);
    }

    /// ParallelMap must stop feeding workers when dropped.
    #[test]
    fn drop_parallel_map() {
        let threads = 5;
        let buffer_size = 20;
        let consume = 7;

        let counter = Arc::new(Mutex::new(0));
        let count = counter.clone();

        let mut iter = (0..).map_parallel_limit(threads, buffer_size, move |i| {
            if i < consume {
                std::thread::sleep(Duration::from_millis(100));
            }
            let mut counter = counter.lock().unwrap();
            *counter += 1;
            2 * i
        });
        for _ in 0..consume {
            iter.next();
        }
        drop(iter);
        assert!(*count.lock().unwrap() < consume + buffer_size);
    }
}
Generated by cgit. See skreutz.com for my tech blog and contact information.