/// Write one round's nodes `lo..hi` from its sorted edge buffer, whose first /// edge is absolute index `round_base`. /// /// Batches of [`WRITE_BATCH_NODES`] nodes bake on the pool a wave at a time; /// while wave `k` drains on this thread, wave `k + 1` bakes. Each batch's edge /// slice is found by binary search on the source-sorted buffer, and its base is /// `round_base` plus the edges before it, so no batch depends on another. #[allow(clippy::too_many_arguments)] fn write_round( buffer: &[TmpEdge], lo: u32, hi: u32, round_base: u64, node_coords: &[geom::Pt], lane_pool: &[u16], csr: &Csr, synth: &[Synth], points: &crate::mem::Mapped, sinks: &mut WriteSinks, round: u32, rounds: u32, ) -> Result<()> { let n_batches = (hi - lo).div_ceil(WRITE_BATCH_NODES) as usize; let wave = (par::threads() * WAVE_BATCHES_PER_THREAD).max(1); let n_waves = n_batches.div_ceil(wave); let span_of = |bi: usize| -> (u32, u32) { let first = lo + bi as u32 * WRITE_BATCH_NODES; (first, first.saturating_add(WRITE_BATCH_NODES).min(hi)) }; let bake_wave = |w: usize| -> Vec<(u64, Result)> { let b0 = w * wave; let b1 = (b0 + wave).min(n_batches); (b0..b1) .into_par_iter() .map(|bi| { let (first, end) = span_of(bi); let e0 = buffer.partition_point(|e| e.source < first); let e1 = buffer.partition_point(|e| e.source < end); let base = round_base + e0 as u64; let baked = bake_batch( &buffer[e0..e1], first, end, base, node_coords, lane_pool, csr, synth, points, ); (base, baked) }) .collect() }; let mut prog = crate::progress::Progress::new( format!("Round {}/{rounds}: write", round + 1), u64::from(hi - lo), ); let drain_wave = |baked: Vec<(u64, Result)>, sinks: &mut WriteSinks, prog: &mut crate::progress::Progress| -> Result<()> { for (base, b) in baked { let b = b?; drain_batch(sinks, &b, base)?; prog.add((b.node_recs.len() / 12) as u64); } Ok(()) }; par::install(|| -> Result<()> { let mut pending = if n_waves > 0 { Some(bake_wave(0)) } else { None }; for w in 0..n_waves { let cur = pending.take().expect("a baked wave is pending"); let (drained, next) = rayon::join( || drain_wave(cur, sinks, &mut prog), || (w + 1 < n_waves).then(|| bake_wave(w + 1)), ); drained?; pending = next; } Ok(()) })?; prog.finish(); debug_assert_eq!(sinks.edges.pushed, round_base + buffer.len() as u64); Ok(()) }