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
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d7d8c23
Draft Celeritas offload template example
stognini Jan 10, 2025
3522121
Use simple offload interface
stognini Jan 13, 2025
c38da6b
Minor improvements
stognini Jan 13, 2025
93344a7
Add missing copyright note
stognini Jan 13, 2025
7cda4be
Fixup
stognini Jan 13, 2025
cf3a6c8
Add offload-template to doc
stognini Jan 13, 2025
7ab16f6
Print error message in CMakelists if Celeritas not found; Use Trackin…
stognini Jan 13, 2025
1e0c3bd
Move to example/
stognini Jan 14, 2025
71fca31
Update README and doxygen comments
stognini Jan 14, 2025
136923c
Add offload-template to test-examples.sh
stognini Jan 14, 2025
2b0c86a
Update readme
stognini Jan 14, 2025
691db71
Fixup
stognini Jan 14, 2025
e60bfeb
Remove extra variables in CMakeLists; Add comments for MT execution
stognini Jan 14, 2025
4a24b74
Merge branch 'develop' into example-template
stognini Jan 14, 2025
d52259d
Run offload-template only with Geant4 v11
stognini Jan 15, 2025
cd760cd
Tweak documentation
sethrj Jan 15, 2025
5995dc0
Change to multithread
stognini Jan 15, 2025
bfa3330
Fixup
stognini Jan 15, 2025
149a02f
Improve documentation
stognini Jan 15, 2025
66211f0
Fix 11.1 to 11.3 build issues, maybe
stognini Jan 15, 2025
cc74293
Address comments
stognini Jan 15, 2025
e51c9c6
Just use auto*, since G4 functions are not marked as const
stognini Jan 15, 2025
6f6e132
Change cmake policy placement
sethrj Jan 15, 2025
4ce08ad
REVERTME: minimize CI
sethrj Jan 15, 2025
2f75fcc
Update policy and include FindCeleritas
sethrj Jan 16, 2025
0af9cfa
Try switching order for now
sethrj Jan 16, 2025
6e5adf7
Fix/add ans colors to ci script
sethrj Jan 16, 2025
e85e2a7
Make 'required' match between geant4 and celeritas
sethrj Jan 16, 2025
e2a6722
Check that celeritas uses geant4
sethrj Jan 16, 2025
e4eed68
OK fucking make them required then
sethrj Jan 16, 2025
beea6de
Revert "OK fucking make them required then"
sethrj Jan 17, 2025
8d993c2
Separtely set GDML variable to look for exact XercesC
sethrj Jan 17, 2025
dfbe718
Merge remote-tracking branch 'upstream/develop' into example-template
sethrj Jan 17, 2025
186a1b0
Wrap Celeritas config script in policy
sethrj Jan 17, 2025
eaf6c30
Export cache variables as cache
sethrj Jan 17, 2025
c15f9c6
Save cache variables as cache
sethrj Jan 17, 2025
3024e9e
Reapply policy
sethrj Jan 17, 2025
8895050
Add minor comment
stognini Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions doc/example/geant4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@

.. _example_geant:

Geant4 integration example
==========================
Geant4 integration examples
===========================

Offload template
----------------

.. include:: ../../example/offload-template/README.rst

Small executables
-----------------

These small examples demonstrate how to offload tracks to Celeritas in a serial
or multithreaded environment using:
Expand Down
2 changes: 2 additions & 0 deletions example/offload-template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/install
53 changes: 53 additions & 0 deletions example/offload-template/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#------------------------------- -*- cmake -*- -------------------------------#
# Copyright Celeritas contributors: see top-level COPYRIGHT file for details
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
#-----------------------------------------------------------------------------#
# Minimal Geant4 application with Celeritas offloading capabilities
#-----------------------------------------------------------------------------#
cmake_minimum_required(VERSION 3.10)
cmake_policy(VERSION 3.12...3.30)
project(offload-template)

#-----------------------------------------------------------------------------#
# Find packages
find_package(Celeritas 0.5)
sethrj marked this conversation as resolved.
Show resolved Hide resolved
find_package(Geant4 REQUIRED)

if(NOT Celeritas_FOUND)
message(FATAL_ERROR
"No Celeritas installation found.\n"
"Export Celeritas_ROOT=/path/to/celeritas/install or use CMAKE_INSTALL_PREFIX")
endif()

#-----------------------------------------------------------------------------#
# Set up sources, include directories, and libraries
set(_sources
src/ActionInitialization.cc
src/Celeritas.cc
src/DetectorConstruction.cc
src/EventAction.cc
src/PrimaryGeneratorAction.cc
src/RunAction.cc
src/SensitiveDetector.cc
)

set(_includes
${CMAKE_CURRENT_SOURCE_DIR}/src
${Geant4_INCLUDE_DIR}
${Celeritas_ROOT}/include
sethrj marked this conversation as resolved.
Show resolved Hide resolved
)

set(_libs
${Geant4_LIBRARIES}
Celeritas::celeritas
Celeritas::accel
)

#-----------------------------------------------------------------------------#
# Add executable
add_executable(run-offload main.cc ${_sources})
target_include_directories(run-offload PRIVATE $<BUILD_INTERFACE:${_includes}>)
sethrj marked this conversation as resolved.
Show resolved Hide resolved

# Use Celeritas CUDA RDC utility function to correctly link libraries
include(CudaRdcUtils)
cuda_rdc_target_link_libraries(run-offload ${_libs})
sethrj marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions example/offload-template/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
====================================
Geant4-Celeritas offloading template
====================================

Template for Geant4 applications with Celeritas physics offloading capabilities.
It shows how to link Celeritas against Geant4 in the :code:`CMakeLists.txt` and
the Geant4 classes needed to initialize Celeritas, offload events, and recover
step information.

Dependencies
------------

- Geant4 v11 or newer
- Celeritas v0.5 or newer

- :code:`CELERITAS_USE_Geant4=ON`

Build and run
-------------

.. code-block:: sh

$ mkdir build
$ cd build
$ cmake ..
sethrj marked this conversation as resolved.
Show resolved Hide resolved
$ make
$ export CELER_DISABLE_PARALLEL=1
$ ./run-offload


Boilerplate offloading code
---------------------------

- :code:`Celeritas.[hh/cc]`: Defines the needed components for a Celeritas
offload execution:

- **Setup options** (memory allocation, physics, field, scoring, and so on)
- **Shared parameters** used in the run (materials, physics processes
cross-section tables, and so on)
- **Transporter** (execute/manage the particle transport)
- **Simple Offload** simplified user-interface. Each `SimpleOffload` call is
briefly described below.

- :code:`G4VUserActionInitialization`

- :code:`Build`: Construct Celeritas Simple Offload interface with
user-defined options (from `Celeritas.cc`) and assign the Celeritas tracking
manager to the appropriate particles

- :code:`G4UserRunAction`

- :code:`BeginOfRunAction`: Initialize Celeritas global shared data on master
and worker threads
- :code:`EndOfRunAction`: Clear data and return Celeritas to an invalid state

- :code:`G4UserEventAction`

- :code:`BeginOfEventAction`: Initialize event in Celeritas
- :code:`EndOfEventAction`: Flush remaining particles

- :code:`G4VSensitiveDetector`

- :code:`ProcessHits`: Currently the *only* Celeritas callback interface to
Geant4; at each step, Celeritas sends data back as a `G4Step` to be
processed by Geant4
43 changes: 43 additions & 0 deletions example/offload-template/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file example/offload-template/main.cc
//! \brief Minimal Geant4 application with Celeritas offloading.
//---------------------------------------------------------------------------//
#include <FTFP_BERT.hh>
#include <G4RunManager.hh>

#include "src/ActionInitialization.hh"
#include "src/DetectorConstruction.hh"

//---------------------------------------------------------------------------//
/*!
* Geant4-Celeritas offloading template.
*
* See README for details.
*/
int main(int argc, char* argv[])
{
if (argc != 1)
{
// Print help message
std::cout << "Usage: " << argv[0] << std::endl;
return EXIT_FAILURE;
}

// 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.

run_manager.SetVerboseLevel(1); // Print minimal information about the run

// Initialize physics, geometry, and actions
run_manager.SetUserInitialization(new FTFP_BERT(/* verbosity = */ 0));
run_manager.SetUserInitialization(new DetectorConstruction());
run_manager.SetUserInitialization(new ActionInitialization());

// Run one event
run_manager.Initialize();
run_manager.BeamOn(1);

return EXIT_SUCCESS;
}
46 changes: 46 additions & 0 deletions example/offload-template/src/ActionInitialization.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file example/offload-template/src/ActionInitialization.cc
//---------------------------------------------------------------------------//
#include "ActionInitialization.hh"

#include <accel/TrackingManagerOffload.hh>

#include "Celeritas.hh"
#include "EventAction.hh"
#include "G4Electron.hh"
#include "G4Gamma.hh"
#include "G4Positron.hh"
#include "PrimaryGeneratorAction.hh"
#include "RunAction.hh"

//---------------------------------------------------------------------------//
/*!
* Construct empty.
*/
ActionInitialization::ActionInitialization() : G4VUserActionInitialization() {}

//---------------------------------------------------------------------------//
/*!
* Set up all user actions and Celeritas offload interface.
*/
void ActionInitialization::Build() const
{
// 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.


// Add Celeritas tracking manager to electrons, positrons, and gammas
auto* celer_tracking = new celeritas::TrackingManagerOffload(
&CelerSharedParams(), &CelerLocalTransporter());
G4Electron::Definition()->SetTrackingManager(celer_tracking);
G4Positron::Definition()->SetTrackingManager(celer_tracking);
G4Gamma::Definition()->SetTrackingManager(celer_tracking);
drbenmorgan marked this conversation as resolved.
Show resolved Hide resolved

// Initialize Geant4 user actions
this->SetUserAction(new RunAction());
this->SetUserAction(new EventAction());
this->SetUserAction(new PrimaryGeneratorAction());
}
23 changes: 23 additions & 0 deletions example/offload-template/src/ActionInitialization.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file example/offload-template/src/ActionInitialization.hh
//---------------------------------------------------------------------------//
#pragma once

#include <G4VUserActionInitialization.hh>

//---------------------------------------------------------------------------//
/*!
* Initialize all user action classes.
*/
class ActionInitialization final : public G4VUserActionInitialization
{
public:
// Construct empty
ActionInitialization();

// Set up user actions and construct Celeritas offload interface
void Build() const final;
};
87 changes: 87 additions & 0 deletions example/offload-template/src/Celeritas.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file example/offload-template/src/Celeritas.cc
//---------------------------------------------------------------------------//
#include "Celeritas.hh"

#include <G4Threading.hh>
#include <accel/AlongStepFactory.hh>
#include <celeritas/alongstep/AlongStepGeneralLinearAction.hh>
#include <celeritas/alongstep/AlongStepUniformMscAction.hh>
#include <celeritas/field/UniformFieldData.hh>
#include <celeritas/io/ImportData.hh>

using namespace celeritas;

//---------------------------------------------------------------------------//
/*!
* Globally shared setup options.
* Setup options are constructed the first time this method is invoked.
*/
SetupOptions& CelerSetupOptions()
{
static SetupOptions options = [] {
// See celeritas/install/include/accel/SetupOptions.hh
SetupOptions so;

// Set along-step factory
so.make_along_step = UniformAlongStepFactory();

so.max_num_tracks = 1024 * 16;
so.initializer_capacity = 1024 * 128 * 4;
so.secondary_stack_factor = 3.0;
so.ignore_processes = {"CoulombScat"}; // Ignored processes list

// Only call back for nonzero energy depositions: this is currently a
// global option for all detectors, so if any SDs extract data from
// tracks with no local energy deposition over the step, it must be set
// to false.
so.sd.ignore_zero_deposition = true;

// Using the pre-step point, reconstruct the G4 touchable handle.
so.sd.locate_touchable = true;
// Reconstruct the track, needed for particle type
so.sd.track = true;

// Save diagnostic information
so.output_file = "celeritas-offload-diagnostic.json";

// Pre/post-step data used in G4VSensitiveDetector::ProcessHits
so.sd.pre.kinetic_energy = true;
so.sd.post.kinetic_energy = true;
return so;
}();
return options;
}

//---------------------------------------------------------------------------//
/*!
* Celeritas problem data.
*/
SharedParams& CelerSharedParams()
{
static SharedParams sp;
return sp;
}

//---------------------------------------------------------------------------//
/*!
* Thread-local (when supported) transporter.
*/
LocalTransporter& CelerLocalTransporter()
{
static G4ThreadLocal LocalTransporter lt;
return lt;
}

//---------------------------------------------------------------------------//
/*!
* Thread-local offload interface.
*/
SimpleOffload& CelerSimpleOffload()
{
static G4ThreadLocal SimpleOffload so;
return so;
}
28 changes: 28 additions & 0 deletions example/offload-template/src/Celeritas.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file example/offload-template/src/Celeritas.hh
//---------------------------------------------------------------------------//
#pragma once

#include <accel/LocalTransporter.hh>
#include <accel/SetupOptions.hh>
#include <accel/SharedParams.hh>
#include <accel/SimpleOffload.hh>

namespace celeritas
{
class LocalTransporter;
struct SetupOptions;
class SharedParams;
} // namespace celeritas

// Global shared setup options
celeritas::SetupOptions& CelerSetupOptions();
// Shared data and GPU setup
celeritas::SharedParams& CelerSharedParams();
// Thread-local transporter
celeritas::LocalTransporter& CelerLocalTransporter();
// Thread-local offload user interface
celeritas::SimpleOffload& CelerSimpleOffload();
Loading
Loading