Skip to content

feat(api): add public Simphony runtime interface - #457

Merged
plexoos merged 5 commits into
mainfrom
rm-g4cx
Oct 17, 2026
Merged

plexoos merged 5 commits into
mainfrom
rm-g4cx

Conversation

@plexoos

@plexoos plexoos commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Introduce an installed, function-based runtime over U4 geometry conversion and CSGOptiX execution. Applications initialize a live Geant4 world through simphony::simphony, query GPU availability, simulate/reset events, and explicitly save geometry.

  • Install the documented simphony.h and SensorIdentifier.h public headers. Applications provide custom sensor mappings through simphony::SensorIdentifier without including U4 implementation headers.
  • Support Required, Optional, and Disabled GPU policies, idempotent initialization with matching inputs, and explicit pending-event/reset checks.
  • Update U4Tree, the default sensor identifier, and the retained G4CX/DD4hep sensor adapters to consume the public protocol.
  • Migrate the Simphony-owned src/ applications to the runtime. The external-example and DD4hep run/event migrations are in the stacked G4CX retirement PR.
  • Cover custom sensor dispatch, changed-identifier rejection, lifecycle errors, geometry-only export, and executable ELF dependencies. Honor CMAKE_INSTALL_PREFIX for installed configuration data.

Validation: independently built the API commit with the retained G4CX library, simg4ox, consgeo, and API/legacy geometry tests. All 22 API, retained-G4CX geometry, and simg4ox integration checks passed. The complete stack passed 255 enabled CTest tests and DD4hep in-tree/standalone runtime checks.

Migration: custom identifiers now implement simphony::SensorIdentifier from SensorIdentifier.h; the old U4SensorIdentifier type/header is removed. The retained G4CX setter accepts the new protocol.

Follow-up: #459 removes G4CX and migrates the remaining external examples and DD4hep run/event actions. It is stacked on this PR and targets rm-g4cx.

@plexoos
plexoos force-pushed the rm-g4cx branch 2 times, most recently from d2ef6d4 to f4a2ebc Compare October 16, 2026 19:35
@plexoos
plexoos marked this pull request as ready for review October 16, 2026 23:10
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T23:13:19.050108Z f4a2ebc Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Missing event resets break repeated runs, while failed initialization retries and negative event IDs can terminate the process.

4 open issues
What changed in this PR

Introduces an installed, function-based Simphony API over geometry conversion and GPU execution, replacing direct G4CX usage in owned frontends.

Changes:

  • Adds backend policies, lifecycle validation, and explicit geometry serialization.
  • Migrates frontends to the public API.
  • Adds API behavior testing and library installation rules.
File Description
tests/​SimphonyApiTest.cc Tests initialization and geometry serialization.
tests/​CMakeLists.txt Registers the API test.
src/​GPURaytrace.h Migrates initialization and simulation.
src/​GPUPhotonSourceMinimal.h Migrates simulation and reset calls.
src/​GPUPhotonFileSource.h Migrates simulation and reset calls.
src/​GPUCerenkov.h Migrates initialization and simulation.
src/​g4app.h Uses the facade for event execution.
src/​consgeo.cc Converts and saves geometry without a GPU backend.
src/​CMakeLists.txt Builds, exports, and installs the facade.
src/​api/​simphony.h Declares the public runtime interface.
src/​api/​simphony.cc Implements backend selection and lifecycle management.

Review effort: Balanced

Comment thread src/GPUCerenkov.h
Comment thread src/GPURaytrace.h
Comment thread src/api/simphony.cc Outdated
Comment thread src/api/simphony.cc
Introduce an installed function-based facade over U4 geometry conversion
and CSGOptiX execution so applications can use Simphony without depending
on the G4CX coordinator.

- Export simphony::simphony and the documented simphony.h interface for
  initialization, backend queries, simulation/reset, and geometry saving.
- Express GPU requirements through Required, Optional, and Disabled
  policies. Validate process-wide initialization, idempotent reuse, and
  pending-event/reset sequencing with descriptive exceptions.
- Define the documented simphony::SensorIdentifier protocol in src/api,
  include it from simphony.h, and install it with the public headers.
  Update U4Tree, its default identifier, and the legacy G4CX/DD4hep sensor
  adapters to use the same interface without adding a U4-to-runtime link.
- Migrate the Simphony-owned src frontends to the runtime while retaining
  G4CX and the existing external example/DD4hep execution paths for the
  follow-up retirement change.
- Cover custom sensor dispatch, changed-identifier rejection, geometry-only
  initialization/export, lifecycle errors, and migrated executable ELF
  dependencies in the API and integration regressions.
- Honor CMAKE_INSTALL_PREFIX when locating installed configuration data.

BREAKING CHANGE: replace U4SensorIdentifier with simphony::SensorIdentifier
from SensorIdentifier.h. Applications implementing a custom sensor mapping
must update their base class and include; the legacy G4CX setter accepts
the new interface while the coordinator remains available in this commit.
GPUCerenkov and GPURaytrace retain event 0 while reading its hits but
never release it. A second /run/beamOn therefore fails the public runtime's
pending-event check, including when opening the first run's output fails.

Reset event 0 after consuming the hits and before returning from an
output-open failure. Keep simulation reset disabled until hit retrieval
has finished so cleanup does not discard results prematurely.

Add executable regressions for two consecutive runs in both frontends,
with normal output and with a directory blocking the hit-file path.
All four cases reproduced the pending-event exception before this fix
and now pass with nonzero GPU hits on both runs.

Refs: #457
Geometry translation and backend creation populate process-global SSim,
U4Tree, and CSG state before the public runtime is marked initialized.
If an exception escapes that sequence, retrying initialization can append
to the same global geometry and abort on a lower-level invariant.

Record a failed lifecycle state when runtime construction throws, preserve
the original exception, and reject subsequent initialization attempts
with a descriptive logic_error requiring a process restart. Keep null-world
validation and GPU-availability checks before this failure boundary so
preflight failures do not prevent a later valid initialization.

Document the failure contract and add a GPU-independent regression that
throws from the sensor callback after real geometry translation, verifies
that initialized/GPU state remains unpublished, and retries with matching
and changed options. The test reproduced an assertion before the fix;
both API initialization tests now pass.

Refs: #457
SEvt asserts that event indices are non-negative. Forwarding simulate(-1)
through an initialized GPU runtime can therefore abort the process instead
of reporting invalid API input.

Validate event IDs at the start of simulate and reset, before lifecycle
checks or backend dispatch, and throw std::invalid_argument for negative
values. Document the non-negative contract and cover rejection both before
initialization and after geometry-only initialization, without requiring
a GPU for invalid-input testing.

The API regression failed with the old lifecycle error before this fix;
both API tests now pass.

Refs: #457
@plexoos
plexoos merged commit 1cefa9e into main Oct 17, 2026
24 checks passed
@plexoos
plexoos deleted the rm-g4cx branch October 17, 2026 14:54
plexoos added a commit that referenced this pull request Oct 18, 2026
Follow-up to #457, now merged into main. The public runtime, sensor
interface, and API regression tests were introduced there; this PR
removes G4CX and migrates its remaining consumers.

Remove the legacy G4CX coordinator and move its remaining consumers to
`simphony::simphony`. This completes the migration of CSG test setup,
DD4hep run/event actions, and the MC-truth and async examples to the
same initialization and event lifecycle used by `simg4ox`.

- Delete G4CX sources, installed target/headers, legacy
executables/tests, top-level build entries, and logger hooks.
- Preserve GDML name handling and auxiliary sensitive-detector tags in
CSG tests while initializing the runtime with the GPU disabled.
- Reuse the DD4hep runtime across runs, preserve hit injection and
end-of-run batch flushing, and export geometry through `SaveGeometry`
plus `GeometryDirectory`.
- Migrate standalone examples and explicitly reset retained events after
result retrieval, including synchronous and output-file failure paths.
- Keep in-tree and standalone DD4hep builds working with C++20 by
removing inherited raw standard flags from DD4hep's Geant4 helper
target.
- Update external-build, saved-geometry, and application-capability
documentation and replace retired geometry-test export paths with
`consgeo` exports.

Validation:

- Full DD4hep-enabled build and all 255 enabled CTest tests passed; two
existing tests remain disabled.
- Clean Release build/install with testing disabled contains no G4CX
library, headers, exported target, or runtime dependency. Installed
`consgeo` export passed.
- All four standalone examples built against the clean package. MC-truth
and both async examples completed repeated runs, including synchronous
and batched modes.
- DD4hep v01-38 / ROOT 6.36.14 / Geant4 11.4.2: in-tree and standalone
plugin builds, standalone installation, and CPU/GPU raindrop tests
passed. With the same GPU kernel, in-tree and standalone hit counts
matched. Consecutive runs, geometry export, and end-of-run batch
flushing passed.
- The local Geant4 build uses initial-exec TLS, so these DD4hep checks
used its documented TLS-check override and Geant4 preloading.

Breaking change: consumers of `simphony::G4CX` / `G4CXOpticks` must link
`simphony::simphony`, include `simphony.h`, initialize from a live
Geant4 world, and reset retained events after reading results.
Saved-foundry rendering and simtrace remain available through
lower-level CSGOptiX tools. The three migrated standalone examples
explicitly move from C++17 to C++20.
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.

2 participants