diff --git a/crates/analysis/src/parallel.rs b/crates/analysis/src/parallel.rs index ff42ea44f3..7d7e47bc4e 100644 --- a/crates/analysis/src/parallel.rs +++ b/crates/analysis/src/parallel.rs @@ -31,6 +31,16 @@ pub(crate) fn threads_for(items: usize, min_items: usize) -> usize { cores().min(items.div_ceil(BATCH)).max(1) } +/// How many threads to make `items` long items on, one at a time ([`try_map_each_on`]): one below +/// `min_items` and in the browser, no more than there are items, and, beyond one thread's +/// `scratch_bytes`, no more than [`scratch_budget`] holds. +pub(crate) fn threads_for_each(items: usize, min_items: usize, scratch_bytes: usize) -> usize { + if cfg!(target_arch = "wasm32") || items < min_items { + return 1; + } + cores().min(items).min(1 + scratch_budget() / scratch_bytes.max(1)).max(1) +} + /// [`threads_for`], each thread holding up to `scratch_bytes` at its peak (a feature's raster /// masks, say): beyond one thread's, no more than [`scratch_budget`] holds. pub(crate) fn threads_for_scratch(items: usize, min_items: usize, scratch_bytes: usize) -> usize { @@ -161,6 +171,29 @@ pub(crate) fn try_map_on>( scratch: impl Fn() -> S + Sync, f: impl Fn(&mut S, usize, &T) -> Result + Sync, ) -> Result, E> { + try_map_by(items, threads, BATCH, scratch, f) +} + +/// [`try_map_on`] with each thread taking one item at a time: for a few long items (a union of +/// many polygons each, say), which batches of [`BATCH`] would leave on one thread. +pub(crate) fn try_map_each_on>( + items: &[T], + threads: usize, + scratch: impl Fn() -> S + Sync, + f: impl Fn(&mut S, usize, &T) -> Result + Sync, +) -> Result, E> { + try_map_by(items, threads, 1, scratch, f) +} + +/// [`try_map_on`], each thread taking `take` items at a time. +fn try_map_by>( + items: &[T], + threads: usize, + take: usize, + scratch: impl Fn() -> S + Sync, + f: impl Fn(&mut S, usize, &T) -> Result + Sync, +) -> Result, E> { + let take = take.max(1); use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; if threads <= 1 || items.len() <= 1 { let mut s = scratch(); @@ -186,8 +219,8 @@ pub(crate) fn try_map_on>( let mut s = scratch(); let mut made = Vec::new(); while !failed.load(Ordering::Relaxed) { - let from = next.fetch_add(BATCH, Ordering::Relaxed); - let Some(batch) = items.get(from..items.len().min(from.saturating_add(BATCH))).filter(|b| !b.is_empty()) else { break }; + let from = next.fetch_add(take, Ordering::Relaxed); + let Some(batch) = items.get(from..items.len().min(from.saturating_add(take))).filter(|b| !b.is_empty()) else { break }; // Cancel is looked at, and progress counted, item by item, as on one thread: an item // can take long (a large zone, say), so a batch of them would hold both back. for (k, item) in batch.iter().enumerate() { @@ -534,6 +567,35 @@ mod tests { assert!(ran.load(std::sync::atomic::Ordering::Relaxed) < 1_000, "{}", ran.load(std::sync::atomic::Ordering::Relaxed)); } + /// A few long items each get a thread of their own, as many as there are (below the cores), + /// and come back in their places. + #[test] + fn long_items_are_taken_one_at_a_time() { + assert_eq!(threads_for_each(1, 2, 1), 1); + assert_eq!(threads_for_each(3, 2, 1), cores().min(3)); + assert_eq!(threads_for_each(3, 2, usize::MAX), 1); + let items: Vec = (0..5).collect(); + let seen = std::sync::Mutex::new(std::collections::HashSet::new()); + let made = try_map_each_on( + &items, + 5, + || (), + |_, i, &x| { + if let Ok(mut s) = seen.lock() { + s.insert(std::thread::current().id()); + } + // Long enough that the other threads start and take the rest. + std::thread::sleep(std::time::Duration::from_millis(50)); + Ok::<_, Cancelled>((i, x * 10)) + }, + ) + .unwrap(); + assert_eq!(made, vec![(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)]); + if cores() > 1 { + assert!(seen.lock().unwrap().len() > 1, "five long items stayed on one thread"); + } + } + #[test] fn no_more_threads_than_batches() { assert_eq!(threads_for(10, 256), 1); diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index c35966051c..63626e3265 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -1314,7 +1314,7 @@ fn nothing_covered(tool: &str, layer: &str, left: LeftOut) -> String { format!("{tool}: none of the {} feature(s) of '{layer}' covers anything: {what}", left.no_polygon + left.no_area) } -/// What a feature [`overlay_zones`] leaves out holds: no geometry, or a geometry [`polygon_of`] +/// What a feature [`overlay_zones_on`] leaves out holds: no geometry, or a geometry [`polygon_of`] /// gives nothing for. const NO_POLYGON: &str = "no geometry, a point or line, or an empty polygon"; @@ -1661,7 +1661,7 @@ const CLIP_BATCH: usize = if cfg!(test) { 16 } else { 4096 }; const MAX_CACHED_MASKS: usize = 8; /// The memory a union kept by Clip holds, about: its coordinates, its rings' and polygons' own -/// parts, and the key naming its masks. +/// parts, and the key naming its masks. Split sizes its areas' polygons with it, keyless. fn kept_union_bytes(key: &[usize], m: &MultiPolygon) -> usize { use std::mem::size_of; let coords = |r: &geo::LineString| r.0.capacity().saturating_mul(size_of::>()); @@ -1964,7 +1964,7 @@ fn mask_of(poly: MultiPolygon) -> MultiPolygon { if poly.0.len() > 1 { union_all(poly.0.iter()) } else { poly.orient(Direction::Default) } } -/// How an overlay tool uses its overlay polygons, for [`overlay_zones`]. +/// How an overlay tool uses its overlay polygons, for [`overlay_zones_on`]. #[derive(Clone, Copy, PartialEq, Eq)] enum ZoneUse { /// Its output comes only from inside them (Intersect, Split): with none left, it would be @@ -1991,20 +1991,10 @@ enum ZoneMade { /// unless `use_` is [`ZoneUse::AsIs`], those of several parts whose union is empty ([`NO_AREA`]), /// as they cover nothing. A polygon of one part is only oriented, its edges kept exactly, so one /// with no area stays a zone, as it always has. -fn overlay_zones<'a>( - inp: &'a dyn Inputs, - p: &Json, - k: &str, - tool: &str, - use_: ZoneUse, -) -> Result<(&'a Dataset, Vec, LeftOut), String> { - overlay_zones_on(inp, p, k, tool, use_, None) -} - -/// [`overlay_zones`] on `threads` threads (by the number of zones when `None`). A zone of several -/// parts is a union: each zone is made on its own, on several threads when there are many -/// (#1252), and they come back in order, as on one thread; Cancel is looked at, and progress -/// counted, between zones. +/// +/// On `threads` threads (by the number of zones when `None`). A zone of several parts is a union: +/// each zone is made on its own, on several threads when there are many (#1252), and they come +/// back in order, as on one thread; Cancel is looked at, and progress counted, between zones. fn overlay_zones_on<'a>( inp: &'a dyn Inputs, p: &Json, @@ -2450,9 +2440,33 @@ fn split_key(v: &Value) -> String { /// [`MAX_SPLIT_NAME`] characters, control characters dropped); one with no value is named by its /// id, as is one whose value is blank once its control characters are dropped. A name the map /// already has gets a number when the layer is added. Layers left with no features are left out; -/// selections in both layers count. Split features left out by [`overlay_zones`] are counted; with +/// selections in both layers count. Split features left out by [`overlay_zones_on`] are counted; with /// none left, it's an error. fn split(inp: &dyn Inputs, p: &Json) -> Result { + split_on(inp, p, None) +} + +/// The split areas [`split_on`] works on at a time a thread: their masks are held until their +/// layers are made. Small in tests, so they cross from one batch to the next. +const SPLIT_AREA_BATCH: usize = if cfg!(test) { 2 } else { 4 }; +/// The (area, feature) parts [`split_on`] makes at a time, so the list of them and their results +/// stay bounded however many features an area's extent takes in. Small in tests, so they cross +/// from one chunk to the next. +const SPLIT_PART_CHUNK: usize = if cfg!(test) { 64 } else { 65_536 }; + +/// [`split`] on `threads` threads (by the number of items in each step, and the memory each takes, +/// when `None`), with the same layers, bit for bit, as on one (#1252). The split polygons are made +/// into masks on several threads ([`overlay_zones_on`]). Then the input features some split area +/// meets are made ready for the overlay ([`dissolved`]) on several, once each. Then the split +/// areas, [`SPLIT_AREA_BATCH`] a thread at a time: their masks (a union for an area of several +/// polygons) on several, then every feature's part in each area on several, [`SPLIT_PART_CHUNK`] +/// parts at a time, and their layers in order. The masks' threads are as many as half the scratch +/// memory work may take holds of their batches' masks and the one each makes; the parts' as many +/// as the other half holds of twice the largest input feature's and area's coordinates. Cancel +/// is looked at, and progress counted, item by item in each step; one feature made ready, one +/// area's union and one part are each a step without a look, which can be long for an area of +/// many large polygons. +fn split_on(inp: &dyn Inputs, p: &Json, threads: Option) -> Result { let ds = input(inp, p, "input")?; if ds.geometry_type == GeometryType::None { return Err(format!("'{}' has no geometry", ds.name)); @@ -2465,7 +2479,7 @@ fn split(inp: &dyn Inputs, p: &Json) -> Result { Some(Json::String(name)) => Some(find_field_case_insensitive(input(inp, p, "split")?, name.trim())?), Some(other) => return Err(format!("Split Field must be a field name, not {other}")), }; - let (sp, zones, left_out) = overlay_zones(inp, p, "split", "Split", ZoneUse::Inside)?; + let (sp, zones, left_out) = overlay_zones_on(inp, p, "split", "Split", ZoneUse::Inside, threads)?; // The split polygons of each layer, in the order their first polygon comes, keyed by the exact // value, so values that would be named alike stay apart. A layer named by a value and one named // by an id are kept apart even when the two read the same. @@ -2500,42 +2514,145 @@ fn split(inp: &dyn Inputs, p: &Json) -> Result { ds.features.get(r).and_then(|f| f.geometry.as_ref()).is_some_and(|g| matches!(g, Geometry::Point(_) | Geometry::MultiPoint(_))) }; let points_at_poles = AtPoles::new(located.iter().map(|(r, e)| if point(*r) { poles(e) } else { (false, false) })); - // Each input feature as the overlay takes it, made once, when a split polygon first meets it. - let mut whole: Vec>> = vec![None; located.len()]; - let mut layers = Vec::new(); - let mut empty = 0usize; + // Each split area: its polygons, their extent and the poles they reach. + let areas: Vec<(String, Vec<&Zone>, Extent, (bool, bool))> = groups + .into_iter() + .map(|(name, members)| { + let polys: Vec<&Zone> = members.iter().filter_map(|zi| zones.get(*zi)).collect(); + let ext = polys.iter().fold(Extent::EMPTY, |e, z| e.union(&z.ext)); + let reached = polys.iter().map(|z| poles(&z.ext)).fold((false, false), |(n, s), (zn, zs)| (n || zn, s || zs)); + (name, polys, ext, reached) + }) + .collect(); + // The input features an area meets, by their place in `located`, in the index's order. + let met_by = |ext: &Extent, reached: (bool, bool), cand_buf: &mut Vec| { + index.candidates_into(ext, cand_buf); + points_at_poles.add_to(reached, cand_buf); + cand_buf.retain(|&li| { + located.get(li).is_some_and(|(r, e)| { + ds.features.get(*r).is_some_and(|f| f.geometry.is_some()) + && (e.intersects(ext) || (point(*r) && meet_at_a_pole(poles(e), reached))) + }) + }); + }; + // Which features some area meets, and how many parts there are to make: looked up area by + // area (the index's lookups, quick), counted area by area, with Cancel looked at for each and + // every few thousand features an area meets. + let mut needed = vec![false; located.len()]; + let mut pairs = 0usize; let mut cand_buf = Vec::new(); - // A layer each: Cancel is looked at, and progress counted, layer by layer. - crate::control::begin(groups.len()); - for (done, (name, members)) in groups.into_iter().enumerate() { + crate::control::begin(areas.len()); + for (done, (_, _, ext, reached)) in areas.iter().enumerate() { crate::control::check()?; crate::control::reached(done); - let polys: Vec<&Zone> = members.iter().filter_map(|zi| zones.get(*zi)).collect(); - let mask = match polys.as_slice() { - [only] => std::borrow::Cow::Borrowed(&only.poly), - _ => std::borrow::Cow::Owned(union_all(polys.iter().flat_map(|z| z.poly.0.iter()))), - }; - let ext = polys.iter().fold(Extent::EMPTY, |e, z| e.union(&z.ext)); - let reached = polys.iter().map(|z| poles(&z.ext)).fold((false, false), |(n, s), (zn, zs)| (n || zn, s || zs)); - let mut out = copy_schema(ds, format!("{prefix}_{name}"), ds.geometry_type); - keep_source_crs(&mut out, ds, sp); - index.candidates_into(&ext, &mut cand_buf); - points_at_poles.add_to(reached, &mut cand_buf); - for &li in &cand_buf { - // A layer can take most of the input: Cancel is looked at feature by feature too. - crate::control::check()?; - let Some((r, e)) = located.get(li) else { continue }; - let Some(f) = ds.features.get(*r) else { continue }; - let meets = e.intersects(&ext) || (point(*r) && meet_at_a_pole(poles(e), reached)); - let (Some(g), Some(slot)) = (f.geometry.as_ref().filter(|_| meets), whole.get_mut(li)) else { continue }; - if let Some(c) = part(slot.get_or_insert_with(|| dissolved(g)), &mask, true) { + met_by(ext, *reached, &mut cand_buf); + pairs = pairs.saturating_add(cand_buf.len()); + for (k, &li) in cand_buf.iter().enumerate() { + if k % 4096 == 4095 { + crate::control::check()?; + } + if let Some(n) = needed.get_mut(li) { + *n = true; + } + } + } + let needed: Vec = needed.iter().enumerate().filter_map(|(li, &n)| n.then_some(li)).collect(); + let largest_feature = + needed.iter().filter_map(|&li| ds.features.get(located.get(li)?.0)?.geometry.as_ref()).map(coord_bytes).max().unwrap_or(0); + let area_bytes = |polys: &[&Zone]| polys.iter().map(|z| kept_union_bytes(&[], &z.poly)).fold(0usize, usize::saturating_add); + let largest_area = areas.iter().map(|(_, polys, ..)| area_bytes(polys)).max().unwrap_or(0); + // Only an area of several polygons has its mask made (a union); one polygon's is borrowed. + let largest_union = areas.iter().filter(|(_, polys, ..)| polys.len() > 1).map(|(_, polys, ..)| area_bytes(polys)).max().unwrap_or(0); + let threads_for = |n: usize, scratch: usize| { + threads.unwrap_or_else(|| crate::parallel::threads_for_scratch(n, PARALLEL_FEATURES, scratch.saturating_mul(2))) + }; + // Counted item by item: the features made ready, the areas' masks and the parts. + crate::control::begin(needed.len().saturating_add(areas.len()).saturating_add(pairs)); + // Each input feature as the overlay takes it, made once. + let made = crate::parallel::try_map_on( + &needed, + threads_for(needed.len(), largest_feature), + || (), + |_, _, &li| Ok::<_, String>(located.get(li).and_then(|(r, _)| ds.features.get(*r)?.geometry.as_ref()).map(dissolved)), + )?; + let mut whole: Vec>> = vec![None; located.len()]; + for (li, w) in needed.iter().zip(made) { + if let Some(slot) = whole.get_mut(*li) { + *slot = w; + } + } + // A mask is a union of one area's polygons, worth a thread of its own from two areas on, each + // thread taking one area at a time; a thread holds its batch's masks and the one it makes. + // Doubled, as `threads_for` doubles: the masks take at most half the scratch memory, the + // parts' work the other half. + let mask_threads = threads + .unwrap_or_else(|| { + crate::parallel::threads_for_each(areas.len(), 2, largest_union.saturating_mul(SPLIT_AREA_BATCH + 1).saturating_mul(2)) + }) + .max(1); + // A part's work: the feature and its part, and the overlay's own copy of the mask, about. + let part_work = largest_feature.saturating_add(largest_area).saturating_mul(2); + let mut layers = Vec::new(); + let mut empty = 0usize; + for batch in areas.chunks(mask_threads.saturating_mul(SPLIT_AREA_BATCH)) { + // The batch's masks: one area's polygon as it is, several unioned. + let masks = crate::parallel::try_map_each_on( + batch, + mask_threads, + || (), + |_, _, (_, polys, ..)| { + Ok::<_, String>(match polys.as_slice() { + [only] => std::borrow::Cow::Borrowed(&only.poly), + _ => std::borrow::Cow::Owned(union_all(polys.iter().flat_map(|z| z.poly.0.iter()))), + }) + }, + )?; + let mut outs: Vec = batch + .iter() + .map(|(name, ..)| { + let mut out = copy_schema(ds, format!("{prefix}_{name}"), ds.geometry_type); + keep_source_crs(&mut out, ds, sp); + out + }) + .collect(); + // Every feature each area meets, area by area in order, their parts made on several + // threads [`SPLIT_PART_CHUNK`] at a time and added to their layers in that order. + let make = |work: &[(usize, usize)], outs: &mut Vec| -> Result<(), String> { + let parts = crate::parallel::try_map_on( + work, + threads_for(work.len(), part_work), + || (), + |_, _, &(ai, li)| { + let (Some(mask), Some(Some(g))) = (masks.get(ai), whole.get(li)) else { return Ok::<_, String>(None) }; + Ok(part(g, mask, true)) + }, + )?; + for (&(ai, li), c) in work.iter().zip(parts) { + let (Some(out), Some(c), Some(f)) = (outs.get_mut(ai), c, located.get(li).and_then(|(r, _)| ds.features.get(*r))) else { + continue; + }; out.push_next(Some(c), f.attrs.clone()); } + Ok(()) + }; + let mut work: Vec<(usize, usize)> = Vec::new(); + for (ai, (_, _, ext, reached)) in batch.iter().enumerate() { + met_by(ext, *reached, &mut cand_buf); + for &li in &cand_buf { + work.push((ai, li)); + if work.len() >= SPLIT_PART_CHUNK { + make(&work, &mut outs)?; + work.clear(); + } + } } - if out.features.is_empty() { - empty += 1; - } else { - layers.push(OutputLayer::Vector(out)); + make(&work, &mut outs)?; + for out in outs { + if out.features.is_empty() { + empty += 1; + } else { + layers.push(OutputLayer::Vector(out)); + } } } let mut msg = format!("Split: {} layers", layers.len()); @@ -12088,6 +12205,78 @@ mod tests { } } + /// Split gives the same layers, bit for bit, on several threads as on one (#1252): areas of one + /// polygon and of several (by a field), polygons of one and of several parts, lines and points, + /// features without geometry, an area that holds nothing, and, at the tests' + /// [`SPLIT_AREA_BATCH`] and [`SPLIT_PART_CHUNK`], several batches of areas and chunks of parts; + /// the threads set or found by the sizes. + #[test] + fn split_on_several_threads_is_on_one() { + let mut input = Dataset::new("input", GeometryType::Polygon, vec![Field::new("v", FieldType::Integer)]); + let mut lines = Dataset::new("lines", GeometryType::Polyline, vec![Field::new("v", FieldType::Integer)]); + let mut points = Dataset::new("points", GeometryType::Point, vec![Field::new("v", FieldType::Integer)]); + let mut areas = Dataset::new("areas", GeometryType::Polygon, vec![Field::new("zone", FieldType::Integer)]); + for i in 0..600i64 { + let (x, y) = ((i * 37 % 300) as f64 * 0.1, (i * 53 % 120) as f64 * 0.1); + let none = i % 97 == 0; + let poly = if i % 7 == 0 { + let ring = |x: f64| { + vec![Coord::new(x, y), Coord::new(x + 0.4, y), Coord::new(x + 0.4, y + 0.4), Coord::new(x, y + 0.4), Coord::new(x, y)] + }; + Geometry::Polygon(vec![vec![ring(x)], vec![ring(x + 0.25)]]) + } else { + sq(x, y, 0.35) + }; + input.push(if none { None } else { Some(poly) }, vec![Value::Int(i)]); + let line = Geometry::Line(vec![vec![Coord::new(x, y), Coord::new(x + 0.9, y + 0.3)]]); + lines.push(if none { None } else { Some(line) }, vec![Value::Int(i)]); + points.push(if none { None } else { Some(Geometry::Point(Coord::new(x + 0.05, y + 0.05))) }, vec![Value::Int(i)]); + } + // A grid of areas over the features, every fourth zone value shared by two areas, and one + // area far from everything. + for i in 0..40i64 { + let (x, y) = ((i % 8) as f64 * 4.0, (i / 8) as f64 * 2.5); + areas.push(Some(sq(x, y, 4.2)), vec![Value::Int(if i % 4 == 0 { 1000 } else { i })]); + } + areas.push(Some(sq(80.0, 80.0, 1.0)), vec![Value::Int(2000)]); + let m = Map(HashMap::from([ + ("input".to_string(), input), + ("lines".to_string(), lines), + ("points".to_string(), points), + ("areas".to_string(), areas), + ])); + let layers = |r: ToolResult| match r.output { + Output::Layers(ls) => ls + .into_iter() + .map(|l| match l { + OutputLayer::Vector(d) => { + format!("{} {:?}", d.name, d.features.iter().map(|f| (&f.geometry, &f.attrs)).collect::>()) + } + _ => panic!("not a vector layer"), + }) + .collect::>(), + _ => panic!("not layers"), + }; + for name in ["input", "lines", "points"] { + for field in [None, Some("zone")] { + let mut p = serde_json::json!({"input": name, "split": "areas"}); + if let Some(f) = field { + p["field"] = f.into(); + } + let one = split_on(&m, &p, Some(1)).unwrap(); + let one_messages = one.messages.clone(); + assert!(one_messages.iter().any(|m| m.contains("held no features")), "{name}, {field:?}: {one_messages:?}"); + let one = layers(one); + assert!(one.len() > 10, "{name}, {field:?}: {}", one.len()); + for threads in [Some(2), Some(7), Some(16), None] { + let many = split_on(&m, &p, threads).unwrap(); + assert_eq!(many.messages, one_messages, "{name}, {field:?}, {threads:?} threads"); + assert_eq!(layers(many), one, "{name}, {field:?}, {threads:?} threads"); + } + } + } + } + /// Clip says how many input features it left out for having no geometry, or an empty one, and /// says nothing of it when all have one. #[test]