pub fn build_with(input: &Path, out_dir: &Path, opts: Options) -> Result { if let Some(n) = opts.threads { crate::par::set_threads(n); } // The region filter is validated up front: a later stage reads `graph/` // assuming it covers the same region the tiles do, and a graph built for // the wrong region is a silent layer mismatch, not a loud failure. if let Some(b) = &opts.bbox { println!( "region filter: keeping ways touching {:.3},{:.3} .. {:.3},{:.3}", b.min_lon, b.min_lat, b.max_lon, b.max_lat, ); } std::fs::create_dir_all(out_dir) .map_err(|e| Error(format!("cannot create {}: {e}", out_dir.display())))?; let mut timer = crate::timing::PhaseTimer::start("graph"); let blobs = pbf::scan_blobs(input)?; println!("[graph] Scanned {} data blob(s) in {}", blobs.len(), input.display()); pbf::probe_compression(input, &blobs)?; let mut pool = NamePool::new(BufWriter::new(create(&out_dir.join("road_names.bin"))?)); let mut mask = Bitset::new(BITSET_SIZE); let mut marked: u64 = 0; let mut max_id: u64 = 0; timer.switch("refs-stops"); // --- Pass 1: which nodes the graph needs --------------------------------- // Folded through a sink rather than collected, so a chunk's ref list is // consumed and freed while later chunks are still decoding. Nothing about a // way survives this pass: the ways pass below re-reads the way blobs, which // is far cheaper than caching every routable way and every ref it holds. // // The region filter needs way NODE LOCATIONS, but ways arrive before their // nodes in a PBF — so pass 1 cannot filter ways. It marks every routable // way's refs unconditionally; the way passes below (which run after the // coordinates are resolved) apply the filter when they re-read the ways. // Stop nodes ARE filtered here: a node's own coordinates arrive with it. let filter = opts.bbox; let blob_kinds = { let bits = mask.atomic(); pbf::run_pass_sink( input, &blobs, pbf::PassSpec { blob_kinds: None, want: KIND_NODES | KIND_WAYS, label: "Pass 1: refs + stops" }, Pass1::default, |state, block| pass1_blob(state, block, filter, &bits), |chunk| { marked += chunk.marked; max_id = max_id.max(chunk.max_id); Ok(()) }, )? }; println!("{marked} graph node(s) expected"); // Cache the complete blob-kinds mask beside the graph so a `mamaps_build` run over the same // `.pbf` can skip its own full pass-1 scan. This pass inflated every blob with `None`, so the // mask is complete. Best-effort: a write failure must not fail the graph build, which never // reads the sidecar itself. if let Err(e) = pbf::write_blob_kinds(out_dir, input, &blobs, &blob_kinds) { eprintln!("WARN: could not write blob-kinds sidecar: {e}"); } let slots = cap_u32("marked graph node", marked)?; // --- The rank index ------------------------------------------------------ // From here on a node's address is the number of marked ids below its own, so // the node array is implicitly ordered by OSM id and needs to store neither // the id nor a sorted side table to look it up by. timer.switch("rank-index"); let counted = mask.build_rank(max_id); if counted != marked { // The mask and the running count are maintained in the same loop, so a // disagreement means one of the two is wrong about which ids were set, // and every address derived from the rank would be off by however many. return Err(Error(format!( "rank index counted {counted} marked node(s) but pass 1 set {marked}" ))); } // --- Pass 2: coordinates and stop codes for the marked nodes ------------- // Zero-initialised, so the slots belonging to dangling refs are never touched // and never fault in: an untouched page of `coords` costs no real memory. let mut coords: Vec = vec![(0, 0); slots as usize]; let mut present = Bitset::new(u64::from(slots)); let mut stop = Bitset::new(u64::from(slots)); let mut stop_codes: Vec<(u32, Vec)> = Vec::new(); let mut node_count: u64 = 0; timer.switch("node-scan"); // Coordinates scatter from the decode workers. Each node id claims its // `present` bit atomically and only the claimant writes its slot, so every // slot has exactly one writer. That equals the serial fold — which let the // LAST occurrence of an id win — whenever ids are unique, as they are in // any well-formed (sorted) PBF. If some id occurs twice, the race for its // slot could pick a different occurrence, so the pass is redone with the // original single-threaded fold, keeping the output exact either way. // Stop codes stay in the ordered sink: their order is interning order. let duplicate = std::sync::atomic::AtomicBool::new(false); { let pres = present.atomic(); let slots_out = par::SyncPtr(coords.as_mut_ptr()); let _ = pbf::run_pass_sink( input, &blobs, pbf::PassSpec { blob_kinds: Some(&blob_kinds), want: KIND_NODES, label: "Pass 2: node scan" }, Pass2::default, |state, block| { let kinds = pass2_blob(state, block, &mask)?; for (dense, lat_e7, lon_e7) in state.locs.drain(..) { if pres.set(u64::from(dense)) { // SAFETY: `dense < slots` (a rank over the mask), and // the `present` claim above makes this the only writer. unsafe { slots_out.write(dense as usize, (lat_e7, lon_e7)) }; state.claimed += 1; } else { duplicate.store(true, Ordering::Relaxed); } } Ok(kinds) }, |chunk| { node_count += chunk.claimed; for (dense, code) in chunk.stops { stop.set(u64::from(dense)); stop_codes.push((dense, code)); } Ok(()) }, )?; } if duplicate.load(Ordering::Relaxed) { eprintln!("WARN: a node id occurs more than once; redoing the node scan serially"); coords.iter_mut().for_each(|c| *c = (0, 0)); present = Bitset::new(u64::from(slots)); stop = Bitset::new(u64::from(slots)); stop_codes.clear(); node_count = 0; let _ = pbf::run_pass_sink( input, &blobs, pbf::PassSpec { blob_kinds: Some(&blob_kinds), want: KIND_NODES, label: "Pass 2: node scan (serial)" }, Pass2::default, |state, block| pass2_blob(state, block, &mask), |chunk| { for (dense, lat_e7, lon_e7) in chunk.locs { coords[dense as usize] = (lat_e7, lon_e7); if present.set(u64::from(dense)) { node_count += 1; } } for (dense, code) in chunk.stops { stop.set(u64::from(dense)); stop_codes.push((dense, code)); } Ok(()) }, )?; } println!( "Located {node_count} node(s); {} marked id(s) had no coordinates in the file", marked - node_count ); // Stop codes go into the pool only after every way name is in it, in the // order pass 2 met them, so `road_names.bin` is unaffected by dense // addressing and by where the way names are interned. let index = NodeIndex { mask, present }; // --- Collapse ------------------------------------------------------------ let spill_dir = opts.spill_dir.clone().unwrap_or_else(|| out_dir.to_path_buf()); let spill_pts_dir = opts.spill_pts_dir.clone().unwrap_or_else(|| spill_dir.clone()); timer.switch("collapse"); let mut collapsed = if opts.within_way_chains { collapse_within_ways( input, &spill_dir, &spill_pts_dir, &blobs, &blob_kinds, &index, &coords, &stop, slots, filter, &mut pool, &mut timer, )? } else { collapse_segments( input, &spill_dir, &spill_pts_dir, &blobs, &blob_kinds, &index, &coords, &stop, slots, filter, &mut pool, &mut timer, )? }; let mut prog = crate::progress::Progress::new("Interning stop codes", stop_codes.len() as u64); for (_, code) in &stop_codes { pool.intern(code).map_err(io_err)?; prog.inc(); } prog.finish(); drop(stop_codes); timer.switch("rank-survivors"); // `saturating_sub` because a file with no routable ways and no stop nodes // marks nothing, and there is then no largest id to index up to. let kept_count = cap_u32( "surviving node", collapsed.kept.build_rank(u64::from(slots).saturating_sub(1)), )?; let kept = collapsed.kept; println!( "Collapsed {node_count} node(s) -> {kept_count} ({} chain(s), {} split(s) for the \ 256-point limit)", collapsed.chain_count, collapsed.splits ); cap_u32("chain", collapsed.chain_count)?; let raw_edge_count = collapsed.raw_edge_count; // --- Morton order ------------------------------------------------------- // Deferred to here, and applied only to the survivors, because that is the // only set the device ever binary-searches. Sorting before the collapse would // have meant carrying a 64-bit spatial key through every node that was about // to be collapsed away. timer.switch("morton-sort"); println!("Sorting {kept_count} surviving node(s) by spatial key..."); // Keys on the pool, written in place: the kept bitset's rank says where // each slot range's keys start, so every range fills its own disjoint // slice and `keys` comes out exactly as the serial loop pushed it // (ascending `dense`) with no second copy. let mut keys: Vec<(u64, u32)> = vec![(0, 0); kept_count as usize]; { const RANGE: usize = 1 << 20; let ranges: Vec<(usize, usize)> = (0..(slots as usize).div_ceil(RANGE)) .map(|r| (r * RANGE, ((r + 1) * RANGE).min(slots as usize))) .collect(); let mut pieces: Vec> = Vec::with_capacity(ranges.len()); let mut rest: &mut [(u64, u32)] = &mut keys; for &(lo, hi) in &ranges { // Kept ids below `hi` minus those below `lo`. let rank = |i: usize| -> usize { if i >= slots as usize { kept_count as usize } else { kept.dense(i as u64) as usize } }; let (mine, tail) = rest.split_at_mut(rank(hi) - rank(lo)); pieces.push(((lo, hi), mine)); rest = tail; } debug_assert!(rest.is_empty()); let coords = &coords; let kept = &kept; par::install(|| { pieces.par_iter_mut().for_each(|((lo, hi), out)| { let mut k = 0usize; for i in *lo as u32..*hi as u32 { if kept.get(u64::from(i)) { let (lat_e7, lon_e7) = coords[i as usize]; out[k] = (spatial_from_e7(lat_e7, lon_e7), i); k += 1; } } debug_assert_eq!(k, out.len()); }); }); } // The key is `(spatial, dense)`, and the keys are laid out with `dense` // ascending, so this is exactly the order the previous STABLE // `sort_by_key(spatial)` produced: ties there fell back to insertion order, // which was ascending `dense`. Spelling the tie-break into the key makes the // order total, and a total order is what lets an UNSTABLE parallel sort be // substituted without changing a single final id -- and final ids are what // `nodes.bin`, `edges.bin` and every offset in the pack are numbered by, so // this is the one comparison in the build that must not move. eprintln!("Sorting {kept_count} spatial key(s)..."); par::install(|| keys.par_sort_unstable()); // Final ids on the pool. `final_of_kept` is a permutation write (every // `dense` appears once in `keys`), `node_coords` an ordered map, and each // byte of `stops_final` a pure function of its own eight final ids. let mut final_of_kept = vec![0u32; kept_count as usize]; let mut stops_final = Bitset::new(u64::from(kept_count)); let node_coords: Vec = { let out = par::SyncPtr(final_of_kept.as_mut_ptr()); let (keys, kept, coords, stop) = (&keys, &kept, &coords, &stop); par::install(|| { keys.par_iter().enumerate().for_each(|(final_id, (_, dense))| { // SAFETY: `keys` holds each kept dense id exactly once, so each // rank is written by exactly one iteration. unsafe { out.write(kept.dense(u64::from(*dense)) as usize, final_id as u32) }; }); stops_final.bits.par_chunks_mut(1 << 16).enumerate().for_each(|(ci, chunk)| { for (j, byte) in chunk.iter_mut().enumerate() { let base = ((ci << 16) + j) * 8; let mut v = 0u8; for k in 0..8 { if let Some((_, dense)) = keys.get(base + k) { if stop.get(u64::from(*dense)) { v |= 1 << k; } } } *byte = v; } }); keys.par_iter().map(|(_, dense)| coords[*dense as usize]).collect() }) }; drop(keys); drop(stop); // The chain spill holds coordinates, so nothing downstream needs the array // indexed by *marked* node — the largest in the build — any longer. drop(coords); drop(index); let ids = FinalIds { kept: &kept, of_kept: &final_of_kept, }; // --- The edge index ----------------------------------------------------- timer.switch("edge-index"); let csr = build_csr(&collapsed.spill, kept_count, &ids, &mut timer)?; // --- Reconnect isolated transit stops ----------------------------------- timer.switch("components"); let rec = reconnect_isolated_stops(&node_coords, &stops_final, &csr); let synth = rec.synth; drop(stops_final); // --- Write -------------------------------------------------------------- // Load the optional DEM up front so a bad `--dem` path fails before the write starts. `None` // leaves `elevation.bin` unwritten and every node's baked elevation at 0. let dem = match &opts.dem { Some(path) => Some(crate::dem::Dem::load(path)?), None => None, }; timer.switch("write"); let written = write_graph( out_dir, &node_coords, &collapsed.spill, &collapsed.lane_pool, &csr, &synth, &ids, opts.round_count(), dem.as_ref(), &mut timer, )?; collapsed.spill.remove(); let unique_names = pool.unique_count(); let name_bytes = pool.byte_len(); pool.finish().map_err(io_err)?; timer.finish(); println!( "Done. Nodes: {} Edges: {} intermediate.bin: {} byte(s)", kept_count, written.edge_count, written.intermediate_bytes ); Ok(Stats { node_count: u64::from(kept_count), edge_count: written.edge_count, unique_names, name_bytes, lcc_size: rec.lcc_size, stops_already_connected: rec.already_connected, reconnected_stops: synth.len() / 2, stops_unreachable: rec.unreachable, raw_node_count: node_count, raw_edge_count, chain_splits: collapsed.splits, geometry_edges: written.geometry_edges, reversed_edges: written.reversed_edges, intermediate_bytes: written.intermediate_bytes, edges_bytes: written.edges_bytes, escape_count: written.escape_count, named_edges: written.named_edges, census: written.census, }) } /// What either collapse strategy hands the writer. /// /// Both produce the same thing — a chain spill plus the set of nodes that survive /// — and differ only in how much memory it costs to get there and whether a chain /// may cross from one OSM way into the next. /// /// The chains are always on disk, even for the reference path that had them in /// memory anyway, so that there is one writer. That also means `coords` — the /// largest array in the build, and the only one indexed by *marked* rather than /// surviving node — can be freed before the writer runs. struct Collapsed { spill: chains::Spill, /// Dense ids that survive as graph nodes. kept: Bitset, lane_pool: Vec, chain_count: u64, splits: usize, raw_edge_count: u64, }