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