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
|
use super::ProblemSolver;
use std::ops::{Deref, DerefMut};
use futures::ready;
use std::future::Future;
use std::pin::Pin;
pub trait AsyncTester {
type Result: Future<Output = Vec<bool>>;
fn test_async(&self, query: Vec<(usize, usize)>) -> Self::Result;
}
pub struct ParallelProblemSolver<T>
where
T: AsyncTester,
{
solver: ProblemSolver,
current_test: Option<(T::Result, Vec<usize>)>,
}
impl<T: AsyncTester> Deref for ParallelProblemSolver<T> {
type Target = ProblemSolver;
fn deref(&self) -> &Self::Target {
&self.solver
}
}
impl<T: AsyncTester> DerefMut for ParallelProblemSolver<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.solver
}
}
impl<T: AsyncTester> ParallelProblemSolver<T> {
pub fn new(width: usize, depth: usize) -> Self {
Self {
solver: ProblemSolver::new(width, depth),
current_test: None,
}
}
}
type TestQuery = (Vec<(usize, usize)>, Vec<usize>);
impl<T: AsyncTester> ParallelProblemSolver<T> {
pub fn try_generate_complete_candidate(&mut self) -> bool {
while !self.is_complete() {
while self.is_current_cell_missing() {
if !self.try_advance_source() {
return false;
}
}
if !self.try_advance_resource() {
return false;
}
}
true
}
fn try_generate_test_query(&mut self) -> Result<TestQuery, usize> {
let mut test_cells = vec![];
let query = self
.solution
.iter()
.enumerate()
.filter_map(|(res_idx, source_idx)| {
let cell = self.cache[res_idx][*source_idx];
match cell {
None => {
test_cells.push(res_idx);
Some(Ok((res_idx, *source_idx)))
}
Some(false) => Some(Err(res_idx)),
Some(true) => None,
}
})
.collect::<Result<_, _>>()?;
Ok((query, test_cells))
}
fn apply_test_result(
&mut self,
resources: Vec<bool>,
testing_cells: Vec<usize>,
) -> Result<(), usize> {
let mut first_missing = None;
for (result, res_idx) in resources.into_iter().zip(testing_cells) {
let source_idx = self.solution[res_idx];
self.cache[res_idx][source_idx] = Some(result);
if !result && first_missing.is_none() {
first_missing = Some(res_idx);
}
}
if let Some(idx) = first_missing {
Err(idx)
} else {
Ok(())
}
}
pub fn try_poll_next(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
tester: &T,
prefetch: bool,
) -> std::task::Poll<Result<Option<Vec<usize>>, usize>>
where
<T as AsyncTester>::Result: Unpin,
{
if self.width == 0 || self.depth == 0 {
return Ok(None).into();
}
'outer: loop {
if let Some((test, testing_cells)) = &mut self.current_test {
let pinned = Pin::new(test);
let set = ready!(pinned.poll(cx));
let testing_cells = testing_cells.clone();
if let Err(res_idx) = self.apply_test_result(set, testing_cells) {
self.idx = res_idx;
self.prune();
if !self.bail() {
if let Some(res_idx) = self.has_missing_cell() {
return Err(res_idx).into();
} else {
return Ok(None).into();
}
}
self.current_test = None;
continue 'outer;
} else {
self.current_test = None;
if !prefetch {
self.dirty = true;
}
return Ok(Some(self.solution.clone())).into();
}
} else {
if self.dirty {
if !self.bail() {
if let Some(res_idx) = self.has_missing_cell() {
return Err(res_idx).into();
} else {
return Ok(None).into();
}
}
self.dirty = false;
}
while self.try_generate_complete_candidate() {
match self.try_generate_test_query() {
Ok((query, testing_cells)) => {
self.current_test = Some((tester.test_async(query), testing_cells));
continue 'outer;
}
Err(res_idx) => {
self.idx = res_idx;
self.prune();
if !self.bail() {
if let Some(res_idx) = self.has_missing_cell() {
return Err(res_idx).into();
} else {
return Ok(None).into();
}
}
}
}
}
return Ok(None).into();
}
}
}
}
|