differential_dataflow/algorithms/graphs/
propagate.rs

1//! Directed label reachability.
2
3use std::hash::Hash;
4
5use timely::dataflow::*;
6
7use crate::{Collection, ExchangeData};
8use crate::lattice::Lattice;
9use crate::difference::{Abelian, Multiply};
10use crate::operators::arrange::arrangement::ArrangeByKey;
11
12/// Propagates labels forward, retaining the minimum label.
13///
14/// This algorithm naively propagates all labels at once, much like standard label propagation.
15/// To more carefully control the label propagation, consider `propagate_core` which supports a
16/// method to limit the introduction of labels.
17pub fn propagate<G, N, L, R>(edges: &Collection<G, (N,N), R>, nodes: &Collection<G,(N,L),R>) -> Collection<G,(N,L),R>
18where
19    G: Scope<Timestamp: Lattice+Ord>,
20    N: ExchangeData+Hash,
21    R: ExchangeData+Abelian,
22    R: Multiply<R, Output=R>,
23    R: From<i8>,
24    L: ExchangeData,
25{
26    propagate_core(&edges.arrange_by_key(), nodes, |_label| 0)
27}
28
29/// Propagates labels forward, retaining the minimum label.
30///
31/// This algorithm naively propagates all labels at once, much like standard label propagation.
32/// To more carefully control the label propagation, consider `propagate_core` which supports a
33/// method to limit the introduction of labels.
34pub fn propagate_at<G, N, L, F, R>(edges: &Collection<G, (N,N), R>, nodes: &Collection<G,(N,L),R>, logic: F) -> Collection<G,(N,L),R>
35where
36    G: Scope<Timestamp: Lattice+Ord>,
37    N: ExchangeData+Hash,
38    R: ExchangeData+Abelian,
39    R: Multiply<R, Output=R>,
40    R: From<i8>,
41    L: ExchangeData,
42    F: Fn(&L)->u64+Clone+'static,
43{
44    propagate_core(&edges.arrange_by_key(), nodes, logic)
45}
46
47use crate::trace::TraceReader;
48use crate::operators::arrange::arrangement::Arranged;
49
50/// Propagates labels forward, retaining the minimum label.
51///
52/// This variant takes a pre-arranged edge collection, to facilitate re-use, and allows
53/// a method `logic` to specify the rounds in which we introduce various labels. The output
54/// of `logic should be a number in the interval [0,64],
55pub fn propagate_core<G, N, L, Tr, F, R>(edges: &Arranged<G,Tr>, nodes: &Collection<G,(N,L),R>, logic: F) -> Collection<G,(N,L),R>
56where
57    G: Scope<Timestamp=Tr::Time>,
58    N: ExchangeData+Hash,
59    R: ExchangeData+Abelian,
60    R: Multiply<R, Output=R>,
61    R: From<i8>,
62    L: ExchangeData,
63    Tr: for<'a> TraceReader<Key<'a>=&'a N, Val<'a>=&'a N, Diff=R>+Clone+'static,
64    F: Fn(&L)->u64+Clone+'static,
65{
66    // Morally the code performs the following iterative computation. However, in the interest of a simplified
67    // dataflow graph and reduced memory footprint we instead have a wordier version below. The core differences
68    // between the two are that 1. the former filters its input and pretends to perform non-monotonic computation,
69    // whereas the latter creates an initially empty monotonic iteration variable, and 2. the latter rotates the
70    // iterative computation so that the arrangement produced by `reduce` can be re-used.
71
72    // nodes.filter(|_| false)
73    //      .iterate(|inner| {
74    //          let edges = edges.enter(&inner.scope());
75    //          let nodes = nodes.enter_at(&inner.scope(), move |r| 256 * (64 - (logic(&r.1)).leading_zeros() as u64));
76    //          inner.join_map(&edges, |_k,l,d| (d.clone(),l.clone()))
77    //               .concat(&nodes)
78    //               .reduce(|_, s, t| t.push((s[0].0.clone(), 1)))
79    //      })
80
81    nodes.scope().iterative::<usize,_,_>(|scope| {
82
83        use crate::operators::reduce::ReduceCore;
84        use crate::operators::iterate::SemigroupVariable;
85        use crate::trace::implementations::{ValBuilder, ValSpine};
86
87        use timely::order::Product;
88
89        let edges = edges.enter(scope);
90        let nodes = nodes.enter_at(scope, move |r| 256 * (64 - (logic(&r.1)).leading_zeros() as usize));
91
92        let proposals = SemigroupVariable::new(scope, Product::new(Default::default(), 1usize));
93
94        let labels =
95        proposals
96            .concat(&nodes)
97            .reduce_abelian::<_,ValBuilder<_,_,_,_>,ValSpine<_,_,_,_>>("Propagate", |_, s, t| t.push((s[0].0.clone(), R::from(1_i8))));
98
99        let propagate: Collection<_, (N, L), R> =
100        labels
101            .join_core(&edges, |_k, l: &L, d| Some((d.clone(), l.clone())));
102
103        proposals.set(&propagate);
104
105        labels
106            .as_collection(|k,v| (k.clone(), v.clone()))
107            .leave()
108    })
109}