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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.!
//! # partopo
//!
//! Execute work described by a dependency graph using either a single-threaded worker or in
//! parallel using a threadpool.
//!
//! ## Usage
//!
//! First, create a [`DependencyDag<T>`] dependency graph using API's from [`daggy::stable_dag::StableDag`]:
//!
//! ```rust
//! use partopo::{DependencyDag, Node};
//!
//! // Construct a DAG:
//! // 1 -> 2
//! // 1 -> 3
//! // 4 -> 5
//! // 2 -> 5
//!
//! let mut dag: DependencyDag<usize> = DependencyDag::new();
//! let idx1 = dag.add_node(Node::new(1));
//! let (_, idx2) = dag.add_child(idx1, (), Node::new(2));
//! let (_, _idx3) = dag.add_child(idx1, (), Node::new(3));
//! let idx4 = dag.add_node(Node::new(4));
//! let (_, idx5) = dag.add_child(idx4, (), Node::new(5));
//! dag.add_edge(idx2, idx5, ()).unwrap();
//! ```
//!
//! Run through the graph using a single-threaded worker:
//!
//! ```rust
//! # use partopo::{DependencyDag, Node};
//! # let mut dag: DependencyDag<usize> = DependencyDag::new();
//! # let idx1 = dag.add_node(Node::new(1));
//! # let (_, idx2) = dag.add_child(idx1, (), Node::new(2));
//! # let (_, _idx3) = dag.add_child(idx1, (), Node::new(3));
//! # let idx4 = dag.add_node(Node::new(4));
//! # let (_, idx5) = dag.add_child(idx4, (), Node::new(5));
//! # dag.add_edge(idx2, idx5, ()).unwrap();
//! fn do_work(data: usize) {
//! println!("{}", data);
//! }
//!
//! partopo::execute(dag, do_work);
//! ```
//!
//! The same example can be adapted to run on multiple threads:
//!
//! ```rust
//! # use partopo::{DependencyDag, Node};
//! # let mut dag: DependencyDag<usize> = DependencyDag::new();
//! # let idx1 = dag.add_node(Node::new(1));
//! # let (_, idx2) = dag.add_child(idx1, (), Node::new(2));
//! # let (_, _idx3) = dag.add_child(idx1, (), Node::new(3));
//! # let idx4 = dag.add_node(Node::new(4));
//! # let (_, idx5) = dag.add_child(idx4, (), Node::new(5));
//! # dag.add_edge(idx2, idx5, ()).unwrap();
//! # fn do_work(data: usize) {
//! # println!("{}", data);
//! # }
//! partopo::par_execute(dag, do_work);
//! ```
pub use daggy;
pub use petgraph;
use ;
use Direction;
use VecDeque;
/// Container for the directed acyclic graph that represent data and their dependencies.
///
/// This is a wrapper type around [`StableDag`](daggy::stable_dag::StableDag) with no edge weights
/// Using a `StableDag` ensures that we can safely remove nodes without invalidating `NodeIndex`'s
pub type DependencyDag<T> = ;
/// A node in the [`DependencyDag<T>`] dependency graph.
///
/// The node contains data to be used by the work function passed to [`execute`] or [`par_execute`].
/// Returns whether a node in a graph has no remaining dependencies
/// Remove processed node from graph and return the nodes that are newly-available
+ '_
/// Return nodes in graph that do not have any dependencies
+ '_
/// Run the given work function on each node in the dependency graph using a single thread.
///
/// The nodes are processed in an order that respects the dependencies.
/// The current implementation uses a topological sort via [Kahn's algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm>).
/// Run the given work function on each node in the dependency graph.
///
/// The nodes are processed in an order that respects the dependencies. The work is parallelized on
/// a threadpool sized to the number of CPU's on the system.
/// The current implementation uses a parallel version of [Kahn's algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm).