From 90f2c3d33cba3ce65e72600475953437de02b49b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 09:58:40 +0100 Subject: [PATCH 1/2] feat(analysis): a run that can be cancelled takes no work cap (#1291 step 2) Viewshed, IDW, Kriging, Kernel Density and Deep Learning refused runs past a second or two of work because nothing could stop them. Run from the Tools or Analysis pane they now go on a thread with progress and Cancel, so a stoppable Control (the panes' runners, and rustgis-cli run, which Ctrl-C stops) lifts those caps. Scripts, models, the assistant, Python and the web app keep them; refusals there say where the work isn't capped. Without a cap one row can take long, so IDW, Kriging, Viewshed and the classifier look at the cancel flag within a row (control::Pace), and training looks at it every mini-batch. Memory caps and default cell sizes are unchanged. --- crates/analysis/src/control.rs | 56 ++++++++++++++++ crates/analysis/src/density.rs | 15 +++-- crates/analysis/src/kriging.rs | 34 ++++++++-- crates/analysis/src/neural.rs | 62 +++++++++++++---- crates/analysis/src/parallel.rs | 13 +++- crates/analysis/src/rasterops.rs | 97 +++++++++++++++------------ crates/engine/src/cmd/deeplearning.rs | 11 ++- crates/engine/src/cmd/gp.rs | 8 ++- crates/engine/src/cmd/workflows.rs | 9 ++- crates/engine/src/lib.rs | 2 +- crates/ui-egui/src/analysispane.rs | 3 +- crates/ui-egui/src/panes.rs | 3 +- docs/user-guide/analysis.md | 34 +++++++--- 13 files changed, 260 insertions(+), 87 deletions(-) diff --git a/crates/analysis/src/control.rs b/crates/analysis/src/control.rs index 35da091ab5..76cde698c3 100644 --- a/crates/analysis/src/control.rs +++ b/crates/analysis/src/control.rs @@ -4,6 +4,10 @@ //! the tool has come. The run's control is the current one of the thread the tool runs on, so a //! tool asks [`check`] in its long loops without passing it along; the threads of //! [`crate::parallel`] take it over from the thread that starts them. +//! +//! A run someone can stop while it goes ([`Control::stoppable`]: the app's panes, `rustgis-cli +//! run`) takes no work cap ([`uncapped`]): the caps, a second or two of work, are for the calls +//! that wait on a tool with no way to stop it (a script, the assistant, the web app). use std::cell::RefCell; use std::sync::Arc; @@ -15,6 +19,8 @@ pub struct Control { cancel: AtomicBool, done: AtomicU64, total: AtomicU64, + /// Someone can stop the run while it goes ([`Control::stoppable`]). + stoppable: bool, } impl Control { @@ -22,6 +28,12 @@ impl Control { Arc::new(Control::default()) } + /// A control for a run someone can stop while it goes, with a Cancel button or Ctrl-C: its + /// tools take no work cap ([`uncapped`]). Not in the browser, whose one thread a run holds. + pub fn stoppable() -> Arc { + Arc::new(Control { stoppable: !cfg!(target_arch = "wasm32"), ..Control::default() }) + } + /// Asks the tool to stop at its next check. pub fn cancel(&self) { self.cancel.store(true, Ordering::Relaxed); @@ -89,6 +101,33 @@ pub(crate) fn cancelled() -> bool { CURRENT.with(|c| c.borrow().as_ref().is_some_and(|c| c.is_cancelled())) } +/// Whether the run on this thread can be stopped while it goes ([`Control::stoppable`]), and so +/// takes no work cap. +pub(crate) fn uncapped() -> bool { + CURRENT.with(|c| c.borrow().as_ref().is_some_and(|c| c.stoppable)) +} + +/// The work units (a distance, a ray step, a multiply-add) between two looks at the cancel flag +/// inside an item of a loop ([`Pace`]): a few milliseconds. +const PACE_WORK: usize = 1 << 22; + +/// Looks at the cancel flag about every [`PACE_WORK`] units of work, within an item that may +/// take long on its own (a row of a wide grid without a work cap), so Cancel stops the run soon. +#[derive(Default)] +pub(crate) struct Pace(usize); + +impl Pace { + /// Whether to stop after `work` more units: once the run was asked to. + pub(crate) fn stop(&mut self, work: usize) -> bool { + self.0 = self.0.saturating_add(work); + if self.0 < PACE_WORK { + return false; + } + self.0 = 0; + cancelled() + } +} + /// Stops the tool when its run was asked to: call it between rows or features of a long loop. pub(crate) fn check() -> Result<(), Cancelled> { if cancelled() { Err(Cancelled) } else { Ok(()) } @@ -159,6 +198,23 @@ mod tests { assert!(current().is_none() && check().is_ok()); } + #[test] + fn only_a_stoppable_run_is_uncapped_and_pace_sees_its_cancel() { + assert!(!uncapped(), "no run"); + { + let _g = install(Some(Control::new())); + assert!(!uncapped(), "a run nobody can stop keeps its caps"); + } + let c = Control::stoppable(); + let _g = install(Some(c.clone())); + assert!(uncapped()); + let mut pace = Pace::default(); + assert!(!pace.stop(PACE_WORK), "not cancelled"); + c.cancel(); + assert!(!pace.stop(1), "not looked at again before PACE_WORK more"); + assert!(pace.stop(PACE_WORK)); + } + #[test] fn no_progress_before_a_step_is_counted() { let c = Control::new(); diff --git a/crates/analysis/src/density.rs b/crates/analysis/src/density.rs index 8a3cf9df49..e620b295d4 100644 --- a/crates/analysis/src/density.rs +++ b/crates/analysis/src/density.rs @@ -47,8 +47,8 @@ pub fn max_density_cells() -> usize { } /// Upper limit on aggregate cell visits across all points on one thread, to prevent CPU -/// exhaustion; a run on several threads may take as many times more -/// ([`crate::parallel::work_cap`]). +/// exhaustion; a run on several threads may take as many times more, and one that can be +/// cancelled any amount ([`crate::parallel::work_cap`]). pub const MAX_DENSITY_WORK: usize = 50_000_000; /// The points between two counts of a strip's progress and checks for a cancel. @@ -335,8 +335,9 @@ fn kernel_density_on( let cap = crate::parallel::work_cap(MAX_DENSITY_WORK, threads); if total_work > cap { return Err(format!( - "{w} × {h_rows} cells from {} points is too much work, more than the {cap} cell visits this computer takes on {threads} thread(s); choose a larger pixel size or a smaller search radius", - points.len() + "{w} × {h_rows} cells from {} points is too much work, more than the {cap} cell visits this computer takes on {threads} thread(s); choose a larger pixel size or a smaller search radius{}", + points.len(), + crate::parallel::UNCAPPED_HINT )); } @@ -552,6 +553,12 @@ mod tests { err.contains("too much work") && err.contains(&format!("the {MAX_DENSITY_WORK} cell visits")) && err.contains("1 thread(s)"), "{err}" ); + // A run that can be cancelled takes it on: cancelled before it starts, it says so. + let run = crate::control::Control::stoppable(); + run.cancel(); + let _installed = crate::control::install(Some(run)); + let err = kernel_density_on(Some(1), &pts, Some(&ext), 10.0, 1000.0, KernelType::Quartic, "huge", Crs::WebMercator).unwrap_err(); + assert_eq!(err, crate::control::CANCELLED); } #[test] diff --git a/crates/analysis/src/kriging.rs b/crates/analysis/src/kriging.rs index 271dc9bcc1..e9edcf9221 100644 --- a/crates/analysis/src/kriging.rs +++ b/crates/analysis/src/kriging.rs @@ -15,7 +15,8 @@ use rustgis_geom::{Coord, Extent}; use crate::rasterops::{NODATA, max_pixels}; /// Maximum cell-point pairs Kriging computes on one thread (about a second's work); a run on several -/// threads may take as many times more ([`crate::parallel::work_cap`]). +/// threads may take as many times more, and one that can be cancelled any amount +/// ([`crate::parallel::work_cap`]). pub const MAX_KRIGING_WORK: usize = 40_000_000; /// Theoretical semivariogram models for spatial autocorrelation. @@ -353,17 +354,22 @@ fn kriging_on( let cap = crate::parallel::work_cap(MAX_KRIGING_WORK, threads); if work > cap { return Err(format!( - "{w} × {h} cells from {} points is too much work, more than the {cap} this computer takes on {threads} thread(s); use a larger cell size", - points.len() + "{w} × {h} cells from {} points is too much work, more than the {cap} this computer takes on {threads} thread(s); use a larger cell size{}", + points.len(), + crate::parallel::UNCAPPED_HINT )); } - let scratch = || (Vec::<(f64, f64, Coord)>::with_capacity(points.len()), KrigingBuffers::new()); + let scratch = || (Vec::<(f64, f64, Coord)>::with_capacity(points.len()), KrigingBuffers::new(), crate::control::Pace::default()); let (mut pred_data, mut var_data) = (crate::rasterops::new_band(w, h, NODATA)?, crate::rasterops::new_band(w, h, NODATA)?); let rows = pred_data.chunks_mut(w).zip(var_data.chunks_mut(w)); crate::control::begin(h); - crate::parallel::for_each_row(rows, threads, scratch, |(near, buffers), row, (pred, var): (&mut [f32], &mut [f32])| { + crate::parallel::for_each_row(rows, threads, scratch, |(near, buffers, pace), row, (pred, var): (&mut [f32], &mut [f32])| { let kx = (extent.max_y - (row as f64 + 0.5) * cell_deg).to_radians().cos().abs().max(1e-6); for (col, (pred, var)) in pred.iter_mut().zip(var.iter_mut()).enumerate() { + // A wide row of many points takes long without a cap: Cancel is looked at within it. + if pace.stop(work_per_cell) { + return; + } let c = Coord::new(extent.min_x + (col as f64 + 0.5) * cell_deg, extent.max_y - (row as f64 + 0.5) * cell_deg); near.clear(); near.extend(points.iter().map(|(p, v)| { @@ -393,6 +399,24 @@ fn kriging_on( mod tests { use super::*; + /// A run past one thread's work is refused where nothing can stop it, and taken on by a run + /// that can be cancelled (#1291 step 2). + #[test] + fn kriging_past_the_cap_runs_only_when_it_can_be_cancelled() { + // 500 × 500 cells from 300 points (and 12 neighbours): about 220 million units, past one + // thread's 40 million. + let pts: Vec<(Coord, f64)> = + (0..300).map(|i| (Coord::new((i % 20) as f64 / 2.0, 40.0 + (i / 20) as f64 / 1.5), i as f64)).collect(); + let extent = Extent::new(0.0, 40.0, 10.0, 50.0); + let params = auto_variogram(&pts, &extent, VariogramModel::Spherical); + let e = kriging_on(Some(1), &pts, &extent, 0.02, ¶ms, 12, "k").unwrap_err(); + assert!(e.contains("too much work") && e.contains("on 1 thread(s)"), "{e}"); + let run = crate::control::Control::stoppable(); + run.cancel(); + let _installed = crate::control::install(Some(run)); + assert_eq!(kriging_on(Some(1), &pts, &extent, 0.02, ¶ms, 12, "k").unwrap_err(), crate::control::CANCELLED); + } + #[test] fn test_exact_interpolation_at_sample_points() { let sample_pts = vec![ diff --git a/crates/analysis/src/neural.rs b/crates/analysis/src/neural.rs index af1dacbf3a..a222ab11fd 100644 --- a/crates/analysis/src/neural.rs +++ b/crates/analysis/src/neural.rs @@ -15,7 +15,8 @@ pub const MAX_EPOCHS: usize = 500; /// Most classes and input bands. pub const MAX_CLASSES: usize = 64; pub const MAX_INPUTS: usize = 64; -/// Most multiply-adds one training may do (seconds, not minutes): epochs are reduced to fit. +/// Most multiply-adds one training may do (seconds, not minutes): epochs are reduced to fit. A +/// run that can be cancelled takes any amount ([`crate::control::uncapped`]). pub const MAX_WORK: f64 = 5e9; /// One dense layer: `out × inp` weights (row per output) and `out` biases. @@ -217,7 +218,8 @@ pub struct Trained { pub model: Mlp, pub accuracy: f64, pub samples: usize, - /// The epochs run (fewer than asked when the work would pass [`MAX_WORK`]). + /// The epochs run (fewer than asked when the work would pass [`MAX_WORK`] in a run that can't + /// be cancelled). pub epochs: usize, } @@ -285,25 +287,28 @@ pub fn train( let inputs: Vec> = samples.iter().map(|(x, _)| scaled(x)).collect(); // Mini-batch gradient descent with momentum. // Each sample costs about three passes over the weights (forward, backward, update). - // Too many samples for even one epoch: a random subset (they're shuffled) is fitted. + // Too many samples for even one epoch: a random subset (they're shuffled) is fitted. A run + // that can be cancelled fits them all, for every epoch asked. let per_sample = 3.0 * layers.iter().map(|l| l.w.len() + l.b.len()).sum::() as f64; + let max_work = if crate::control::uncapped() { f64::INFINITY } else { MAX_WORK }; let mut fit = fit; - fit.truncate(((MAX_WORK / per_sample) as usize).max(1)); + fit.truncate(((max_work / per_sample).min(usize::MAX as f64) as usize).max(1)); let per_epoch = per_sample * fit.len().max(1) as f64; - let epochs = epochs.clamp(1, MAX_EPOCHS).min(((MAX_WORK / per_epoch) as usize).max(1)); + let epochs = epochs.clamp(1, MAX_EPOCHS).min(((max_work / per_epoch).min(usize::MAX as f64) as usize).max(1)); let (lr, momentum, batch) = (0.05f32, 0.9f32, 32usize); let mut vel: Vec<(Vec, Vec)> = layers.iter().map(|l| (vec![0.0; l.w.len()], vec![0.0; l.b.len()])).collect(); crate::control::begin(epochs); - for epoch in 0..epochs { - if crate::control::cancelled() { - break; - } + 'epochs: for epoch in 0..epochs { crate::control::reached(epoch); for i in (1..fit.len()).rev() { let j = rng.below(i + 1); fit.swap(i, j); } for chunk in fit.chunks(batch) { + // An epoch of every sample takes long without a cap: Cancel is looked at each batch. + if crate::control::cancelled() { + break 'epochs; + } let mut grads: Vec<(Vec, Vec)> = layers.iter().map(|l| (vec![0.0; l.w.len()], vec![0.0; l.b.len()])).collect(); for &si in chunk { let (Some(x), Some((_, class))) = (inputs.get(si), samples.get(si)) else { continue }; @@ -389,10 +394,14 @@ pub fn classify(r: &Raster, model: &Mlp, nodata: f32) -> Result<(Vec, Vec) -> Result<(Vec, Vec), String> { model.validate()?; - // A large model on a large raster is refused rather than run for minutes. + // A large model on a large raster is refused rather than run for minutes, unless the run can + // be cancelled. let weights: usize = model.layers.iter().map(|l| l.w.len() + l.b.len()).sum(); - if f64::from(r.width) * f64::from(r.height) * weights as f64 > 4.0 * MAX_WORK { - return Err("that model is too large for this raster; use a smaller model or clip the raster first".into()); + if !crate::control::uncapped() && f64::from(r.width) * f64::from(r.height) * weights as f64 > 4.0 * MAX_WORK { + return Err(format!( + "that model is too large for this raster; use a smaller model or clip the raster first{}", + crate::parallel::UNCAPPED_HINT + )); } if let Some(b) = model.bands.iter().find(|b| **b >= r.bands.len()) { return Err(format!("{} has no band {}", r.name, b + 1)); @@ -408,9 +417,14 @@ fn classify_on(r: &Raster, model: &Mlp, nodata: f32, threads: Option) -> crate::parallel::for_each_row( class.chunks_mut(w.max(1)).zip(prob.chunks_mut(w.max(1))), threads, - || vec![0.0f32; model.bands.len()], - |x: &mut Vec, row, (class, prob): (&mut [f32], &mut [f32])| { + || (vec![0.0f32; model.bands.len()], crate::control::Pace::default()), + |(x, pace): &mut (Vec, crate::control::Pace), row, (class, prob): (&mut [f32], &mut [f32])| { for (col, (c_out, p_out)) in class.iter_mut().zip(prob.iter_mut()).enumerate() { + // A large model on a wide row takes long without a cap: Cancel is looked at + // within it. + if pace.stop(weights) { + return; + } // A non-finite value is treated as no data. let ok = model.bands.iter().zip(x.iter_mut()).all(|(b, xv)| { match r.value(*b, col as i64, row as i64).filter(|v| v.is_finite()) { @@ -472,6 +486,26 @@ mod tests { assert_eq!((c, p), (c1, p1)); } + /// A model too large for a raster is refused where nothing can stop the run, and taken on by + /// a run that can be cancelled (#1291 step 2). + #[test] + fn a_large_model_on_a_large_raster_runs_only_when_it_can_be_cancelled() { + use rustgis_data::raster::{ColorHint, Crs, GeoTransform}; + let samples: Vec<(Vec, usize)> = (0..40).map(|i| (vec![i as f32], usize::from(i >= 20))).collect(); + // About 5,500 weights on 2000 × 2000 pixels: past four times MAX_WORK. + let model = train(&[0], vec!["a".into(), "b".into()], &samples, &[80, 64], 1).unwrap().model; + let (w, h) = (2000u32, 2000u32); + let transform = GeoTransform { origin_x: 0.0, origin_y: 45.0, pixel_width: 0.001, pixel_height: -0.001 }; + let r = + Raster::new("img", w, h, vec![vec![1.0; (w * h) as usize]], transform, Crs::LonLat, None, ColorHint::Gray, "Float32").unwrap(); + let e = classify_on(&r, &model, -1.0, Some(1)).unwrap_err(); + assert!(e.starts_with("that model is too large for this raster"), "{e}"); + let run = crate::control::Control::stoppable(); + run.cancel(); + let _installed = crate::control::install(Some(run)); + assert_eq!(classify_on(&r, &model, -1.0, Some(1)).unwrap_err(), crate::control::CANCELLED); + } + #[test] fn learns_a_nonlinear_boundary_and_validates() { // Class 1 inside a ring, class 0 outside: not separable by a line. diff --git a/crates/analysis/src/parallel.rs b/crates/analysis/src/parallel.rs index abbaba511f..d01000e5db 100644 --- a/crates/analysis/src/parallel.rs +++ b/crates/analysis/src/parallel.rs @@ -60,11 +60,20 @@ pub(crate) fn threads_for_rows(rows: usize, work: usize, scratch_bytes: usize) - /// A work cap tuned for one thread (a second or two of its work) for a run on `threads` threads: /// as many times larger, since the threads share the work and the run takes about as long -/// (#1291 step 2). On one thread, and so in the browser, the cap as it is. +/// (#1291 step 2). On one thread, and so in the browser, the cap as it is. None for a run that +/// can be stopped while it goes ([`crate::control::uncapped`]): its progress shows, and Cancel +/// stops it. pub(crate) fn work_cap(one_thread: usize, threads: usize) -> usize { - one_thread.saturating_mul(threads.max(1)) + if crate::control::uncapped() { usize::MAX } else { one_thread.saturating_mul(threads.max(1)) } } +/// What a refusal past [`work_cap`] adds: where the work isn't capped. Nothing in the browser. +pub(crate) const UNCAPPED_HINT: &str = if cfg!(target_arch = "wasm32") { + "" +} else { + " (or run it from its pane in the desktop app, or with rustgis-cli run, which take any amount of work and can be cancelled)" +}; + /// The cores to use, at most [`MAX_THREADS`]. fn cores() -> usize { std::thread::available_parallelism().map_or(1, |n| n.get()).min(MAX_THREADS) diff --git a/crates/analysis/src/rasterops.rs b/crates/analysis/src/rasterops.rs index 887db64e68..3fe4c2968e 100644 --- a/crates/analysis/src/rasterops.rs +++ b/crates/analysis/src/rasterops.rs @@ -429,8 +429,8 @@ pub fn line_of_sight( } /// Most ray steps a viewshed takes, and cell-point pairs an interpolation weighs, on one thread -/// (each about a second or two); a run on several threads may take as many times more -/// ([`crate::parallel::work_cap`]). +/// (each about a second or two); a run on several threads may take as many times more, and one +/// that can be cancelled any amount ([`crate::parallel::work_cap`]). const MAX_VIEWSHED_WORK: usize = 400_000_000; pub(crate) const MAX_IDW_WORK: usize = 400_000_000; @@ -468,52 +468,52 @@ fn viewshed_on(threads: Option, r: &Raster, b: usize, observer: Coord, he let cap = crate::parallel::work_cap(MAX_VIEWSHED_WORK, threads); if ray_work > cap { return Err(format!( - "the radius takes {ray_work} ray steps, more than the {cap} this computer takes on {threads} thread(s); use a smaller radius" + "the radius takes {ray_work} ray steps, more than the {cap} this computer takes on {threads} thread(s); use a smaller radius{}", + crate::parallel::UNCAPPED_HINT )); } let w = r.width as usize; let mut out = new_band(w, r.height as usize, NODATA)?; let window = out.chunks_mut(w).skip(usize::try_from(first).unwrap_or(0)).take(rows); crate::control::begin(rows); - crate::parallel::for_each_row( - window, - threads, - || (), - |_, i, line: &mut [f32]| { - let row = first + i as i64; - for col in (oc - rad_c).max(0)..=(oc + rad_c).min(r.width as i64 - 1) { - let (dx, dy) = ((col - oc) as f64 * cx, (row - or) as f64 * cy); - let dist = dx.hypot(dy); - if dist > radius_m { - continue; - } - let Some(zt) = r.value(b, col, row) else { continue }; - if dist == 0.0 { - if let Some(o) = line.get_mut(col as usize) { - *o = 1.0; - } - continue; - } - let target = (zt as f64 - dist * dist / (2.0 * SIGHT_RADIUS_M) - z0) / dist; - // Walk the ray from the observer; any cell higher than the line hides the target. - let steps = ((col - oc).abs().max((row - or).abs())) as usize; - let mut visible = true; - for s in 1..steps { - let t = s as f64 / steps as f64; - let (c, rr) = ((oc as f64 + (col - oc) as f64 * t).round() as i64, (or as f64 + (row - or) as f64 * t).round() as i64); - let Some(zi) = r.value(b, c, rr) else { continue }; - let di = dist * t; - if (zi as f64 - di * di / (2.0 * SIGHT_RADIUS_M) - z0) / di > target { - visible = false; - break; - } - } + crate::parallel::for_each_row(window, threads, crate::control::Pace::default, |pace, i, line: &mut [f32]| { + let row = first + i as i64; + for col in (oc - rad_c).max(0)..=(oc + rad_c).min(r.width as i64 - 1) { + let (dx, dy) = ((col - oc) as f64 * cx, (row - or) as f64 * cy); + let dist = dx.hypot(dy); + if dist > radius_m { + continue; + } + let Some(zt) = r.value(b, col, row) else { continue }; + if dist == 0.0 { if let Some(o) = line.get_mut(col as usize) { - *o = if visible { 1.0 } else { 0.0 }; + *o = 1.0; + } + continue; + } + let target = (zt as f64 - dist * dist / (2.0 * SIGHT_RADIUS_M) - z0) / dist; + // Walk the ray from the observer; any cell higher than the line hides the target. + let steps = ((col - oc).abs().max((row - or).abs())) as usize; + // A wide window's row takes long without a cap: Cancel is looked at within it. + if pace.stop(steps) { + return; + } + let mut visible = true; + for s in 1..steps { + let t = s as f64 / steps as f64; + let (c, rr) = ((oc as f64 + (col - oc) as f64 * t).round() as i64, (or as f64 + (row - or) as f64 * t).round() as i64); + let Some(zi) = r.value(b, c, rr) else { continue }; + let di = dist * t; + if (zi as f64 - di * di / (2.0 * SIGHT_RADIUS_M) - z0) / di > target { + visible = false; + break; } } - }, - )?; + if let Some(o) = line.get_mut(col as usize) { + *o = if visible { 1.0 } else { 0.0 }; + } + } + })?; Ok(out) } @@ -559,8 +559,9 @@ fn idw_on( let cap = crate::parallel::work_cap(MAX_IDW_WORK, threads); if work > cap { return Err(format!( - "{w} × {h} cells from {} points is too much work, more than the {cap} cell-point pairs this computer takes on {threads} thread(s); use a larger cell size", - points.len() + "{w} × {h} cells from {} points is too much work, more than the {cap} cell-point pairs this computer takes on {threads} thread(s); use a larger cell size{}", + points.len(), + crate::parallel::UNCAPPED_HINT )); } let mut data = new_band(w, h, NODATA)?; @@ -568,11 +569,16 @@ fn idw_on( crate::parallel::for_each_row( data.chunks_mut(w), threads, - || Vec::with_capacity(points.len()), - |near: &mut Vec<(f64, f64)>, row, out: &mut [f32]| { + || (Vec::with_capacity(points.len()), crate::control::Pace::default()), + |(near, pace): &mut (Vec<(f64, f64)>, crate::control::Pace), row, out: &mut [f32]| { // East-west degrees shrink with latitude, which only changes by row. let kx = (extent.max_y - (row as f64 + 0.5) * cell_deg).to_radians().cos(); for (col, o) in out.iter_mut().enumerate() { + // Each cell weighs every point, so a wide row of many points takes long without a + // cap: Cancel is looked at within it. + if pace.stop(points.len()) { + return; + } let c = Coord::new(extent.min_x + (col as f64 + 0.5) * cell_deg, extent.max_y - (row as f64 + 0.5) * cell_deg); near.clear(); near.extend(points.iter().map(|(p, v)| { @@ -1548,6 +1554,11 @@ mod tests { e.contains("too much work") && e.contains(&format!("{MAX_IDW_WORK} cell-point pairs")) && e.contains("on 1 thread(s)"), "{e}" ); + // A run that can be cancelled takes it on: cancelled before its first row, it says so. + let run = crate::control::Control::stoppable(); + run.cancel(); + let _installed = crate::control::install(Some(run)); + assert_eq!(idw_on(Some(1), &pts, &extent, 0.01, 2.0, 0, "idw").unwrap_err(), crate::control::CANCELLED); } /// Rows made on several threads give the same surface, bit for bit, as one thread (#1252), diff --git a/crates/engine/src/cmd/deeplearning.rs b/crates/engine/src/cmd/deeplearning.rs index b0998667a4..14c412971e 100644 --- a/crates/engine/src/cmd/deeplearning.rs +++ b/crates/engine/src/cmd/deeplearning.rs @@ -142,9 +142,16 @@ fn model_index(s: &Session, p: &Value) -> Result { } } -/// Trains a model from the pixels under training polygons. +/// Trains a model from the pixels under training polygons. Where only a shell waits on it +/// ([`Session::long_runs`](crate::Session::long_runs)), Ctrl-C stops it, so it takes no work cap, +/// as from the pane. fn train(s: &Session, p: &Value) -> Result { - training(s, p)?.fit() + let t = training(s, p)?; + if s.long_runs.is_some() { + rustgis_analysis::run_under("Train Deep Learning Model", &rustgis_analysis::control::Control::stoppable(), || t.fit()) + } else { + t.fit() + } } /// A model to train: the raster, the training polygons and their classes, read of the session, and diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index c18cf994ed..ca2b126959 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1006,9 +1006,13 @@ fn zonal_stats(s: &mut Session, p: &Value) -> CmdResult { run_tool(s, "zonalStats", p) } -/// Runs a (built-in) tool and applies its result, as `gp.run` does. +/// Runs a (built-in) tool and applies its result, as `gp.run` does. Where only a shell waits on +/// it ([`Session::long_runs`](crate::Session::long_runs)), Ctrl-C stops it, so it takes no work +/// cap ([`rustgis_analysis::control::Control::stoppable`]). pub fn run_tool(s: &mut Session, tool: &str, params: &Value) -> CmdResult { - run_tool_with(s, tool, params, &rustgis_analysis::control::Control::new()) + use rustgis_analysis::control::Control; + let control = if s.long_runs.is_some() { Control::stoppable() } else { Control::new() }; + run_tool_with(s, tool, params, &control) } /// [`run_tool`] under `control`: setting its flag from another thread stops the tool at its next diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 07f4e6fb49..7f9bb48f0e 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -208,7 +208,14 @@ pub(crate) fn made_or_ran(s: &mut Session, p: &Value, command: &str) -> CmdResul let (_, m) = s.raster_results.remove(at); return add_made(s, m); } - add_made(s, work(s, p)?()?) + let made = work(s, p)?; + // Where only a shell waits on it, Ctrl-C stops it: no work cap, as on the app's thread. + let made = if s.long_runs.is_some() { + rustgis_analysis::run_under(name, &rustgis_analysis::control::Control::stoppable(), made)? + } else { + made()? + }; + add_made(s, made) } #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index f26ecc2289..130ca3540a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -706,7 +706,7 @@ pub struct Session { /// Set where nothing waits on a command but a shell (`rustgis-cli run`): long runs a command /// works out where it's called (a trend over a state from a data cube) take the app's larger /// bounds, as its panes do on a thread, and tell their progress (a share, 0 to 1, and 1 when - /// one succeeds) through this. Python leaves it unset: a run there holds the interpreter's + /// one succeeds) through this; tools and rasters take no work cap, since Ctrl-C stops them. Python leaves it unset: a run there holds the interpreter's /// lock and can't be interrupted, so it keeps the Script pane's bounds. pub long_runs: Option, } diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index d959863a1d..9268b4bf4f 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -212,7 +212,8 @@ fn start(app: &mut RustGisApp, command: &'static str, name: &'static str, p: Val app.workflow.error = Some(crate::i18n::fmt("{tool} is still running: wait for it, or cancel it.", &[("tool", tl_raw!(job.name))])); return; } - let control = rustgis_engine::analysis::control::Control::new(); + // Cancel stops it, so it takes no work cap. + let control = rustgis_engine::analysis::control::Control::stoppable(); let ctl = control.clone(); let kind = if command == "gp.zonalStats" { gp::tool_start(&mut app.session, "zonalStats", &p).and_then(|run| { diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 7dc974e37a..b12fe84f7d 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -2709,7 +2709,8 @@ fn start_tool_run(app: &mut RustGisApp, tool: &'static str, params: &Value, ctx: match rustgis_engine::cmd::gp::tool_start(&mut app.session, tool, params) { Ok(run) => { let slot = crate::Slot::default(); - let control = rustgis_engine::analysis::control::Control::new(); + // Cancel stops it, so it takes no work cap. + let control = rustgis_engine::analysis::control::Control::stoppable(); let (out, ctl, ctx) = (slot.clone(), control.clone(), ctx.clone()); let id = run.id; let spawned = std::thread::Builder::new().name(tool.into()).spawn(move || { diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index c08810007d..e4f8764484 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -78,16 +78,26 @@ time; so does the map's right-click are added: a cancelled run keeps none. Scripts, models, the AI Assistant, the command line and the web app wait for each tool to finish. -**Viewshed**, **IDW Interpolation** and **Kriging** refuse a run that would take more than a second -or two of work on one thread (a ray step, a cell-point pair), and **Kernel Density** one whose -points' search windows (the square around each point's radius) cover more than 50 million cells. On the desktop they run on several -cores (up to 16), and they take as many times more work as the threads they run on. Threads share -cores and memory, so a run at that limit can take up to about twice as long as a one-thread run at -its own (measured on one machine, a Xeon Silver 4114; it can be longer on others); in the web app, -on one thread, the limits stay as they are. The error names the -limit on this computer and the threads it counted. A tool's own default cell size still follows the -one-thread limit, so it makes the same grid on any computer; a finer cell size given is what the -larger limit lets through. +**Viewshed**, **IDW Interpolation** and **Kriging** have no work limit when you run them from their +pane in the desktop app (the Tools or Analysis pane), nor does **Kernel Density** or **Deep +Learning**: a large run shows its progress, and **Cancel** stops it within a moment, however wide +its rows. Neither does `rustgis-cli run`, which Ctrl-C stops. Where a run can't be stopped while it +goes (a script, a model, the AI Assistant, Python and the web app), Viewshed, IDW Interpolation and +Kriging refuse a run that would take more than a second or two of work on one thread (a ray step, a +cell-point pair), Kernel Density one whose points' search windows (the square around each point's +radius) cover more than 50 million cells, and Deep Learning a model too large for its raster; its +training fits a share of the sampled pixels, or fewer epochs, instead (`dl.train` reports the epochs +it ran). A task or model recorded from a pane run keeps these limits when it runs again in the app, +so a step the pane took on can be refused there, and a model trained again can differ. On the +desktop these limits grow with the cores the run takes (up to 16), as many times more work as the +threads it runs on. Threads share cores and memory, so a run at that limit can take up to about +twice as long as a one-thread run at its own (measured on one machine, a Xeon Silver 4114; it can be +longer on others); in the web app, on one thread, the limits stay as they are. The error names the +limit on this computer and the threads it counted. The limits on memory (the cells a grid may have, +and those on the following pages) hold everywhere, as does Viewshed's on its window: a radius +covering more than 25 million cells, or 100,000 cells across, is refused. A tool's own default cell +size still follows the one-thread limit, so it makes the same grid on any computer and from any +caller; a finer cell size given is what a larger limit, or none, lets through. From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's units, takes one of them in any letter case (`"meters"` for `Meters`); any other value is an error @@ -1322,7 +1332,9 @@ the wrong field. **Analyze › Deep Learning Tools** trains a neural network (a multilayer perceptron) on the pixels under training polygons and classifies every pixel, adding a class raster with a confidence band -and reporting the accuracy on held-back pixels. Trained models stay in the project; **Publish › +and reporting the accuracy on held-back pixels. Run from its pane in the desktop app, it trains +on every sampled pixel for all the epochs asked, however long that takes, and can be cancelled; +elsewhere it keeps to a few seconds of work (see the limits above). Trained models stay in the project; **Publish › Share As › Deep Learning** saves one as a package to apply in other projects. **Analyze › Create Hosted Imagery** cuts a raster into XYZ web map tiles (PNG, transparent outside the raster) with a TileJSON file and a viewer page. From 88795f94c41229de6d617de7f58e43f5c55a528e Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Wed, 4 Nov 2026 10:59:16 +0100 Subject: [PATCH 2/2] fix(analysis): rustgis-cli run is uncapped only at a terminal; Kernel Density and training stop sooner on Cancel (#1418 review) --- apps/rustgis-cli/src/main.rs | 5 +++- crates/analysis/src/density.rs | 9 +++++-- crates/analysis/src/neural.rs | 2 ++ crates/analysis/src/parallel.rs | 2 +- crates/engine/src/cmd/deeplearning.rs | 8 +++--- crates/engine/src/cmd/gp.rs | 6 ++--- crates/engine/src/cmd/workflows.rs | 9 +++---- crates/engine/src/lib.rs | 8 +++++- docs/user-guide/analysis.md | 35 ++++++++++++++------------- 9 files changed, 49 insertions(+), 35 deletions(-) diff --git a/apps/rustgis-cli/src/main.rs b/apps/rustgis-cli/src/main.rs index c23a0982d9..99b454e78a 100644 --- a/apps/rustgis-cli/src/main.rs +++ b/apps/rustgis-cli/src/main.rs @@ -849,8 +849,11 @@ fn main() -> ExitCode { } "run" => { // Nothing waits on `run` but the shell (an assistant or an MCP client does, and has a - // timeout): long runs take the app's bounds, their progress on stderr. + // timeout): long runs take the app's bounds, their progress on stderr. A person at the + // terminal can stop a run with Ctrl-C, so there tools take no work cap; a program + // calling it may have a timeout, and keeps the caps. s.long_runs = Some(progress); + s.stoppable = std::io::stdin().is_terminal() && std::io::stderr().is_terminal(); open_inputs(&mut s, &args)?; for (i, a) in args.iter().enumerate() { if a == "--cmd" diff --git a/crates/analysis/src/density.rs b/crates/analysis/src/density.rs index e620b295d4..c788d22bd2 100644 --- a/crates/analysis/src/density.rs +++ b/crates/analysis/src/density.rs @@ -198,8 +198,8 @@ where crate::parallel::for_each_row( grid.chunks_mut(w * strip_rows), threads, - || (), - |_, s, strip: &mut [f64]| { + crate::control::Pace::default, + |pace, s, strip: &mut [f64]| { let first = s * strip_rows; let last = first + strip.len() / w - 1; let (mut counted, mut cells) = (0, 0usize); @@ -232,6 +232,11 @@ where } let rem_sq = h_sq - dy_sq; let row_offset = (row - first) * w; + // One point's window can span the grid without a cap: Cancel is looked at + // within it. + if pace.stop(col_max + 1 - col_min) { + return; + } for col in col_min..=col_max { let x_cell = origin_x + (col as f64 + 0.5) * cell_size; diff --git a/crates/analysis/src/neural.rs b/crates/analysis/src/neural.rs index a222ab11fd..1e3b26d265 100644 --- a/crates/analysis/src/neural.rs +++ b/crates/analysis/src/neural.rs @@ -375,6 +375,8 @@ pub fn train( } } } + // A cancelled run makes no model: the accuracy check on held-back samples can take long too. + crate::control::check()?; if !layers.iter().all(|l| l.w.iter().chain(&l.b).all(|v| v.is_finite())) { return Err("training diverged; try fewer epochs or smaller layers".into()); } diff --git a/crates/analysis/src/parallel.rs b/crates/analysis/src/parallel.rs index d01000e5db..539e5ff790 100644 --- a/crates/analysis/src/parallel.rs +++ b/crates/analysis/src/parallel.rs @@ -71,7 +71,7 @@ pub(crate) fn work_cap(one_thread: usize, threads: usize) -> usize { pub(crate) const UNCAPPED_HINT: &str = if cfg!(target_arch = "wasm32") { "" } else { - " (or run it from its pane in the desktop app, or with rustgis-cli run, which take any amount of work and can be cancelled)" + " (or run it from its pane in the desktop app, which takes any amount of work and can be cancelled)" }; /// The cores to use, at most [`MAX_THREADS`]. diff --git a/crates/engine/src/cmd/deeplearning.rs b/crates/engine/src/cmd/deeplearning.rs index 14c412971e..ee1e94cd7e 100644 --- a/crates/engine/src/cmd/deeplearning.rs +++ b/crates/engine/src/cmd/deeplearning.rs @@ -142,12 +142,12 @@ fn model_index(s: &Session, p: &Value) -> Result { } } -/// Trains a model from the pixels under training polygons. Where only a shell waits on it -/// ([`Session::long_runs`](crate::Session::long_runs)), Ctrl-C stops it, so it takes no work cap, -/// as from the pane. +/// Trains a model from the pixels under training polygons. Where a person at a terminal can stop +/// it with Ctrl-C ([`Session::stoppable`](crate::Session::stoppable)), it takes no work cap, as +/// from the pane. fn train(s: &Session, p: &Value) -> Result { let t = training(s, p)?; - if s.long_runs.is_some() { + if s.stoppable { rustgis_analysis::run_under("Train Deep Learning Model", &rustgis_analysis::control::Control::stoppable(), || t.fit()) } else { t.fit() diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index ca2b126959..abcb17b881 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1006,12 +1006,12 @@ fn zonal_stats(s: &mut Session, p: &Value) -> CmdResult { run_tool(s, "zonalStats", p) } -/// Runs a (built-in) tool and applies its result, as `gp.run` does. Where only a shell waits on -/// it ([`Session::long_runs`](crate::Session::long_runs)), Ctrl-C stops it, so it takes no work +/// Runs a (built-in) tool and applies its result, as `gp.run` does. Where a person at a terminal +/// can stop it with Ctrl-C ([`Session::stoppable`](crate::Session::stoppable)), it takes no work /// cap ([`rustgis_analysis::control::Control::stoppable`]). pub fn run_tool(s: &mut Session, tool: &str, params: &Value) -> CmdResult { use rustgis_analysis::control::Control; - let control = if s.long_runs.is_some() { Control::stoppable() } else { Control::new() }; + let control = if s.stoppable { Control::stoppable() } else { Control::new() }; run_tool_with(s, tool, params, &control) } diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 7f9bb48f0e..469f7110ae 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -209,12 +209,9 @@ pub(crate) fn made_or_ran(s: &mut Session, p: &Value, command: &str) -> CmdResul return add_made(s, m); } let made = work(s, p)?; - // Where only a shell waits on it, Ctrl-C stops it: no work cap, as on the app's thread. - let made = if s.long_runs.is_some() { - rustgis_analysis::run_under(name, &rustgis_analysis::control::Control::stoppable(), made)? - } else { - made()? - }; + // Where a person at a terminal can stop it with Ctrl-C: no work cap, as on the app's thread. + let made = + if s.stoppable { rustgis_analysis::run_under(name, &rustgis_analysis::control::Control::stoppable(), made)? } else { made()? }; add_made(s, made) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 130ca3540a..71fc8efd2a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -706,9 +706,14 @@ pub struct Session { /// Set where nothing waits on a command but a shell (`rustgis-cli run`): long runs a command /// works out where it's called (a trend over a state from a data cube) take the app's larger /// bounds, as its panes do on a thread, and tell their progress (a share, 0 to 1, and 1 when - /// one succeeds) through this; tools and rasters take no work cap, since Ctrl-C stops them. Python leaves it unset: a run there holds the interpreter's + /// one succeeds) through this. Python leaves it unset: a run there holds the interpreter's /// lock and can't be interrupted, so it keeps the Script pane's bounds. pub long_runs: Option, + /// Set where a person at a terminal waits on a command (`rustgis-cli run` with stdin and + /// stderr a terminal), who can stop it with Ctrl-C: tools and rasters take no work cap + /// ([`rustgis_analysis::control::Control::stoppable`]). A program that calls it (an agent, a + /// CI job) may wait with a timeout, so there they keep their caps. + pub stoppable: bool, } /// The most `layer.addUrl` requests kept for the front end at a time. @@ -944,6 +949,7 @@ impl Session { raster_results: Vec::new(), raster_runs: Vec::new(), long_runs: None, + stoppable: false, } } diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index e4f8764484..3e9634422c 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -81,23 +81,24 @@ Assistant, the command line and the web app wait for each tool to finish. **Viewshed**, **IDW Interpolation** and **Kriging** have no work limit when you run them from their pane in the desktop app (the Tools or Analysis pane), nor does **Kernel Density** or **Deep Learning**: a large run shows its progress, and **Cancel** stops it within a moment, however wide -its rows. Neither does `rustgis-cli run`, which Ctrl-C stops. Where a run can't be stopped while it -goes (a script, a model, the AI Assistant, Python and the web app), Viewshed, IDW Interpolation and -Kriging refuse a run that would take more than a second or two of work on one thread (a ray step, a -cell-point pair), Kernel Density one whose points' search windows (the square around each point's -radius) cover more than 50 million cells, and Deep Learning a model too large for its raster; its -training fits a share of the sampled pixels, or fewer epochs, instead (`dl.train` reports the epochs -it ran). A task or model recorded from a pane run keeps these limits when it runs again in the app, -so a step the pane took on can be refused there, and a model trained again can differ. On the -desktop these limits grow with the cores the run takes (up to 16), as many times more work as the -threads it runs on. Threads share cores and memory, so a run at that limit can take up to about -twice as long as a one-thread run at its own (measured on one machine, a Xeon Silver 4114; it can be -longer on others); in the web app, on one thread, the limits stay as they are. The error names the -limit on this computer and the threads it counted. The limits on memory (the cells a grid may have, -and those on the following pages) hold everywhere, as does Viewshed's on its window: a radius -covering more than 25 million cells, or 100,000 cells across, is refused. A tool's own default cell -size still follows the one-thread limit, so it makes the same grid on any computer and from any -caller; a finer cell size given is what a larger limit, or none, lets through. +its rows. Neither does `rustgis-cli run` typed at a terminal, which Ctrl-C stops. Where a run can't +be stopped while it goes (a script, a model, the AI Assistant, Python, the web app, and `rustgis-cli +run` called from another program, such as an agent or a CI job, which may wait with a timeout), +Viewshed, IDW Interpolation and Kriging refuse a run that would take more than a second or two of +work on one thread (a ray step, a cell-point pair), Kernel Density one whose points' search windows +(the square around each point's radius) cover more than 50 million cells, and Deep Learning a model +too large for its raster; its training fits a share of the sampled pixels, or fewer epochs, instead +(`dl.train` reports the epochs it ran). A task or model recorded from a pane run keeps these limits +when it runs again in the app, so a step the pane took on can be refused there, and a model trained +again can differ. On the desktop these limits grow with the cores the run takes (up to 16), as many +times more work as the threads it runs on. Threads share cores and memory, so a run at that limit +can take up to about twice as long as a one-thread run at its own (measured on one machine, a Xeon +Silver 4114; it can be longer on others); in the web app, on one thread, the limits stay as they +are. The error names the limit on this computer and the threads it counted. The limits on memory +(the cells a grid may have, and those on the following pages) hold everywhere, as does Viewshed's on +its window: a radius covering more than 25 million cells, or 100,000 cells across, is refused. A +tool's own default cell size still follows the one-thread limit, so it makes the same grid on any +computer and from any caller; a finer cell size given is what a larger limit, or none, lets through. From the command line or a script (`gp.run`), a built-in tool's parameter with a list of choices, such as Buffer's units, takes one of them in any letter case (`"meters"` for `Meters`); any other value is an error