Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / tiler_part3_s3.rs
tiler_part3_s3.rs312 lines · 14.8 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests_parallel { |
| 3 | use super::*; |
| 4 | use crate::tiler::tests::*; |
| 5 | use crate::tilespill::{ChunkRef, ChunkSpill}; |
| 6 | |
| 7 | /// Drive a merge iterator to a vec, failing the test on the first error |
| 8 | /// rather than publishing a short sequence. |
| 9 | fn collect<I>(merged: &mut I) -> Vec<(u64, Vec<ChunkEntry>)> |
| 10 | where |
| 11 | I: Iterator<Item = Result<(u64, Vec<ChunkEntry>)>>, |
| 12 | { |
| 13 | merged |
| 14 | .by_ref() |
| 15 | .collect::<Result<Vec<_>>>() |
| 16 | .expect("merge the chunks") |
| 17 | } |
| 18 | |
| 19 | /// Map a fixture into spill chunks at `z`, returning refs plus the spill. |
| 20 | /// The spill outlives the refs: callers keep both alive together. Forces |
| 21 | /// small vertex chunks so the fixture spans several chunks even at test |
| 22 | /// scale (a single-chunk merge exercises nothing). |
| 23 | fn map_chunks( |
| 24 | features: &[crate::extract::Feature], |
| 25 | z: u8, |
| 26 | ) -> (Vec<ChunkRef>, ChunkSpill) { |
| 27 | let _guard = budget(); |
| 28 | crate::tiler::set_chunk_vertices(64); |
| 29 | let store = spilled(features); |
| 30 | let spill = ChunkSpill::create(scratch()).expect("spill"); |
| 31 | let tolerance = tile_build::simplify::tolerance_for(z, crate::DEFAULT_MAX_ZOOM, 1.0); |
| 32 | let buffer = tile_build::geom::buffer_for(crate::tiler::EXTENT); |
| 33 | let settings = settings(0, 14); |
| 34 | let _ = &settings; |
| 35 | let (chunks, _) = |
| 36 | crate::tiler::map_zoom(&store, &settings, z, tolerance, buffer, &spill) |
| 37 | .expect("map the fixture"); |
| 38 | crate::tiler::set_chunk_vertices(0); |
| 39 | (chunks, spill) |
| 40 | } |
| 41 | |
| 42 | /// The gate this work stands on: range-parallel output is the serial |
| 43 | /// output, tile for tile, layer for layer, byte for byte — over a |
| 44 | /// multi-chunk fixture with lakes, buildings and cross-tile rivers, at a |
| 45 | /// zoom where the wash gate fires (z14) and one where it does not (z10). |
| 46 | /// Both zooms are above z3, where the bucket table inverts exactly (see |
| 47 | /// `parallel_merge_supported`) — the identity below rests on that. |
| 48 | #[test] |
| 49 | fn parallel_merge_is_the_serial_merge() { |
| 50 | for z in [10u8, 14] { |
| 51 | let (chunks, spill) = map_chunks(&a_crowd(), z); |
| 52 | assert!(chunks.len() > 1, "the fixture must span chunks at z{z}"); |
| 53 | let mut serial = super::merge(&chunks, &spill); |
| 54 | let want = collect(&mut serial); |
| 55 | assert!(!want.is_empty(), "the fixture must yield tiles at z{z}"); |
| 56 | for workers in [2usize, 3, 7] { |
| 57 | let plan = |
| 58 | super::plan_ranges(&chunks, &spill, z, workers).expect("plan the ranges"); |
| 59 | let got = std::thread::scope(|s| { |
| 60 | let queues = super::RangeQueues::new(workers); |
| 61 | let chunks_ref = &chunks; |
| 62 | let spill_ref = &spill; |
| 63 | let plan_ref = &plan; |
| 64 | for r in 0..workers { |
| 65 | let tx = queues.sender(r); |
| 66 | s.spawn(move || { |
| 67 | super::merge_range_into(chunks_ref, spill_ref, plan_ref, r, tx); |
| 68 | }); |
| 69 | } |
| 70 | let mut coord = super::ParallelMerge::new(queues); |
| 71 | collect(&mut coord) |
| 72 | }); |
| 73 | assert_eq!(got, want, "worker count {workers} moved z{z}"); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// The planner answers the same from `write_chunk`'s recorded tile runs as from skip-walking |
| 79 | /// the chunks back off the spill: same cuts, seeks, counts and ends. (The first plan takes the |
| 80 | /// runs; the second finds them gone and walks.) |
| 81 | #[test] |
| 82 | fn planning_from_recorded_runs_equals_walking_the_chunks() { |
| 83 | for z in [10u8, 14] { |
| 84 | for workers in [2usize, 5, 13] { |
| 85 | let (chunks, spill) = map_chunks(&a_crowd(), z); |
| 86 | let indexed = super::plan_ranges(&chunks, &spill, z, workers).expect("plan"); |
| 87 | let walked = super::plan_ranges(&chunks, &spill, z, workers).expect("plan"); |
| 88 | assert_eq!(indexed.bounds, walked.bounds, "z{z} x{workers}"); |
| 89 | assert_eq!(indexed.range_cost, walked.range_cost, "z{z} x{workers}"); |
| 90 | assert_eq!((indexed.hot_tile, indexed.hot_cost), (walked.hot_tile, walked.hot_cost)); |
| 91 | assert_eq!(indexed.offsets, walked.offsets, "z{z} x{workers}"); |
| 92 | assert_eq!(indexed.remaining, walked.remaining, "z{z} x{workers}"); |
| 93 | assert_eq!(indexed.exact_end, walked.exact_end, "z{z} x{workers}"); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /// Empty chunks (a map chunk whose features all clipped away) start where the next chunk |
| 99 | /// starts. The run index must not hand one chunk's runs to the other: re-spill a fixture with |
| 100 | /// an empty chunk ahead of every real one and the indexed plan still equals the walked plan, |
| 101 | /// and the parallel merge still equals the serial one. |
| 102 | #[test] |
| 103 | fn empty_chunks_do_not_borrow_their_neighbours_runs() { |
| 104 | let z = 14u8; |
| 105 | let (chunks, spill) = map_chunks(&a_crowd(), z); |
| 106 | let respill = ChunkSpill::create(scratch()).expect("spill"); |
| 107 | let mut rechunks = Vec::new(); |
| 108 | for chunk in &chunks { |
| 109 | let mut reader = spill.reader(chunk, 64 << 10); |
| 110 | let mut map = std::collections::BTreeMap::new(); |
| 111 | while let Some((key, entry)) = reader.next().expect("read back") { |
| 112 | map.insert(key, entry); |
| 113 | } |
| 114 | rechunks.push(respill.write_chunk(std::collections::BTreeMap::new()).expect("empty")); |
| 115 | rechunks.push(respill.write_chunk(map).expect("chunk")); |
| 116 | } |
| 117 | assert!(rechunks.windows(2).any(|w| w[0].len == 0 && w[0].at == w[1].at)); |
| 118 | let want = collect(&mut super::merge(&rechunks, &respill)); |
| 119 | for workers in [2usize, 5] { |
| 120 | let indexed = super::plan_ranges(&rechunks, &respill, z, workers).expect("plan"); |
| 121 | let walked = super::plan_ranges(&rechunks, &respill, z, workers).expect("plan"); |
| 122 | assert_eq!(indexed.offsets, walked.offsets); |
| 123 | assert_eq!(indexed.remaining, walked.remaining); |
| 124 | assert_eq!(indexed.exact_end, walked.exact_end); |
| 125 | assert_eq!(indexed.bounds, walked.bounds); |
| 126 | // A fresh index for the merge itself. |
| 127 | let respill2 = ChunkSpill::create(scratch()).expect("spill"); |
| 128 | let mut again = Vec::new(); |
| 129 | for c in &rechunks { |
| 130 | let mut reader = respill.reader(c, 64 << 10); |
| 131 | let mut map = std::collections::BTreeMap::new(); |
| 132 | while let Some((key, entry)) = reader.next().expect("read back") { |
| 133 | map.insert(key, entry); |
| 134 | } |
| 135 | again.push(respill2.write_chunk(map).expect("chunk")); |
| 136 | } |
| 137 | let plan = super::plan_ranges(&again, &respill2, z, workers).expect("plan"); |
| 138 | let got = std::thread::scope(|s| { |
| 139 | let queues = super::RangeQueues::new(workers); |
| 140 | for r in 0..workers { |
| 141 | let tx = queues.sender(r); |
| 142 | let (c, sp, p) = (&again, &respill2, &plan); |
| 143 | s.spawn(move || super::merge_range_into(c, sp, p, r, tx)); |
| 144 | } |
| 145 | collect(&mut super::ParallelMerge::new(queues)) |
| 146 | }); |
| 147 | assert_eq!(got, want, "x{workers}"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// A range worker whose reader fails while skipping to the range start reports the error and |
| 152 | /// finishes, rather than looping on the failed reader. Here the plan is corrupted on purpose |
| 153 | /// (a seek one byte into an entry). |
| 154 | #[test] |
| 155 | fn a_failing_range_reader_reports_and_stops() { |
| 156 | let z = 14u8; |
| 157 | let (chunks, spill) = map_chunks(&a_crowd(), z); |
| 158 | let workers = 3usize; |
| 159 | let mut plan = super::plan_ranges(&chunks, &spill, z, workers).expect("plan"); |
| 160 | let r = 1usize; |
| 161 | let ci = (0..chunks.len()) |
| 162 | .find(|&ci| plan.remaining[ci][r] > 0) |
| 163 | .expect("a chunk with entries in the middle range"); |
| 164 | plan.offsets[ci][r] += 1; |
| 165 | let queues = super::RangeQueues::new(workers); |
| 166 | std::thread::scope(|s| { |
| 167 | for w in 0..workers { |
| 168 | let tx = queues.sender(w); |
| 169 | let (c, sp, p) = (&chunks, &spill, &plan); |
| 170 | s.spawn(move || super::merge_range_into(c, sp, p, w, tx)); |
| 171 | } |
| 172 | }); |
| 173 | let mut coord = super::ParallelMerge::new(queues); |
| 174 | assert!(coord.by_ref().any(|item| item.is_err()), "the corrupt seek must surface"); |
| 175 | } |
| 176 | |
| 177 | /// Ranges that hold nothing yield nothing: a single-tile fixture merged |
| 178 | /// under more workers than tiles still produces exactly that tile. |
| 179 | /// (A pool, not a lake: wash areas never reach z14 tiles under the cap.) |
| 180 | #[test] |
| 181 | fn empty_ranges_yield_nothing() { |
| 182 | let pool = crate::extract::Feature { |
| 183 | class: crate::schema::Class::area( |
| 184 | tilecodec::mamaps::dict::LAYER_LANDTYPE, |
| 185 | crate::schema::kind("swimming_pool"), |
| 186 | 14, |
| 187 | ), |
| 188 | geometry: crate::tiler::tests::square(-122.0, 37.0, 0.005), |
| 189 | name: None, |
| 190 | id: tilecodec::mamaps::body::ID_NONE, |
| 191 | transit_color: 0, |
| 192 | transit_ordinal: 0, |
| 193 | transit_lanes: 0, |
| 194 | transit_taper: 0, |
| 195 | lane_count: 0, |
| 196 | turn_fwd: Vec::new(), |
| 197 | turn_bwd: Vec::new(), |
| 198 | building: None, |
| 199 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 200 | }; |
| 201 | let (chunks, spill) = map_chunks(&[pool], 14); |
| 202 | let mut serial = super::merge(&chunks, &spill); |
| 203 | let want = collect(&mut serial); |
| 204 | assert!(!want.is_empty()); |
| 205 | let workers = 4usize; |
| 206 | let plan = super::plan_ranges(&chunks, &spill, 14, workers).expect("plan"); |
| 207 | let got = std::thread::scope(|s| { |
| 208 | let queues = super::RangeQueues::new(workers); |
| 209 | let chunks_ref = &chunks; |
| 210 | let spill_ref = &spill; |
| 211 | let plan_ref = &plan; |
| 212 | for r in 0..workers { |
| 213 | let tx = queues.sender(r); |
| 214 | s.spawn(move || { |
| 215 | super::merge_range_into(chunks_ref, spill_ref, plan_ref, r, tx); |
| 216 | }); |
| 217 | } |
| 218 | let mut coord = super::ParallelMerge::new(queues); |
| 219 | collect(&mut coord) |
| 220 | }); |
| 221 | assert_eq!(got, want, "empty ranges must not add or drop tiles"); |
| 222 | } |
| 223 | |
| 224 | /// The seek/count half of the plan, pinned directly: per-chunk |
| 225 | /// `remaining` summed over chunks must cover the range's tile-layer |
| 226 | /// entries — every (tile, layer) pair the range owns must be walked, no |
| 227 | /// fewer. The serial merge yields each tile with its layers; counting |
| 228 | /// layer entries per tile over the serial output gives the per-tile |
| 229 | /// histogram, and each range's share must be covered by the plan's rows. |
| 230 | /// |
| 231 | /// Coverage, not equality: the plan counts from the containing bucket's |
| 232 | /// cursor, so it INCLUDES the below-bound prefix the skip loop walks AND |
| 233 | /// the mid-bucket tail past the end bound that the `end_tile` stop |
| 234 | /// discards. Planned >= wanted always; planned < wanted means a starved |
| 235 | /// reader — the failure this test exists to catch. |
| 236 | #[test] |
| 237 | fn plan_seek_counts_match_the_tile_histogram() { |
| 238 | for z in [10u8, 14] { |
| 239 | let (chunks, spill) = map_chunks(&a_crowd(), z); |
| 240 | assert!(chunks.len() > 1, "the fixture must span chunks at z{z}"); |
| 241 | // Per-tile LAYER-entry counts from the serial output: one per |
| 242 | // (tile, layer) pair, which is the unit the spill — and the |
| 243 | // reader's `left` budget — counts in. |
| 244 | let mut serial = super::merge(&chunks, &spill); |
| 245 | let mut tile_entries: HashMap<u64, u64> = HashMap::new(); |
| 246 | for (tile, layers) in collect(&mut serial) { |
| 247 | *tile_entries.entry(tile).or_insert(0) += layers.len() as u64; |
| 248 | } |
| 249 | for workers in [2usize, 3, 7] { |
| 250 | let plan = |
| 251 | super::plan_ranges(&chunks, &spill, z, workers).expect("plan the ranges"); |
| 252 | // Bounds ascend and partition the zoom: first is the base, |
| 253 | // last is the end sentinel, workers+1 entries. |
| 254 | assert_eq!(plan.bounds.len(), workers + 1, "z{z} w{workers}"); |
| 255 | for w in plan.bounds.windows(2) { |
| 256 | assert!(w[0] <= w[1], "bounds ascend at z{z} w{workers}"); |
| 257 | } |
| 258 | for r in 0..workers { |
| 259 | // Entries the plan says range r walks, summed over chunks. |
| 260 | let planned: u64 = |
| 261 | plan.remaining.iter().map(|row| row[r]).sum(); |
| 262 | // Layer entries the histogram puts inside the range. |
| 263 | let want: u64 = tile_entries |
| 264 | .iter() |
| 265 | .filter(|(&t, _)| t >= plan.bounds[r] && t < plan.bounds[r + 1]) |
| 266 | .map(|(_, &n)| n) |
| 267 | .sum(); |
| 268 | assert!( |
| 269 | planned >= want, |
| 270 | "z{z} w{workers} range {r}: plan walks {planned}, histogram holds {want}" |
| 271 | ); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /// z0–z3 stay serial: their spans (1–64 tiles) don't divide the 256 cost |
| 278 | /// buckets, so range edges can't invert exactly. The zoom loop gates on |
| 279 | /// this — if the gate ever lets a low zoom through, planning must refuse. |
| 280 | #[test] |
| 281 | fn low_zooms_stay_serial() { |
| 282 | for z in [0u8, 1, 2, 3] { |
| 283 | assert!(!super::parallel_merge_supported(z), "z{z} must stay serial"); |
| 284 | } |
| 285 | for z in [4u8, 10, 14] { |
| 286 | assert!(super::parallel_merge_supported(z), "z{z} must parallelise"); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /// The full-archive version of the merge gate: the zoom loop's own |
| 291 | /// selection (`merge_gate` → plan/workers vs serial) agrees with itself |
| 292 | /// across thread counts. The unit tests above drive the planner and |
| 293 | /// workers directly; this drives `build()` end to end — so a miswired |
| 294 | /// gate can't hide behind correct components. One thread takes the |
| 295 | /// serial path (single worker cap... no: `merge_workers > 1` fails, so |
| 296 | /// serial), four threads take the range workers wherever the zoom is big |
| 297 | /// enough; both must produce the same archive. |
| 298 | #[test] |
| 299 | fn gate_paths_agree_through_build() { |
| 300 | use crate::tiler::tests::{budget, release_threads}; |
| 301 | let _guard = budget(); |
| 302 | crate::tiler::set_chunk_vertices(64); |
| 303 | let store = spilled(&a_crowd()); |
| 304 | tile_build::par::set_threads(1); |
| 305 | let serial = crate::tiler::build(&store, &settings(0, 14)).expect("one thread").0; |
| 306 | tile_build::par::set_threads(4); |
| 307 | let parallel = crate::tiler::build(&store, &settings(0, 14)).expect("four threads").0; |
| 308 | crate::tiler::set_chunk_vertices(0); |
| 309 | release_threads(); |
| 310 | assert_eq!(parallel, serial, "thread count moved the archive"); |
| 311 | } |
| 312 | } |