Repository navigation
Add an onload process and onload steps #2715
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| // EXPLICIT INSTANTIATION | ||
| //---------------------------------------------------------------------------// | ||
| template class StepTransfer<OnloadStepOutput, | ||
| HostRef<OnloadStateData>, | ||
| DeviceRef<OnloadStateData>>; | ||
|
|
||
| //---------------------------------------------------------------------------// | ||
| } // namespace detail | ||
| } // namespace celeritas | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think now that we extracted the DtoH pipeline to 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name |
||
|
|
||
| //! 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
|
|
||
| //! 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 | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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