Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example offloading template #1573

Draft
wants to merge 38 commits into
base: develop
Choose a base branch
from

Conversation

stognini
Copy link
Member

@stognini stognini commented Jan 13, 2025

This PR adds a new example that acts more as a template structure for users to learn how to either add Celeritas capabilities to their existing framework or build a new Geant4 application (with offloading capabilities) from scratch.

It is technically very similar to the existing simple-offload.cc, but it provides more directions to the user, as it is much more similar to the well known examples found in Geant4. The main reasons to add it, in my opinion, is that it provides:

  • A short, independent README with code directions and explanations;
  • A very simple and clear CMakeLists.txt that teaches the user how to link a Celeritas' install against their project; and
  • An equivalent and well known code structure organization, in terms of .hh/.cc files, as any Geant4 example, which should make direct comparisons to existing Geant4 codes more intuitive.

@stognini stognini requested review from sethrj and whokion January 13, 2025 16:09
@stognini stognini added the documentation Documentation, examples, tests, and CI label Jan 13, 2025
Copy link

github-actions bot commented Jan 13, 2025

Test summary

0 tests   0 ✅  0s ⏱️
0 suites  0 💤
0 files    0 ❌

Results for commit 8895050.

♻️ This comment has been updated with latest results.

Copy link
Member

@sethrj sethrj left a comment

Choose a reason for hiding this comment

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

Let's move the example up a directory, make sure it shows up in the manual, and add it to scripts/ci/test-examples.sh

example/accel/offload-template/src/Celeritas.cc Outdated Show resolved Hide resolved
example/accel/offload-template/CMakeLists.txt Outdated Show resolved Hide resolved
example/accel/offload-template/CMakeLists.txt Outdated Show resolved Hide resolved
example/accel/offload-template/README.md Outdated Show resolved Hide resolved
example/accel/offload-template/src/ActionInitialization.cc Outdated Show resolved Hide resolved
@whokion
Copy link
Contributor

whokion commented Jan 14, 2025

A couple of general comments:

  1. Use G4RunManagerFactory instead of G4RunManager (also use G4PhysListFactory if there is no custom physics list)
  2. Use G4VTrackingManager (i.e, add some like OffloadingTrackingManager to implement an example of those pure/virtual methods - see an example at ​CMSEmStandardPhysicsTrackingManager)

Copy link
Contributor

@whokion whokion left a comment

Choose a reason for hiding this comment

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

A good start, but may need some refinement to make the example more general.

example/offload-template/CMakeLists.txt Outdated Show resolved Hide resolved
example/offload-template/CMakeLists.txt Show resolved Hide resolved
example/offload-template/README.rst Show resolved Hide resolved
}

// Construct run manager
G4RunManager run_manager;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
G4RunManager run_manager;
auto runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);

Copy link
Member

Choose a reason for hiding this comment

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

Would this make it MT if Geant4 is built with threading?

Copy link
Member Author

@stognini stognini Jan 14, 2025

Choose a reason for hiding this comment

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

I actually made this a G4RunManagerType::Serial to run a single-thread execution, and just added a comment on the MT case.

Edit: My reasoning to keep it single-threaded is so that we don't have to also require an install with GEANT4_BUILD_MULTITHREADED=ON as a condition just to test a tiny offload example.

Copy link
Contributor

Choose a reason for hiding this comment

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

GEANT4_BUILD_MULTITHREADED=ON is now the default (so, you do not need explicitly to set it), which then is able to run both sequential and tasking (default tasking option is the traditional old MT). So it will support all cases.

Comment on lines 31 to 33
// Construct Celeritas offloading interface
CelerSimpleOffload().Build(
&CelerSetupOptions(), &CelerSharedParams(), &CelerLocalTransporter());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be in BuildForMaster (assuming that the default run manager (tasking run manager) is used)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really, because it is not an MT application. But good point, I may have to either make it an MT example, or add a few notes throughout it about the MT case.

Copy link
Member

Choose a reason for hiding this comment

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

It probably should support MT? Most new Geant4 apps should.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I thought of that. But then we have to require a Geant4 install with GEANT4_BUILD_MULTITHREADED=ON for a tiny example. Fairly, that would be standard for most cases, but seemed simpler to run a single-threaded one, so any compilation works, and explain how MT should be handled in a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

The BuildForMaster member function can be supplied always. If Geant4 is not MT enabled, it'll never be called, but by being there both cases can be supported.

example/offload-template/src/DetectorConstruction.cc Outdated Show resolved Hide resolved
example/offload-template/CMakeLists.txt Outdated Show resolved Hide resolved
example/offload-template/CMakeLists.txt Outdated Show resolved Hide resolved
@whokion
Copy link
Contributor

whokion commented Jan 14, 2025

2. Use G4VTrackingManager (i.e, add some like OffloadingTrackingManager to implement an example of those pure/virtual methods - see an example at ​[CMSEmStandardPhysicsTrackingManager](https://cmssdt.cern.ch/lxr/source/SimG4Core/PhysicsLists/src/CMSEmStandardPhysicsTrackingManager.cc))

Sorry that I did not know that TrackingManagerOffload already implemented and you already used the interface. Your approach is good.

example/offload-template/main.cc Outdated Show resolved Hide resolved
Comment on lines 31 to 33
// Construct Celeritas offloading interface
CelerSimpleOffload().Build(
&CelerSetupOptions(), &CelerSharedParams(), &CelerLocalTransporter());
Copy link
Contributor

Choose a reason for hiding this comment

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

The BuildForMaster member function can be supplied always. If Geant4 is not MT enabled, it'll never be called, but by being there both cases can be supported.

Copy link
Member

@sethrj sethrj left a comment

Choose a reason for hiding this comment

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

Looks good, but please take a look at the couple remaining suggestions.

example/offload-template/src/DetectorConstruction.cc Outdated Show resolved Hide resolved
example/offload-template/src/DetectorConstruction.cc Outdated Show resolved Hide resolved
scripts/ci/test-examples.sh Outdated Show resolved Hide resolved
@sethrj
Copy link
Member

sethrj commented Jan 17, 2025

Hey @drbenmorgan , I've been trying to understand some weird behavior in the multiple cmake package scripts we use and thought you might know... https://github.com/celeritas-project/celeritas/actions/runs/12812027794 shows @stognini 's example failing for Geant4 11.1 and 11.2 .

I finally realized we'd already "solved" this issue before: https://github.com/celeritas-project/celeritas/blob/eb9b0d721354503f12d3fbf18be0171225dcfdb8/cmake/CeleritasConfig.cmake.in#L70--L72 is due to #1152 , which notes "issues with the new geant4 cmake variable cache break[ing] xercesc importing for 11.1–11.2". For some reason though our previous fix fails when Celeritas isn't marked REQUIRED: see e4eed68. (Maybe it invalidates the find_dependency cache?)

Do you have any ideas how to better fix this? Thanks!

@drbenmorgan
Copy link
Contributor

Argh, this looks like the same issue as in apt-sim/AdePT#267, the hack fix there was to explicitly find Xerces before anything else. I'll have to go back to that and the reproducer to try and properly track this down upstream...

@sethrj
Copy link
Member

sethrj commented Jan 17, 2025

Yeah, we added that and it worked as long as celeritas and Geant were both required 😅

@drbenmorgan
Copy link
Contributor

O.k., no better ideas from my side at the moment - will try and take a look on the VecGeom etc side next week...

@sethrj
Copy link
Member

sethrj commented Jan 17, 2025

Aha, I see in the vecgeom config file:

if(NOT DEFINED XercesC_INCLUDE_DIR)
  set(XercesC_INCLUDE_DIR "/opt/spack/opt/spack/sequoia/xerces-c/3.2.5/3zdtn2m/include")
endif()

... which I introduced in VecGeom 52f767d29f, which maybe is conflicting with something in Geant4 and/or XercesC...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation, examples, tests, and CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants