rinnd 0.4.0__tar.gz → 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {rinnd-0.4.0 → rinnd-0.4.1}/PKG-INFO +1 -1
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd/Cargo.toml +1 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/Cargo.toml +3 -1
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/nndescent/mod.rs +29 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/search/greedy.rs +25 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/tree/hybrid_forest.rs +144 -66
- {rinnd-0.4.0 → rinnd-0.4.1}/pyproject.toml +1 -1
- {rinnd-0.4.0 → rinnd-0.4.1}/Cargo.lock +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/Cargo.toml +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd/src/lib.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/benches/distance.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/benches/heap.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/examples/graph_bench.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/alternatives.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/angular.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/binary.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/cosine.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/euclidean.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/hellinger.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/inner_product.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/minkowski.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/mod.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/quantized.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/tile.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/distance/traits.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/graph/mod.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/graph/neighbor_graph.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/graph/search_graph.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/heap/candidate_heap.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/heap/mod.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/heap/neighbor_heap.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/index.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/lib.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/nndescent/candidates.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/nndescent/update.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/rng.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/search/mod.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/tree/breadth_first.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/tree/builder.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/tree/flat_tree.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/tree/mod.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-core/src/visited.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/Cargo.toml +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/benches/simd_distance.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/src/avx2.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/src/avx512.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/src/detect.rs +0 -0
- {rinnd-0.4.0 → rinnd-0.4.1}/crates/rinnd-simd/src/lib.rs +0 -0
|
@@ -21,6 +21,7 @@ active-candidates = ["rinnd-core/active-candidates"]
|
|
|
21
21
|
blocked-candidates = ["rinnd-core/blocked-candidates"]
|
|
22
22
|
hybrid-forest = ["rinnd-core/hybrid-forest"]
|
|
23
23
|
tiled-leaves = ["rinnd-core/tiled-leaves"]
|
|
24
|
+
generation-prefetch = ["rinnd-core/generation-prefetch"]
|
|
24
25
|
|
|
25
26
|
[dependencies]
|
|
26
27
|
rinnd-core = { path = "../rinnd-core", default-features = false, features = ["std", "rayon"] }
|
|
@@ -9,7 +9,7 @@ repository.workspace = true
|
|
|
9
9
|
rust-version.workspace = true
|
|
10
10
|
|
|
11
11
|
[features]
|
|
12
|
-
default = ["std", "rayon", "batched-euclidean", "batched-angular", "compact-candidates", "batched-leaves", "candidate-membership", "candidate-workspace", "active-candidates", "hybrid-forest", "tiled-leaves"]
|
|
12
|
+
default = ["std", "rayon", "batched-euclidean", "batched-angular", "compact-candidates", "batched-leaves", "candidate-membership", "candidate-workspace", "active-candidates", "hybrid-forest", "tiled-leaves", "generation-prefetch"]
|
|
13
13
|
std = []
|
|
14
14
|
rayon = ["dep:rayon"]
|
|
15
15
|
mmap = ["dep:memmap2"]
|
|
@@ -28,6 +28,8 @@ active-candidates = []
|
|
|
28
28
|
batched-leaves = []
|
|
29
29
|
# Register-tiled leaf-initialization kernels (bit-identical distances).
|
|
30
30
|
tiled-leaves = []
|
|
31
|
+
# Prefetch next hub's candidate vectors during update generation (dim <= 256).
|
|
32
|
+
generation-prefetch = []
|
|
31
33
|
packed-updates = []
|
|
32
34
|
|
|
33
35
|
[dependencies]
|
|
@@ -508,6 +508,31 @@ struct PotentialUpdate {
|
|
|
508
508
|
|
|
509
509
|
const UPDATE_BLOCK_SIZE: usize = 16384;
|
|
510
510
|
|
|
511
|
+
// Above this, whole-vector prefetch (49 lines at d = 784) slowed generation; the hardware prefetcher suffices.
|
|
512
|
+
const GENERATION_PREFETCH_MAX_DIM: usize = 256;
|
|
513
|
+
|
|
514
|
+
/// Prefetch every cache line of a hub's candidate vectors and their heap roots.
|
|
515
|
+
#[inline(always)]
|
|
516
|
+
fn prefetch_hub(candidates: &CandidateSets, hub: usize, data: &[f32], dim: usize, graph: &NeighborHeap) {
|
|
517
|
+
#[cfg(target_arch = "x86_64")]
|
|
518
|
+
unsafe {
|
|
519
|
+
use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T0};
|
|
520
|
+
let lines = (dim * 4).div_ceil(64);
|
|
521
|
+
for &candidate in candidates.get_new(hub).iter().chain(candidates.get_old(hub)) {
|
|
522
|
+
if candidate < 0 {
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
let vector = data.as_ptr().add(candidate as usize * dim) as *const i8;
|
|
526
|
+
for line in 0..lines {
|
|
527
|
+
_mm_prefetch(vector.add(line * 64), _MM_HINT_T0);
|
|
528
|
+
}
|
|
529
|
+
_mm_prefetch(graph.distances.as_ptr().add(candidate as usize * graph.k) as *const i8, _MM_HINT_T0);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
#[cfg(not(target_arch = "x86_64"))]
|
|
533
|
+
let _ = (candidates, hub, data, dim, graph);
|
|
534
|
+
}
|
|
535
|
+
|
|
511
536
|
trait UpdateRecord: Copy + Send + Sync {
|
|
512
537
|
fn encode(
|
|
513
538
|
update: PotentialUpdate,
|
|
@@ -770,11 +795,15 @@ fn update_iteration_observed<D: Distance<f32> + Sync, Record: UpdateRecord>(
|
|
|
770
795
|
|
|
771
796
|
// Generate updates and bucket by destination vertex block
|
|
772
797
|
let generation_started = Instant::now();
|
|
798
|
+
let prefetch_hubs = cfg!(feature = "generation-prefetch") && dim <= GENERATION_PREFETCH_MAX_DIM;
|
|
773
799
|
thread_buckets.par_iter_mut().enumerate().for_each(|(t, local_buckets)| {
|
|
774
800
|
let row_start = block_start + t * rows_per_thread;
|
|
775
801
|
let row_end = (row_start + rows_per_thread).min(block_end);
|
|
776
802
|
|
|
777
803
|
for i in row_start..row_end {
|
|
804
|
+
if prefetch_hubs && i + 1 < row_end {
|
|
805
|
+
prefetch_hub(candidates, i + 1, data, dim, graph);
|
|
806
|
+
}
|
|
778
807
|
let new_cands = candidates.get_new(i);
|
|
779
808
|
let old_cands = candidates.get_old(i);
|
|
780
809
|
|
|
@@ -86,6 +86,31 @@ fn initialize_fp32<D: Distance<f32>>(
|
|
|
86
86
|
&mut workspace.tree_leaves,
|
|
87
87
|
);
|
|
88
88
|
for &(start, end) in &workspace.tree_leaves {
|
|
89
|
+
if D::USE_DISTANCE_FOUR && dim >= 32 {
|
|
90
|
+
let mut pending = [0i32; 4];
|
|
91
|
+
let mut count = 0;
|
|
92
|
+
for &idx in &tree.indices[start..end] {
|
|
93
|
+
if idx >= 0 && !workspace.visited.check_and_mark(idx) {
|
|
94
|
+
pending[count] = idx;
|
|
95
|
+
count += 1;
|
|
96
|
+
if count == 4 {
|
|
97
|
+
let vectors = pending.map(|c| &data[c as usize * dim..(c as usize + 1) * dim]);
|
|
98
|
+
let distances = distance.distance_four(query, vectors);
|
|
99
|
+
for slot in 0..4 {
|
|
100
|
+
workspace.result_heap.push(distances[slot], pending[slot]);
|
|
101
|
+
workspace.seed_set.push(distances[slot], pending[slot]);
|
|
102
|
+
}
|
|
103
|
+
count = 0;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for &idx in &pending[..count] {
|
|
108
|
+
let d = distance.distance(query, &data[idx as usize * dim..(idx as usize + 1) * dim]);
|
|
109
|
+
workspace.result_heap.push(d, idx);
|
|
110
|
+
workspace.seed_set.push(d, idx);
|
|
111
|
+
}
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
89
114
|
for &idx in &tree.indices[start..end] {
|
|
90
115
|
if idx >= 0 && !workspace.visited.check_and_mark(idx) {
|
|
91
116
|
let d = distance
|
|
@@ -21,8 +21,39 @@ use super::breadth_first::{batched_dot_products, child_seed, make_split, tie_sid
|
|
|
21
21
|
use crate::rng::FastRng;
|
|
22
22
|
use rayon::prelude::*;
|
|
23
23
|
|
|
24
|
-
const NO_NODE:
|
|
25
|
-
const LOCAL_SUBTREE_BYTES: usize =
|
|
24
|
+
const NO_NODE: u32 = u32::MAX;
|
|
25
|
+
const LOCAL_SUBTREE_BYTES: usize = 1 << 20;
|
|
26
|
+
/// Local subtrees copy their rows into a contiguous buffer only for short rows;
|
|
27
|
+
/// longer rows are already contiguous enough that the copy costs more than it saves.
|
|
28
|
+
const GATHER_MAX_DIM: usize = 128;
|
|
29
|
+
const ASSIGN_CHUNK: usize = 16384;
|
|
30
|
+
|
|
31
|
+
/// Point-indexed node assignments shared across threads; each point is written by
|
|
32
|
+
/// exactly one task because permutation ranges are disjoint and hold distinct points.
|
|
33
|
+
#[derive(Clone, Copy)]
|
|
34
|
+
struct SharedAssignments(*mut u32);
|
|
35
|
+
unsafe impl Send for SharedAssignments {}
|
|
36
|
+
unsafe impl Sync for SharedAssignments {}
|
|
37
|
+
|
|
38
|
+
impl SharedAssignments {
|
|
39
|
+
#[inline(always)]
|
|
40
|
+
unsafe fn set(self, point: i32, value: u32) {
|
|
41
|
+
*self.0.add(point as usize) = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Assign `value` to every point in each disjoint permutation range, in parallel.
|
|
46
|
+
fn assign_ranges(permutation: &[i32], assignments: &mut [u32], ranges: &[(usize, usize, u32)]) {
|
|
47
|
+
let target = SharedAssignments(assignments.as_mut_ptr());
|
|
48
|
+
ranges.par_iter().for_each(|&(start, end, value)| {
|
|
49
|
+
permutation[start..end].par_chunks(ASSIGN_CHUNK).for_each(|chunk| {
|
|
50
|
+
for &point in chunk {
|
|
51
|
+
// SAFETY: ranges are disjoint and the permutation holds each point once.
|
|
52
|
+
unsafe { target.set(point, value) };
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
26
57
|
|
|
27
58
|
/// Points per cache-local subtree.
|
|
28
59
|
pub(crate) fn local_points_for(dim: usize) -> usize {
|
|
@@ -60,7 +91,7 @@ type Leaf = (usize, Path, Vec<i32>);
|
|
|
60
91
|
|
|
61
92
|
struct Tree {
|
|
62
93
|
permutation: Vec<i32>,
|
|
63
|
-
assignments: Vec<
|
|
94
|
+
assignments: Vec<u32>,
|
|
64
95
|
frontier: Vec<Node>,
|
|
65
96
|
splits: Vec<Split>,
|
|
66
97
|
leaves: Vec<Leaf>,
|
|
@@ -79,74 +110,82 @@ impl Tree {
|
|
|
79
110
|
}
|
|
80
111
|
}
|
|
81
112
|
|
|
82
|
-
fn retire(&mut self, node: &Node) {
|
|
83
|
-
for &point in &self.permutation[node.start..node.end] {
|
|
84
|
-
self.assignments[point as usize] = NO_NODE;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
113
|
/// Decide every frontier node: leaf now, defer to the cache-local phase,
|
|
89
114
|
/// or split at this level. Returns whether any node splits.
|
|
90
115
|
fn classify(&mut self, data: &[f32], dim: usize, leaf_size: usize, local_points: usize, angular: bool, max_depth: usize) -> bool {
|
|
91
116
|
let frontier = std::mem::take(&mut self.frontier);
|
|
92
117
|
self.splits.clear();
|
|
118
|
+
let mut ranges = Vec::with_capacity(frontier.len());
|
|
93
119
|
for node in frontier {
|
|
94
120
|
let size = node.end - node.start;
|
|
95
121
|
if size <= leaf_size || node.depth >= max_depth {
|
|
96
|
-
|
|
122
|
+
ranges.push((node.start, node.end, NO_NODE));
|
|
97
123
|
self.leaves.push((node.depth, node.path.clone(), self.permutation[node.start..node.end].to_vec()));
|
|
98
124
|
} else if size <= local_points {
|
|
99
|
-
|
|
125
|
+
ranges.push((node.start, node.end, NO_NODE));
|
|
100
126
|
let ids = self.permutation[node.start..node.end].to_vec();
|
|
101
127
|
self.deferred.push((node, ids));
|
|
102
128
|
} else {
|
|
103
129
|
match make_split(data, dim, &self.permutation[node.start..node.end], node.seed, angular) {
|
|
104
130
|
None => {
|
|
105
|
-
|
|
131
|
+
ranges.push((node.start, node.end, NO_NODE));
|
|
106
132
|
self.leaves.push((node.depth, node.path.clone(), self.permutation[node.start..node.end].to_vec()));
|
|
107
133
|
}
|
|
108
134
|
Some(split) => {
|
|
109
|
-
|
|
110
|
-
for &point in &self.permutation[node.start..node.end] {
|
|
111
|
-
self.assignments[point as usize] = index;
|
|
112
|
-
}
|
|
135
|
+
ranges.push((node.start, node.end, self.frontier.len() as u32));
|
|
113
136
|
self.frontier.push(node);
|
|
114
137
|
self.splits.push(split);
|
|
115
138
|
}
|
|
116
139
|
}
|
|
117
140
|
}
|
|
118
141
|
}
|
|
142
|
+
assign_ranges(&self.permutation, &mut self.assignments, &ranges);
|
|
119
143
|
!self.frontier.is_empty()
|
|
120
144
|
}
|
|
121
145
|
|
|
122
146
|
/// Partition each split node exactly as the breadth-first builder does.
|
|
147
|
+
/// Children's assignments are written by the next `classify`.
|
|
123
148
|
fn partition(&mut self, tree_slot: usize, batch_width: usize, sides: &[u8]) {
|
|
124
149
|
let frontier = std::mem::take(&mut self.frontier);
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
150
|
+
// Frontier nodes are disjoint ranges in increasing order of `start`.
|
|
151
|
+
let mut slices = Vec::with_capacity(frontier.len());
|
|
152
|
+
let mut rest: &mut [i32] = &mut self.permutation;
|
|
153
|
+
let mut offset = 0;
|
|
154
|
+
for node in &frontier {
|
|
155
|
+
let (_, tail) = std::mem::take(&mut rest).split_at_mut(node.start - offset);
|
|
156
|
+
let (slice, tail) = tail.split_at_mut(node.end - node.start);
|
|
157
|
+
slices.push(slice);
|
|
158
|
+
rest = tail;
|
|
159
|
+
offset = node.end;
|
|
160
|
+
}
|
|
161
|
+
let lefts: Vec<usize> = slices
|
|
162
|
+
.into_par_iter()
|
|
163
|
+
.map(|slice| {
|
|
164
|
+
let (mut left, mut right) = (0, slice.len());
|
|
165
|
+
while left < right {
|
|
166
|
+
if sides[slice[left] as usize * batch_width + tree_slot] == 0 {
|
|
167
|
+
left += 1;
|
|
168
|
+
} else {
|
|
169
|
+
right -= 1;
|
|
170
|
+
slice.swap(left, right);
|
|
171
|
+
}
|
|
135
172
|
}
|
|
136
|
-
|
|
173
|
+
left
|
|
174
|
+
})
|
|
175
|
+
.collect();
|
|
176
|
+
let mut next = Vec::with_capacity(frontier.len() * 2);
|
|
177
|
+
let mut retired = Vec::new();
|
|
178
|
+
for (node, offset) in frontier.into_iter().zip(lefts) {
|
|
179
|
+
let left = node.start + offset;
|
|
137
180
|
if left == node.start || left == node.end {
|
|
138
|
-
|
|
181
|
+
retired.push((node.start, node.end, NO_NODE));
|
|
139
182
|
self.leaves.push((node.depth, node.path.clone(), self.permutation[node.start..node.end].to_vec()));
|
|
140
183
|
continue;
|
|
141
184
|
}
|
|
142
185
|
next.push(Node { start: node.start, end: left, depth: node.depth + 1, seed: child_seed(node.seed, false), path: node.path.child(node.depth, false) });
|
|
143
186
|
next.push(Node { start: left, end: node.end, depth: node.depth + 1, seed: child_seed(node.seed, true), path: node.path.child(node.depth, true) });
|
|
144
187
|
}
|
|
145
|
-
|
|
146
|
-
for &point in &self.permutation[node.start..node.end] {
|
|
147
|
-
self.assignments[point as usize] = index;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
188
|
+
assign_ranges(&self.permutation, &mut self.assignments, &retired);
|
|
150
189
|
self.frontier = next;
|
|
151
190
|
self.splits.clear();
|
|
152
191
|
}
|
|
@@ -289,75 +328,104 @@ pub(crate) fn build_hybrid_leaf_forest(
|
|
|
289
328
|
assert!((1..=MAX_TREE_BATCH_WIDTH).contains(&tree_batch_width));
|
|
290
329
|
|
|
291
330
|
let seeds: Vec<u64> = (0..n_trees).map(|_| rng.next_u64()).collect();
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
let batch_width = seed_batch.len();
|
|
295
|
-
let mut trees: Vec<Tree> = seed_batch.iter().map(|&seed| Tree::new(n_points, seed)).collect();
|
|
331
|
+
build_trees(data, n_points, dim, &seeds, tree_batch_width, leaf_size, angular, max_depth, local_points)
|
|
332
|
+
}
|
|
296
333
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
334
|
+
/// Build all trees with shared breadth-first levels and return their leaves in tree order,
|
|
335
|
+
/// each in breadth-first order. Margins are computed for `tree_batch_width` trees per pass.
|
|
336
|
+
#[allow(clippy::too_many_arguments)]
|
|
337
|
+
fn build_trees(
|
|
338
|
+
data: &[f32],
|
|
339
|
+
n_points: usize,
|
|
340
|
+
dim: usize,
|
|
341
|
+
seeds: &[u64],
|
|
342
|
+
tree_batch_width: usize,
|
|
343
|
+
leaf_size: usize,
|
|
344
|
+
angular: bool,
|
|
345
|
+
max_depth: usize,
|
|
346
|
+
local_points: usize,
|
|
347
|
+
) -> Vec<Vec<i32>> {
|
|
348
|
+
let n_trees = seeds.len();
|
|
349
|
+
let mut trees: Vec<Tree> = seeds.par_iter().map(|&seed| Tree::new(n_points, seed)).collect();
|
|
350
|
+
// Points outside every frontier keep stale sides, which partition never reads.
|
|
351
|
+
let mut sides = vec![0u8; n_points * n_trees];
|
|
352
|
+
|
|
353
|
+
// Breadth-first levels over large nodes, shared by all trees.
|
|
354
|
+
loop {
|
|
355
|
+
let any_splits = trees
|
|
356
|
+
.par_iter_mut()
|
|
357
|
+
.map(|tree| tree.classify(data, dim, leaf_size, local_points, angular, max_depth))
|
|
358
|
+
.reduce(|| false, |a, b| a | b);
|
|
359
|
+
if !any_splits {
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
sides.par_chunks_mut(n_trees).enumerate().for_each(|(point_index, point_sides)| {
|
|
363
|
+
let point = &data[point_index * dim..(point_index + 1) * dim];
|
|
364
|
+
for (group, group_sides) in trees.chunks(tree_batch_width).zip(point_sides.chunks_mut(tree_batch_width)) {
|
|
308
365
|
let mut hyperplanes: [Option<&[f32]>; MAX_TREE_BATCH_WIDTH] = [None; MAX_TREE_BATCH_WIDTH];
|
|
309
366
|
let mut any = false;
|
|
310
|
-
for (slot, tree) in
|
|
367
|
+
for (slot, tree) in group.iter().enumerate() {
|
|
311
368
|
let node = tree.assignments[point_index];
|
|
312
369
|
if node != NO_NODE {
|
|
313
|
-
hyperplanes[slot] = Some(tree.splits[node].hyperplane.as_slice());
|
|
370
|
+
hyperplanes[slot] = Some(tree.splits[node as usize].hyperplane.as_slice());
|
|
314
371
|
any = true;
|
|
315
372
|
}
|
|
316
373
|
}
|
|
317
374
|
if !any {
|
|
318
|
-
|
|
375
|
+
continue;
|
|
319
376
|
}
|
|
320
|
-
let
|
|
321
|
-
|
|
322
|
-
for (slot, tree) in trees.iter().enumerate() {
|
|
377
|
+
let margins = batched_dot_products(point, &hyperplanes, group.len());
|
|
378
|
+
for (slot, tree) in group.iter().enumerate() {
|
|
323
379
|
let node = tree.assignments[point_index];
|
|
324
380
|
if node != NO_NODE {
|
|
381
|
+
let node = node as usize;
|
|
325
382
|
let margin = margins[slot] + tree.splits[node].offset;
|
|
326
|
-
|
|
383
|
+
group_sides[slot] = if margin.abs() < 1e-8 {
|
|
327
384
|
tie_side(tree.frontier[node].seed, point_index)
|
|
328
385
|
} else {
|
|
329
386
|
(margin >= 0.0) as u8
|
|
330
387
|
};
|
|
331
388
|
}
|
|
332
389
|
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
trees.par_iter_mut().enumerate().for_each(|(slot, tree)| tree.partition(slot, n_trees, &sides));
|
|
393
|
+
}
|
|
394
|
+
drop(sides);
|
|
336
395
|
|
|
337
|
-
|
|
338
|
-
|
|
396
|
+
// Cache-local subtrees for all trees at once, then breadth-first output order per tree.
|
|
397
|
+
trees
|
|
398
|
+
.into_par_iter()
|
|
399
|
+
.map(|tree| {
|
|
339
400
|
let mut leaves = tree.leaves;
|
|
340
401
|
let local: Vec<Vec<Leaf>> = tree
|
|
341
402
|
.deferred
|
|
342
403
|
.into_par_iter()
|
|
343
404
|
.map(|(node, mut ids)| {
|
|
405
|
+
let mut out = Vec::new();
|
|
406
|
+
let mut scratch = (Vec::new(), Vec::new());
|
|
407
|
+
if dim > GATHER_MAX_DIM {
|
|
408
|
+
let mut rows: Vec<u32> = ids.iter().map(|&id| id as u32).collect();
|
|
409
|
+
local_subtree(data, dim, data, &mut ids, &mut rows, node.depth, node.seed, node.path, leaf_size, angular, max_depth, &mut out, &mut scratch);
|
|
410
|
+
return out;
|
|
411
|
+
}
|
|
344
412
|
let mut buffer = Vec::with_capacity(ids.len() * dim);
|
|
345
413
|
for &id in &ids {
|
|
346
414
|
buffer.extend_from_slice(&data[id as usize * dim..(id as usize + 1) * dim]);
|
|
347
415
|
}
|
|
348
416
|
let mut rows: Vec<u32> = (0..ids.len() as u32).collect();
|
|
349
|
-
let mut out = Vec::new();
|
|
350
|
-
let mut scratch = (Vec::new(), Vec::new());
|
|
351
417
|
local_subtree(data, dim, &buffer, &mut ids, &mut rows, node.depth, node.seed, node.path, leaf_size, angular, max_depth, &mut out, &mut scratch);
|
|
352
418
|
out
|
|
353
419
|
})
|
|
354
420
|
.collect();
|
|
355
421
|
leaves.extend(local.into_iter().flatten());
|
|
356
422
|
leaves.sort_unstable_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
|
|
423
|
+
leaves.into_iter().map(|leaf| leaf.2).collect::<Vec<_>>()
|
|
424
|
+
})
|
|
425
|
+
.collect::<Vec<_>>()
|
|
426
|
+
.into_iter()
|
|
427
|
+
.flatten()
|
|
428
|
+
.collect()
|
|
361
429
|
}
|
|
362
430
|
|
|
363
431
|
#[cfg(test)]
|
|
@@ -399,6 +467,16 @@ mod tests {
|
|
|
399
467
|
}
|
|
400
468
|
}
|
|
401
469
|
|
|
470
|
+
#[test]
|
|
471
|
+
fn hybrid_matches_breadth_first_without_gather() {
|
|
472
|
+
let (n, dim) = (3000, GATHER_MAX_DIM + 72);
|
|
473
|
+
let x = data(n, dim, 3);
|
|
474
|
+
for &(angular, width, local, threads) in &[(false, 6, 500, 2), (true, 4, 200, 3), (false, 5, 10_000, 1)] {
|
|
475
|
+
let (a, b) = both(&x, n, dim, 11, 60, angular, 200, width, local, threads);
|
|
476
|
+
assert_eq!(a, b, "angular={angular} width={width} local={local} threads={threads}");
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
402
480
|
#[test]
|
|
403
481
|
fn hybrid_matches_with_depth_limit_and_ties() {
|
|
404
482
|
// Many duplicate points force tie-breaking and depth-limited leaves.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|