Skip to content

Performance: major runtime optimizations for parallel simulation, spatial joins, raster covariates, and result aggregation PR Description#100

Open
hambrecht wants to merge 6 commits into
DistanceDevelopment:masterfrom
hambrecht:perf/parallel-major-improvements
Open

Performance: major runtime optimizations for parallel simulation, spatial joins, raster covariates, and result aggregation PR Description#100
hambrecht wants to merge 6 commits into
DistanceDevelopment:masterfrom
hambrecht:perf/parallel-major-improvements

Conversation

@hambrecht
Copy link
Copy Markdown

Summary
This PR introduces four major performance changes to the simulation pipeline, each isolated in its own commit.
The goal is to reduce wall-clock time, improve CPU utilization, and lower memory/serialization overhead for large runs.

What Changed

  1. Parallel execution redesign
  • Switched to a worker-state model so workers initialize shared state once.
  • Replaced static task distribution with load-balanced scheduling.
  • Reduced repeated argument serialization overhead in parallel loops.
  • Key file: run.simulation.R
  1. Spatial distance computation redesign
  • Replaced per-transect intersection loops with indexed spatial joins.
  • Switched to vectorized distance computation paths.
  • Removed iterative reduce/rbind assembly bottlenecks.
  • Key files:
    • calc.perp.dists.R
    • calc.rad.dists.R
  1. Raster covariate caching
  • Added process-local caching for cov.surface rasters and value vectors.
  • Avoids reloading/re-extracting full raster values every repetition.
  • Improves repeated scale-parameter calculation performance when raster covariates are enabled.
  • Key file: calculate.scale.param.R
  1. Result transport and aggregation redesign
  • Changed per-repetition return shape to compact rep-level payloads.
  • Added rep-level extract/apply helpers for lower transfer and copy costs.
  • Updated serial and parallel aggregation paths to consume compact payloads.
  • Maintains backward-compatible accumulation path for legacy result shape.
  • Key files:
    • single.sim.loop.R
    • accumulate.PP.results.R
    • run.simulation.R

Expected Runtime Impact

  1. Better core utilization in heterogeneous replicate workloads due to load balancing.
  2. Lower parallel overhead from reduced repeated serialization of large objects.
  3. Faster geometry-heavy runs due to indexed spatial joins and vectorized distance calculations.
  4. Faster raster-enabled runs due to worker-local cache reuse.
  5. Reduced memory pressure and lower inter-process transfer volume during result aggregation.

Validation

  1. Targeted raster feature regression test passed fully: test-check_RasterCovariate.R
  2. Result: PASS 11, FAIL 0, WARN 0, SKIP 0

Commit Structure

  1. perf(parallel): switch to worker-state load-balanced execution
  2. perf(spatial): vectorize detection-distance joins with sf indexes
  3. perf(raster): add worker-local cov.surface cache
  4. perf(results): stream per-repetition payloads instead of full result objects

hambrecht added 6 commits May 5, 2026 15:37
- add cov.surface support to Detectability and make.detectability
- apply raster covariates in calculate.scale.param
- validate cov.surface in simulation checks
- add raster covariate test coverage
- update related simulation/covariate paths used by tests
Update Detectability plot validation so covariates supplied via cov.surface are treated as valid even when absent from population covariates. Add raster-specific plotting path that reads surface values and uses raster quantiles to generate representative detection curves, enabling mixed covariate plotting (for example size + canopy) without false missing-covariate errors.
- initialize worker state once per cluster
- dispatch only repetition indices to workers
- replace static scheduling with parLapplyLB for better core utilization
- remove repeated heavy argument passing in parallel loop
- replace per-transect st_intersection loops with one st_join
- compute line/point distances in vectorized st_distance calls
- remove iterative reduce/rbind assembly path for possible detections
- cache raster handles/value vectors in a process-local runtime cache
- avoid re-reading raster files and full value vectors every repetition
- keep cache lookup keyed by file path (or raster footprint for in-memory surfaces)
…objects

- add extract/apply helpers for compact per-repetition result slices
- return rep-level payloads from single.sim.loop
- aggregate serial/parallel outputs without full-object round-trips
- keep backward-compatible accumulator path for legacy result shapes
- fix in-memory raster cache key generation for SpatRaster extents
Copilot AI review requested due to automatic review settings May 6, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant