Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / scripts / maps / osm_ingest / src / graph_build_part11_s1.rs

graph_build_part11_s1.rs87 lines · 3.0 KiBRaw
1/// Write one round's nodes `lo..hi` from its sorted edge buffer, whose first
2/// edge is absolute index `round_base`.
3///
4/// Batches of [`WRITE_BATCH_NODES`] nodes bake on the pool a wave at a time;
5/// while wave `k` drains on this thread, wave `k + 1` bakes. Each batch's edge
6/// slice is found by binary search on the source-sorted buffer, and its base is
7/// `round_base` plus the edges before it, so no batch depends on another.
8#[allow(clippy::too_many_arguments)]
9fn write_round(
10 buffer: &[TmpEdge],
11 lo: u32,
12 hi: u32,
13 round_base: u64,
14 node_coords: &[geom::Pt],
15 lane_pool: &[u16],
16 csr: &Csr,
17 synth: &[Synth],
18 points: &crate::mem::Mapped,
19 sinks: &mut WriteSinks,
20 round: u32,
21 rounds: u32,
22) -> Result<()> {
23 let n_batches = (hi - lo).div_ceil(WRITE_BATCH_NODES) as usize;
24 let wave = (par::threads() * WAVE_BATCHES_PER_THREAD).max(1);
25 let n_waves = n_batches.div_ceil(wave);
26 let span_of = |bi: usize| -> (u32, u32) {
27 let first = lo + bi as u32 * WRITE_BATCH_NODES;
28 (first, first.saturating_add(WRITE_BATCH_NODES).min(hi))
29 };
30 let bake_wave = |w: usize| -> Vec<(u64, Result<BakedBatch>)> {
31 let b0 = w * wave;
32 let b1 = (b0 + wave).min(n_batches);
33 (b0..b1)
34 .into_par_iter()
35 .map(|bi| {
36 let (first, end) = span_of(bi);
37 let e0 = buffer.partition_point(|e| e.source < first);
38 let e1 = buffer.partition_point(|e| e.source < end);
39 let base = round_base + e0 as u64;
40 let baked = bake_batch(
41 &buffer[e0..e1],
42 first,
43 end,
44 base,
45 node_coords,
46 lane_pool,
47 csr,
48 synth,
49 points,
50 );
51 (base, baked)
52 })
53 .collect()
54 };
55 let mut prog = crate::progress::Progress::new(
56 format!("Round {}/{rounds}: write", round + 1),
57 u64::from(hi - lo),
58 );
59 let drain_wave = |baked: Vec<(u64, Result<BakedBatch>)>,
60 sinks: &mut WriteSinks,
61 prog: &mut crate::progress::Progress|
62 -> Result<()> {
63 for (base, b) in baked {
64 let b = b?;
65 drain_batch(sinks, &b, base)?;
66 prog.add((b.node_recs.len() / 12) as u64);
67 }
68 Ok(())
69 };
70 par::install(|| -> Result<()> {
71 let mut pending = if n_waves > 0 { Some(bake_wave(0)) } else { None };
72 for w in 0..n_waves {
73 let cur = pending.take().expect("a baked wave is pending");
74 let (drained, next) = rayon::join(
75 || drain_wave(cur, sinks, &mut prog),
76 || (w + 1 < n_waves).then(|| bake_wave(w + 1)),
77 );
78 drained?;
79 pending = next;
80 }
81 Ok(())
82 })?;
83 prog.finish();
84 debug_assert_eq!(sinks.edges.pushed, round_base + buffer.len() as u64);
85 Ok(())
86}
87