Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ list(APPEND SOURCES
track/TrackInitParams.cc
user/ActionTimes.cc
user/DetectorSteps.cc
user/OnloadSteps.cc
user/ParticleTallyData.cc
user/RootStepWriterIO.json.cc
user/SimpleCalo.cc
Expand Down Expand Up @@ -270,6 +271,7 @@ if(CELERITAS_USE_Geant4)
ext/detail/GeantSurfacePhysicsLoader.cc
ext/detail/HitProcessor.cc
ext/detail/LevelTouchableUpdater.cc
ext/detail/OnloadProcessor.cc
ext/detail/NaviTouchableUpdater.cc
ext/detail/NullGeantProcess.cc
ext/detail/SensDetInserter.cc
Expand Down Expand Up @@ -459,6 +461,7 @@ celeritas_polysource(track/InitializeTracksAction)
celeritas_polysource(track/StatusChecker)
celeritas_polysource(user/ActionDiagnostic)
celeritas_polysource(user/DetectorSteps)
celeritas_polysource(user/OnloadSteps)
celeritas_polysource(user/SlotDiagnostic)
celeritas_polysource(user/StepDiagnostic)
celeritas_polysource(user/detail/SimpleCaloImpl)
Expand Down
191 changes: 191 additions & 0 deletions src/celeritas/ext/detail/OnloadProcessor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/ext/detail/OnloadProcessor.cc
//---------------------------------------------------------------------------//
#include "OnloadProcessor.hh"

#include <cstddef>
#include <iostream>
#include <utility>
#include <G4Step.hh>
#include <G4StepPoint.hh>
#include <G4Track.hh>

#include "corecel/Assert.hh"
#include "corecel/Types.hh"
#include "corecel/cont/Range.hh"
#include "corecel/io/Logger.hh"
#include "corecel/sys/ScopedProfiling.hh"
#include "corecel/sys/TraceCounter.hh"
#include "celeritas/Types.hh"
#include "celeritas/ext/GeantTrackReconstruction.hh"
#include "celeritas/user/OnloadData.hh"
#include "celeritas/user/OnloadSteps.hh"

#include "StepTransfer.t.hh"
#include "../GeantStepPointView.hh"
#include "../GeantStepView.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct local navigator and step data.
*
* If a device is active, the transfer also creates the event for copying
* device steps.
*/
OnloadProcessor::OnloadProcessor(VecParticle const& particles)
{
step_ = GeantTrackReconstruction::make_g4step();
CELER_ASSERT(step_);
if (!particles.empty())
{
// Reconstruct track
track_reconstruction_
= std::make_shared<GeantTrackReconstruction>(particles, step_);
}
}

//---------------------------------------------------------------------------//
/*!
* Allocate pinned host output for the largest possible device step.
*
* This \b must be called during setup, once the device step state exists and
* before any device steps are processed, so that no pinned memory is
* allocated while stepping: growing the output later would call
* \c cudaHostAlloc and \c cudaFreeHost , which serialize CUDA API calls from
* all host threads.
*/
void OnloadProcessor::reserve(StepStateDeviceRef const& states)
{
transfer_.reserve(states);
}

//---------------------------------------------------------------------------//
/*!
* Process onloaded steps (CPU).
*/
void OnloadProcessor::operator()(StepStateHostRef const& states)
{
this->process_local_steps(transfer_.copy(states));
}

//---------------------------------------------------------------------------//
/*!
* Enqueue compaction of device onloaded steps without synchronizing.
*
* The selection and compaction into the state's scratch space are enqueued on
* the state's stream by the transfer, along with a copy of the selected count
* to pinned host memory.
*/
void OnloadProcessor::operator()(StepStateDeviceRef const& states)
{
transfer_.compact_async(states);
}

//---------------------------------------------------------------------------//
/*!
* Enqueue the copy of compacted device steps after their step completes.
*
* This waits for the compaction enqueued by the call operator, which has
* already completed if the caller established device step completion. Any
* previously copied steps are processed first since they share host storage.
* The copy is enqueued on the memcpy stream without synchronizing. This does
* nothing if no compacted steps are pending.
*/
void OnloadProcessor::copy_pending_steps()
{
if (!transfer_.has_compacted())
{
return;
}

// The copy overwrites the output, so finish processing it first
this->process_copied_steps();
transfer_.copy_pending_async();
}

//---------------------------------------------------------------------------//
/*!
* Wait for and process copied device steps.
*/
void OnloadProcessor::process_copied_steps()
{
if (transfer_.take())
{
this->process_local_steps(transfer_.output());
}
}

//---------------------------------------------------------------------------//
/*!
* Process copied device steps only if the copy has completed.
*/
void OnloadProcessor::process_copied_steps_if_ready()
{
if (transfer_.try_take())
{
this->process_local_steps(transfer_.output());
}
}

//---------------------------------------------------------------------------//
/*!
* Generate and call onloaded steps from a onloaded output.
*
* In an application setting, this is always called with the output of our
* local transfer as an argument. For tests, we can call this function
* explicitly using local test data.
*/
void OnloadProcessor::operator()(OnloadStepOutput const& out) const
{
ScopedProfiling profile_this{"process-onloaded-steps"};
trace_counter("process-onloaded-steps", out.size());
for (auto i : range(out.size()))
{
(*this)(out, i);
}
}

//---------------------------------------------------------------------------//
/*!
* Process steps already copied into temporary CPU storage.
*/
void OnloadProcessor::process_local_steps(OnloadStepOutput const& steps)
{
if (steps)
{
num_hits_ += steps.size();
(*this)(steps);
}
}

//---------------------------------------------------------------------------//
/*!
* Generate and call a single onloaded step.
*/
void OnloadProcessor::operator()(OnloadStepOutput const& out, size_type i) const
{
CELER_EXPECT(i < out.size());

// TODO: reconstruct tracks and IDs if particles were provided

// TODO: invoke poststep processes
std::cout << "Call PostStep" << std::endl;
Comment on lines +174 to +179

@esseivaju esseivaju Dec 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should use the CELER_NOT_IMPLEMENTED and CELER_LOG_LOCAL macros here

}

//---------------------------------------------------------------------------//
// EXPLICIT INSTANTIATION
//---------------------------------------------------------------------------//
template class StepTransfer<OnloadStepOutput,
HostRef<OnloadStateData>,
DeviceRef<OnloadStateData>>;

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
146 changes: 146 additions & 0 deletions src/celeritas/ext/detail/OnloadProcessor.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/ext/detail/OnloadProcessor.hh
//---------------------------------------------------------------------------//
#pragma once

#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <G4TouchableHandle.hh>

#include "corecel/Macros.hh"
#include "corecel/Types.hh"
#include "corecel/cont/EnumArray.hh"
#include "celeritas/Types.hh"
#include "celeritas/user/OnloadData.hh"
#include "celeritas/user/OnloadSteps.hh"

#include "StepTransfer.hh"
#include "TouchableUpdaterInterface.hh"
#include "../GeantTrackReconstruction.hh"

class G4ParticleDefinition;
class G4Step;
class G4StepPoint;
class G4Track;

namespace celeritas
{
struct OnloadStepOutput;

namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Transfer Celeritas onloaded steps to Geant4.
*
* This serves a similar purpose to the HitProcessor class for hit objects, and
* follows the same host/device phases (compacted, copied, processed). The
* device and pinned host buffers and the state machine that sequences those
* phases are owned by the \c StepTransfer member: see its documentation. This
* class decides when to take the copied output from the transfer and
* processes it. Since the copy overwrites the transferred output,
* \c copy_pending_steps processes any previously copied steps first, and does
* nothing if no compacted steps are pending.
*/
class OnloadProcessor

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think now that we extracted the DtoH pipeline to StepTransfer, the different processors (OnloadProcessor, HitProcessor) could share a common base StepProcessor which holds and drive the StepTransfer.

Maybe that's fine as a follow-up

{
public:
//!@{
//! \name Type aliases
using StepStateHostRef = HostRef<OnloadStateData>;
using StepStateDeviceRef = DeviceRef<OnloadStateData>;
using VecParticle = std::vector<G4ParticleDefinition const*>;
//!@}

public:
// Construct from particles
OnloadProcessor(VecParticle const& particles);

CELER_DEFAULT_MOVE_DELETE_COPY(OnloadProcessor);

// Process CPU-generated steps
void operator()(StepStateHostRef const&);

// Allocate pinned host output for the largest possible device step
void reserve(StepStateDeviceRef const&);

// Enqueue compaction of device-generated steps for later processing
void operator()(StepStateDeviceRef const&);

// Enqueue the copy of compacted device steps after their step completes
void copy_pending_steps();

// Wait for and process copied device steps
void process_copied_steps();

// Process copied device steps if the copy has completed
void process_copied_steps_if_ready();

//! Whether device-generated step data is pending
bool has_pending_steps() const noexcept { return transfer_.has_pending(); }

// Generate and call steps from a onloaded output (for testing)
void operator()(OnloadStepOutput const& out) const;

// Generate and call steps from a single onloaded step
void operator()(OnloadStepOutput const& out, size_type i) const;

// Get and reset the steps counted (generally once per event)
inline size_type exchange_hits();
Comment on lines +93 to +94

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the name num_hits_ and exchange_hits is a bit confusing here since we're not counting hits. Maybe should be renamed exchange_steps and implemented in the base class.


//! Access local Geant4 track metadata reconstruction
std::shared_ptr<GeantTrackReconstruction> const& track_reconstruction() const
{
return track_reconstruction_;
}

private:
using Transfer = StepTransfer<OnloadStepOutput,
HostRef<OnloadStateData>,
DeviceRef<OnloadStateData>>;

//! Copy of host or device steps to temporary CPU storage
Transfer transfer_;

//! Shared step object
std::shared_ptr<G4Step> step_;

//! Track reconstruction for step processing
std::shared_ptr<GeantTrackReconstruction> track_reconstruction_;

//! Navigator for finding points
std::unique_ptr<TouchableUpdaterInterface> update_touchable_;
Comment on lines +116 to +117

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Isupdate_touchable_ planed to be used later? At the moment it is not used at all and could be dropped


//! Accumulated number of steps
size_type num_hits_{0};

// Process steps already copied into temporary CPU storage
void process_local_steps(OnloadStepOutput const&);
};

// Instantiated in OnloadProcessor.cc
extern template class StepTransfer<OnloadStepOutput,
HostRef<OnloadStateData>,
DeviceRef<OnloadStateData>>;

//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Get and reset number of steps counted (generally once per event).
*/
size_type OnloadProcessor::exchange_hits()
{
using namespace celeritas::literals;
CELER_EXPECT(!this->has_pending_steps());
return std::exchange(num_hits_, 0_sz);
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
Loading
Loading