#![allow(clippy::too_many_arguments)]
use super::ifds_reach_step;
use crate::dispatch_input_cache::OwnedDispatchInputs;
use vyre_primitives::graph::program_graph::ProgramGraphShape;
pub fn ifds_reach_closure_via<F>(
dispatch: &F,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
seed_bits: &[u32],
max_iterations: u32,
) -> Result<Vec<u32>, String>
where
F: Fn(&vyre::ir::Program, &[Vec<u8>], Option<[u32; 3]>) -> Result<Vec<Vec<u8>>, String>,
{
let owned_inputs = OwnedDispatchInputs::new();
ifds_reach_closure_borrowed_via(
&|program, inputs, grid| {
owned_inputs.dispatch_refreshing_frontier(program, inputs, grid, dispatch)
},
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
seed_bits,
max_iterations,
)
}
pub fn ifds_reach_closure_borrowed_via<F>(
dispatch: &F,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
seed_bits: &[u32],
max_iterations: u32,
) -> Result<Vec<u32>, String>
where
F: Fn(&vyre::ir::Program, &[&[u8]], Option<[u32; 3]>) -> Result<Vec<Vec<u8>>, String>,
{
crate::dispatch_decode::require_positive_iterations("ifds_reach_closure", max_iterations)?;
crate::dispatch_decode::require_bitset_tail_clear("ifds_reach_closure", seed_bits, node_count)?;
let out = ifds_reach_closure_borrowed_into_via(
&|program, inputs, grid, outputs| {
let result = dispatch(program, inputs, grid)?;
crate::output_scratch::replace_outputs_preserving_slots(outputs, result);
Ok(())
},
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
seed_bits,
max_iterations,
)?;
crate::dispatch_decode::require_bitset_tail_clear(
"ifds_reach_closure output",
&out,
node_count,
)?;
Ok(out)
}
pub fn ifds_reach_closure_borrowed_into_via<F>(
dispatch: &F,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
seed_bits: &[u32],
max_iterations: u32,
) -> Result<Vec<u32>, String>
where
F: Fn(&vyre::ir::Program, &[&[u8]], Option<[u32; 3]>, &mut Vec<Vec<u8>>) -> Result<(), String>,
{
let mut scratch = crate::fixed_point_scratch::FixedPointScratch::default();
ifds_reach_closure_borrowed_into_with_scratch_via(
dispatch,
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
seed_bits,
max_iterations,
&mut scratch,
)
}
pub fn ifds_reach_closure_borrowed_into_with_scratch_via<F>(
dispatch: &F,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
seed_bits: &[u32],
max_iterations: u32,
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
) -> Result<Vec<u32>, String>
where
F: Fn(&vyre::ir::Program, &[&[u8]], Option<[u32; 3]>, &mut Vec<Vec<u8>>) -> Result<(), String>,
{
let mut result = crate::staging_reserve::reserved_fixed_point_result(
"ifds_reach_closure result",
node_count,
)?;
ifds_reach_closure_borrowed_into_result_with_scratch_via(
dispatch,
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
seed_bits,
max_iterations,
scratch,
&mut result,
)?;
Ok(result)
}
pub fn ifds_reach_closure_borrowed_into_result_with_scratch_via<F>(
dispatch: &F,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
seed_bits: &[u32],
max_iterations: u32,
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
result: &mut Vec<u32>,
) -> Result<(), String>
where
F: Fn(&vyre::ir::Program, &[&[u8]], Option<[u32; 3]>, &mut Vec<Vec<u8>>) -> Result<(), String>,
{
crate::dispatch_decode::require_positive_iterations("ifds_reach_closure", max_iterations)?;
let words = crate::dispatch_decode::bitset_word_capacity("ifds_reach_closure", node_count)?;
crate::dispatch_decode::require_bitset_words("ifds_reach_closure", seed_bits, words)?;
crate::dispatch_decode::require_bitset_tail_clear("ifds_reach_closure", seed_bits, node_count)?;
crate::dispatch_decode::require_csr_shape(
"ifds_reach_closure",
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
)?;
scratch.prepare_frontier_words(words, seed_bits)?;
let edge_count = u32::try_from(edge_targets.len()).map_err(|error| {
format!(
"ifds_reach_closure edge target count does not fit u32: {error}. Fix: shard the ProgramGraph before GPU dispatch."
)
})?;
let program = ifds_reach_step(
ProgramGraphShape::new(node_count, edge_count),
"fin",
"fout",
);
let node_count_usize =
crate::dispatch_decode::u32_to_usize(node_count, "ifds_reach_closure node count")?;
scratch.prepare_static_input_byte_slots(5)?;
crate::dispatch_decode::pack_repeated_u32_into(
0,
node_count_usize,
&mut scratch.static_input_bytes[0],
)?;
crate::dispatch_decode::try_pack_u32_into(edge_offsets, &mut scratch.static_input_bytes[1], "weir u32 byte staging")?;
crate::dispatch_decode::try_pack_u32_into(edge_targets, &mut scratch.static_input_bytes[2], "weir u32 byte staging")?;
crate::dispatch_decode::try_pack_u32_into(edge_kind_mask, &mut scratch.static_input_bytes[3], "weir u32 byte staging")?;
crate::dispatch_decode::pack_repeated_u32_into(
0,
node_count_usize,
&mut scratch.static_input_bytes[4],
)?;
scratch.prepare_next_words(words)?;
for _ in 0..max_iterations {
crate::dispatch_decode::try_pack_u32_into(&scratch.frontier_words, &mut scratch.frontier_bytes, "weir u32 byte staging")?;
let inputs = [
scratch.static_input_bytes[0].as_slice(),
scratch.static_input_bytes[1].as_slice(),
scratch.static_input_bytes[2].as_slice(),
scratch.static_input_bytes[3].as_slice(),
scratch.static_input_bytes[4].as_slice(),
scratch.frontier_bytes.as_slice(),
scratch.frontier_bytes.as_slice(),
];
dispatch(
&program,
&inputs,
Some([node_count.max(1), 1, 1]),
&mut scratch.outputs,
)?;
crate::dispatch_decode::unpack_only_exact_u32_into(
&scratch.outputs,
"ifds_reach_closure",
"frontier",
words,
&mut scratch.next,
)?;
crate::dispatch_decode::require_bitset_tail_clear(
"ifds_reach_closure output",
&scratch.next,
node_count,
)?;
if crate::fixed_point_scratch::copy_if_converged(
&scratch.next,
&scratch.frontier_words,
result,
)? {
return Ok(());
}
std::mem::swap(&mut scratch.frontier_words, &mut scratch.next);
}
Err(format!(
"ifds_reach_closure did not converge within {max_iterations} iterations"
))
}