Skip to content

feat(geant4): parallelize in-browser Geant4 simulations across multiple Web Workers (#2526) - #2584

Draft
DariuszRozmus wants to merge 2 commits into
masterfrom
2526-geant4-multiworker-simulation
Draft

DariuszRozmus wants to merge 2 commits into
masterfrom
2526-geant4-multiworker-simulation

Conversation

@DariuszRozmus

Copy link
Copy Markdown

Summary

Closes #2526

Parallelizes in-browser Geant4 WebAssembly simulations (demo / local mode) by splitting primary particles across multiple concurrent Web Workers instead of running on a single worker.

Changes

  • src/services/Geant4MacroHelpers.ts (new):
    • distributePrimaries: evenly divides total primaries across K workers, distributing remainders across the first workers and skipping workers assigned 0 primaries.
    • generateRandomSeeds / prepareWorkerMacro: injects unique pairs of 32-bit cryptographic seeds (/random/setSeeds <seed1> <seed2> via crypto.getRandomValues) immediately before each worker's rewritten /run/beamOn <workerPrimaries> command to guarantee statistically independent Monte Carlo streams.
    • mergeGeant4ResultFiles: deterministically merges Geant4 primitive scorer dump files cell-by-cell (total(value), total(val^2), entry) across all workers before parsing.
  • src/services/Geant4LocalWorkerSimulationService.ts:
    • Refactored workers from a single Geant4Worker per job to Record<JobId, Geant4Worker[]>.
    • Scoped numPrimaries and primariesPerWorker per jobId (removing shared instance state).
    • Added worker pool aggregation helpers (getJobCombinedState, areAllWorkersCompleted, getJobStartTime, getJobEndTime, multi-task getJobTasksStatus, and multi-worker cancelJob / markSafeForTermination).
    • Updated hydrateResults to fetch output files from all workers and merge them via mergeGeant4ResultFiles before passing them to Geant4ResultsFileParser.
    • Added determineWorkerCount defaulting to min(navigator.hardwareConcurrency - 1, MAX_LOCAL_GEANT4_WORKERS) with MAX_LOCAL_GEANT4_WORKERS = 3 as a safe browser memory cap (since the upstream geant4_wasm.wasm build currently allocates a fixed -s TOTAL_MEMORY=512MB per worker without ALLOW_MEMORY_GROWTH, spawning too many workers causes RangeError: Array buffer allocation failed).
  • src/WrapperApp/components/Simulation/RunSimulationForm.tsx:
    • Unlocked Number of tasks for Geant4 simulations, defaulting to min(hardwareConcurrency - 1, MAX_LOCAL_GEANT4_WORKERS) with helper text indicating the safe worker limit and detected CPU cores.
  • src/services/__tests__/Geant4MacroHelpers.test.ts (new):
    • Added 11 unit tests covering primary distribution, RNG seed generation, macro rewriting, and multi-worker scorer file merging.

Verification

  • npx tsc --noEmit — passed with 0 errors
  • npm test -- src/services/__tests__/Geant4 --watchAll=false — 15/15 tests passing
  • Prettier & ESLint — 0 errors

Companion Documentation PRs

@DariuszRozmus
DariuszRozmus marked this pull request as draft December 2, 2026 19:59

This branch has not been deployed

No deployments
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.

Parallelize in-browser Geant4 simulation using multiple Web Workers in demo mode

1 participant