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
//! The batched fan: the fan's origins run [`LANES`] at a time through one
//! multi-source level sweep (ADR-0612) instead of one layered search per
//! origin. The sweep answers, per level, which origins first reach which
//! far node; each such pair is then one partition whose trails are rebuilt
//! by the pair search, bounded by the known distance. The partition body,
//! the end-pattern admission and the selector are the per-origin fan's.
#[cfg(test)]
use std::cell::Cell;
use relate_core::{NodeId, Result};
use crate::shortest::multi_source::LANES;
use super::anchor::{Side, Way};
use super::far_set::FarSet;
use super::route::Route;
use super::MatchedSink;
#[cfg(test)]
thread_local! {
static BATCHED_OFF: Cell<bool> = const { Cell::new(false) };
static BATCHES: Cell<u64> = const { Cell::new(0) };
}
/// Run `f` with the batched fan off on this thread: the per-origin layered
/// fan is the oracle of the parity tests.
#[cfg(test)]
pub(super) fn without_batched_fan<T>(f: impl FnOnce() -> T) -> T {
crate::exec::test_support::with_flag_disabled(&BATCHED_OFF, f)
}
/// Take and reset this thread's count of batches the sweep ran.
#[cfg(test)]
pub(super) fn take_batches() -> u64 {
BATCHES.with(|c| c.replace(0))
}
fn enabled() -> bool {
#[cfg(test)]
if BATCHED_OFF.with(Cell::get) {
return false;
}
true
}
/// The set bits of `mask`, as lane numbers.
fn lanes(mut mask: u64) -> impl Iterator<Item = usize> {
std::iter::from_fn(move || {
(mask != 0).then(|| {
let lane = mask.trailing_zeros() as usize;
mask &= mask - 1;
lane
})
})
}
impl Route<'_> {
/// Answer the fan from `origins` in batches, or `Ok(false)` when this
/// fan keeps the per-origin search — before any row is pushed. The
/// batch applies to a fan with a listed far side, several origins, and
/// an unfiltered selection: a path predicate answers the shortest
/// *satisfying* trail, which a distance does not decide; one origin
/// has nothing to share; and a node-id span past the sweep's tables
/// keeps the layered search's memory bounded by the ball.
pub(super) fn fan_batched(
&self,
origins: &Side,
way: Way,
far: Option<&FarSet>,
out: &mut MatchedSink<'_, '_>,
) -> Result<bool> {
let (Some(far), Some(ids)) = (far, origins.ids()) else {
return Ok(false);
};
if ids.len() < 2 || !enabled() || self.filters.path.is_some() || !self.search.sweep_fits() {
return Ok(false);
}
let targets = far.ids();
for chunk in ids.chunks(LANES) {
if self.halted() {
break;
}
// A lone trailing origin has nothing to share: the layered fan.
match chunk {
[origin] => self.fan(*origin, way, Some(far), out)?,
_ => self.fan_chunk(chunk, way, (far, &targets), out)?,
}
}
Ok(true)
}
/// One batch of at most [`LANES`] origins. Depth 0 first (an origin
/// that is itself a far node), then one callback per sweep level that
/// reaches far nodes, each reached (origin, far) pair one partition.
fn fan_chunk(
&self,
ids: &[NodeId],
way: Way,
(far, targets): (&FarSet, &[NodeId]),
out: &mut MatchedSink<'_, '_>,
) -> Result<()> {
#[cfg(test)]
BATCHES.with(|c| c.set(c.get() + 1));
// Forward, the origin is the start, bound once; backward, each
// reached node is a start, bound in its own partition. An origin
// the start pattern refuses never joins the sweep.
let mut rows = Vec::with_capacity(ids.len());
let mut active = 0u64;
for (lane, &origin) in ids.iter().enumerate() {
self.ctx.graph.cancel_tick()?;
match way {
Way::Forward => {
let row = self.bind_start(origin)?;
active |= u64::from(row.is_some()) << lane;
rows.push(row);
}
Way::Backward => {
active |= 1 << lane;
rows.push(None);
}
}
}
// Far nodes each origin has reached, for the fan's early stop.
let mut settled = vec![0usize; ids.len()];
let mut retire = 0u64;
for lane in lanes(active) {
if self.halted() {
return Ok(());
}
let origin = ids[lane];
if far.contains(origin) {
// The origin's own partition (s = e) needs no trails.
self.fan_member(origin, origin, way, rows[lane].as_ref(), out, |_, _| {
Ok(Vec::new())
})?;
settled[lane] += 1;
retire |= u64::from(settled[lane] == far.len()) << lane;
}
}
active &= !retire;
let search = way.search(&self.search);
search.sweep_levels(ids, active, targets, |_, reached| {
let any = reached.iter().fold(0, |all, &(_, mask)| all | mask);
let mut retire = 0u64;
for lane in lanes(any) {
let before = out.folded();
for &(node, mask) in reached {
if mask & (1 << lane) == 0 {
continue;
}
if self.halted() {
return Ok(u64::MAX);
}
settled[lane] += 1;
self.fan_member(ids[lane], node, way, rows[lane].as_ref(), out, |s, e| {
self.search.pair_trails(s, e)
})?;
}
// As the layered fan: every far node reached ends the
// origin's search; a first-layer spelling ends it at
// the first level that produced a row.
let done = settled[lane] == far.len()
|| (self.shape.spelling.first_layer_only() && out.folded() > before);
retire |= u64::from(done) << lane;
}
Ok(if self.halted() { u64::MAX } else { retire })
})
}
}