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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Queue-to-queue sparse CSR expansion for delta fixpoint waves.
//!
//! A full frontier bitset scan is the wrong shape once a dataflow pipeline has
//! already compacted the active wave. This primitive consumes only queued
//! sources, updates a resident accumulator bitset, and appends first-time
//! discoveries directly into the next active queue.
use std::sync::Arc;
use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
use crate::bitset::bitset_words;
mod strided;
pub use strided::{
csr_queue_delta_strided_dispatch_grid, csr_queue_delta_strided_enqueue,
csr_queue_delta_strided_logical_lanes_per_launch,
csr_queue_delta_strided_source_slots_per_launch,
CSR_QUEUE_DELTA_STRIDED_CAPPED_LAUNCH_MIN_CAPACITY, CSR_QUEUE_DELTA_STRIDED_ENQUEUE_OP_ID,
CSR_QUEUE_DELTA_STRIDED_LANES_PER_SOURCE, CSR_QUEUE_DELTA_STRIDED_MAX_SOURCE_SLOTS_PER_LAUNCH,
};
/// Canonical op id for queue-to-queue delta CSR expansion.
pub const CSR_QUEUE_DELTA_ENQUEUE_OP_ID: &str = "vyre-primitives::graph::csr_queue_delta_enqueue";
/// Default workgroup size for queue-to-queue delta expansion.
pub const CSR_QUEUE_DELTA_ENQUEUE_WORKGROUP_SIZE: [u32; 3] = [256, 1, 1];
/// Build a GPU program that expands queued CSR rows and enqueues only new nodes.
///
/// `accumulator` is the monotone reachability bitset. When an allowed edge
/// reaches a destination whose bit was absent, the destination is appended to
/// `next_queue` and `next_len` is incremented. The observed next length can
/// exceed `next_queue_capacity`; stores are clamped so callers can detect
/// overflow pressure without corrupting resident memory.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn csr_queue_delta_enqueue(
active_queue: &str,
active_len: &str,
edge_offsets: &str,
edge_targets: &str,
edge_kind_mask: &str,
accumulator: &str,
next_queue: &str,
next_len: &str,
node_count: u32,
edge_count: u32,
active_queue_capacity: u32,
next_queue_capacity: u32,
allow_mask: u32,
) -> Program {
if node_count == 0 || active_queue_capacity == 0 || next_queue_capacity == 0 {
return crate::invalid_output_program(
CSR_QUEUE_DELTA_ENQUEUE_OP_ID,
next_len,
DataType::U32,
format!(
"Fix: csr_queue_delta_enqueue requires node_count > 0 and non-zero queue capacities, got node_count={node_count} active_queue_capacity={active_queue_capacity} next_queue_capacity={next_queue_capacity}."
),
);
}
let lane = Expr::InvocationId { axis: 0 };
let words = bitset_words(node_count);
let physical_edge_count = edge_count.max(1);
let body = vec![
Node::let_bind("qd_idx", lane.clone()),
Node::if_then(
Expr::lt(Expr::var("qd_idx"), Expr::u32(active_queue_capacity)),
vec![Node::if_then(
Expr::lt(Expr::var("qd_idx"), Expr::load(active_len, Expr::u32(0))),
vec![
Node::let_bind("qd_src", Expr::load(active_queue, Expr::var("qd_idx"))),
Node::if_then(
Expr::lt(Expr::var("qd_src"), Expr::u32(node_count)),
vec![
Node::let_bind(
"qd_edge_start",
Expr::load(edge_offsets, Expr::var("qd_src")),
),
Node::let_bind(
"qd_edge_end",
Expr::load(
edge_offsets,
Expr::add(Expr::var("qd_src"), Expr::u32(1)),
),
),
Node::loop_for(
"qd_e",
Expr::var("qd_edge_start"),
Expr::var("qd_edge_end"),
vec![Node::if_then(
Expr::lt(Expr::var("qd_e"), Expr::u32(edge_count)),
vec![
Node::let_bind(
"qd_kind",
Expr::load(edge_kind_mask, Expr::var("qd_e")),
),
Node::if_then(
Expr::ne(
Expr::bitand(
Expr::var("qd_kind"),
Expr::u32(allow_mask),
),
Expr::u32(0),
),
vec![
Node::let_bind(
"qd_dst",
Expr::load(edge_targets, Expr::var("qd_e")),
),
Node::if_then(
Expr::lt(
Expr::var("qd_dst"),
Expr::u32(node_count),
),
vec![
Node::let_bind(
"qd_dst_word",
Expr::shr(
Expr::var("qd_dst"),
Expr::u32(5),
),
),
Node::let_bind(
"qd_dst_bit",
Expr::shl(
Expr::u32(1),
Expr::bitand(
Expr::var("qd_dst"),
Expr::u32(31),
),
),
),
Node::let_bind(
"qd_old",
Expr::atomic_or(
accumulator,
Expr::var("qd_dst_word"),
Expr::var("qd_dst_bit"),
),
),
Node::if_then(
Expr::eq(
Expr::bitand(
Expr::var("qd_old"),
Expr::var("qd_dst_bit"),
),
Expr::u32(0),
),
vec![
Node::let_bind(
"qd_slot",
Expr::atomic_add(
next_len,
Expr::u32(0),
Expr::u32(1),
),
),
Node::if_then(
Expr::lt(
Expr::var("qd_slot"),
Expr::u32(
next_queue_capacity,
),
),
vec![Node::store(
next_queue,
Expr::var("qd_slot"),
Expr::var("qd_dst"),
)],
),
],
),
],
),
],
),
],
)],
),
],
),
],
)],
),
];
Program::wrapped(
vec![
BufferDecl::storage(active_queue, 0, BufferAccess::ReadOnly, DataType::U32)
.with_count(active_queue_capacity),
BufferDecl::storage(active_len, 1, BufferAccess::ReadOnly, DataType::U32).with_count(1),
BufferDecl::storage(edge_offsets, 2, BufferAccess::ReadOnly, DataType::U32)
.with_count(node_count + 1),
BufferDecl::storage(edge_targets, 3, BufferAccess::ReadOnly, DataType::U32)
.with_count(physical_edge_count),
BufferDecl::storage(edge_kind_mask, 4, BufferAccess::ReadOnly, DataType::U32)
.with_count(physical_edge_count),
BufferDecl::storage(accumulator, 5, BufferAccess::ReadWrite, DataType::U32)
.with_count(words),
BufferDecl::storage(next_queue, 6, BufferAccess::ReadWrite, DataType::U32)
.with_count(next_queue_capacity),
BufferDecl::storage(next_len, 7, BufferAccess::ReadWrite, DataType::U32).with_count(1),
],
CSR_QUEUE_DELTA_ENQUEUE_WORKGROUP_SIZE,
vec![Node::Region {
generator: Ident::from(CSR_QUEUE_DELTA_ENQUEUE_OP_ID),
source_region: None,
body: Arc::new(body),
}],
)
}
/// CPU reference for queue-to-queue delta expansion.
#[must_use]
#[cfg(any(test, feature = "cpu-parity"))]
#[allow(clippy::too_many_arguments)]
pub fn csr_queue_delta_enqueue_cpu(
active_queue: &[u32],
active_len: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
accumulator: &[u32],
node_count: u32,
next_queue_capacity: usize,
allow_mask: u32,
) -> (Vec<u32>, Vec<u32>, u32) {
let mut accumulator = accumulator.to_vec();
let mut next_queue = Vec::new();
let next_len = try_csr_queue_delta_enqueue_cpu_into(
active_queue,
active_len,
edge_offsets,
edge_targets,
edge_kind_mask,
&mut accumulator,
node_count,
next_queue_capacity,
allow_mask,
&mut next_queue,
)
.unwrap_or_else(|err| {
panic!("csr_queue_delta_enqueue CPU oracle received malformed input. {err}")
});
(accumulator, next_queue, next_len)
}
/// Fallible CPU reference for queue-to-queue delta expansion into caller storage.
///
/// On validation failure both `accumulator` and `next_queue` are left unchanged.
#[cfg(any(test, feature = "cpu-parity"))]
#[allow(clippy::too_many_arguments)]
pub fn try_csr_queue_delta_enqueue_cpu_into(
active_queue: &[u32],
active_len: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
accumulator: &mut Vec<u32>,
node_count: u32,
next_queue_capacity: usize,
allow_mask: u32,
next_queue: &mut Vec<u32>,
) -> Result<u32, String> {
let layout = super::csr_frontier_queue::validate_csr_queue_graph(
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
)?;
if accumulator.len() != layout.words {
return Err(format!(
"Fix: csr_queue_delta_enqueue requires accumulator.len() == bitset_words(node_count), got len={} but expected {} for node_count={node_count}.",
accumulator.len(),
layout.words
));
}
crate::graph::scratch::reserve_graph_items(
next_queue,
next_queue_capacity,
"CSR queue delta CPU oracle",
"next active frontier queue",
)?;
let mut next_tmp = Vec::with_capacity(next_queue_capacity);
let mut accumulator_tmp = accumulator.clone();
let take = (active_len as usize).min(active_queue.len());
let mut next_seen = 0_u32;
for &src in &active_queue[..take] {
if src >= node_count {
continue;
}
let start = edge_offsets[src as usize] as usize;
let end = edge_offsets[src as usize + 1] as usize;
for edge in start..end {
if edge_kind_mask[edge] & allow_mask == 0 {
continue;
}
let dst = edge_targets[edge];
let word = dst as usize / 32;
let bit = 1_u32 << (dst % 32);
let old = accumulator_tmp[word];
if old & bit != 0 {
continue;
}
accumulator_tmp[word] = old | bit;
if next_tmp.len() < next_queue_capacity {
next_tmp.push(dst);
}
next_seen = next_seen.saturating_add(1);
}
}
*accumulator = accumulator_tmp;
next_queue.clear();
next_queue.extend_from_slice(&next_tmp);
Ok(next_seen)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn emitted_program_has_stable_delta_queue_shape() {
let program = csr_queue_delta_enqueue(
"active_queue",
"active_len",
"edge_offsets",
"edge_targets",
"edge_kind_mask",
"accumulator",
"next_queue",
"next_len",
64,
7,
8,
16,
1,
);
assert_eq!(
program.workgroup_size,
CSR_QUEUE_DELTA_ENQUEUE_WORKGROUP_SIZE
);
assert_eq!(program.buffers.len(), 8);
}
#[test]
fn cpu_delta_enqueue_only_emits_first_time_discoveries() {
let edge_offsets = [0, 3, 4, 4, 4, 4];
let edge_targets = [1, 2, 3, 4];
let edge_kind_mask = [1, 1, 2, 1];
let accumulator = vec![0b00001];
let (accumulator, next_queue, next_len) = csr_queue_delta_enqueue_cpu(
&[0, 1],
2,
&edge_offsets,
&edge_targets,
&edge_kind_mask,
&accumulator,
5,
8,
1,
);
assert_eq!(accumulator, vec![0b10111]);
assert_eq!(next_queue, vec![1, 2, 4]);
assert_eq!(next_len, 3);
}
#[test]
fn cpu_delta_enqueue_reports_queue_pressure_without_clobbering_accumulator() {
let edge_offsets = [0, 3, 3, 3, 3];
let edge_targets = [1, 2, 3];
let edge_kind_mask = [1, 1, 1];
let mut accumulator = vec![0b0001];
let mut next_queue = Vec::new();
let next_len = try_csr_queue_delta_enqueue_cpu_into(
&[0],
1,
&edge_offsets,
&edge_targets,
&edge_kind_mask,
&mut accumulator,
4,
2,
1,
&mut next_queue,
)
.expect("Fix: canonical queue delta graph should enqueue bounded discoveries");
assert_eq!(accumulator, vec![0b1111]);
assert_eq!(next_queue, vec![1, 2]);
assert_eq!(next_len, 3);
}
#[test]
fn cpu_delta_enqueue_rejects_bad_accumulator_without_clobbering_outputs() {
let mut accumulator = vec![0xCAFE_BABE, 0xDEAD_BEEF];
let mut next_queue = vec![9, 8, 7];
let err = try_csr_queue_delta_enqueue_cpu_into(
&[0],
1,
&[0, 1],
&[0],
&[1],
&mut accumulator,
1,
4,
1,
&mut next_queue,
)
.expect_err("wrong accumulator width must fail before mutation");
assert!(err.contains("accumulator.len() == bitset_words(node_count)"));
assert_eq!(accumulator, vec![0xCAFE_BABE, 0xDEAD_BEEF]);
assert_eq!(next_queue, vec![9, 8, 7]);
}
}