ATLAS Offline Software
Loading...
Searching...
No Matches
TrackParticleCalibratorExampleAlg.cxx
Go to the documentation of this file.
1//
2// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3//
4
5// Local include(s).
7
8// Framework include(s).
13
14// System include(s).
15#include <cstring>
16
17namespace AthCUDAExamples {
18
20
21 // Initialize the keys.
22 ATH_CHECK(m_inputKey.initialize());
23 ATH_CHECK(m_outputKey.initialize());
24
25 // Initialize the tools.
26 ATH_CHECK(m_hostMR.retrieve());
27 ATH_CHECK(m_deviceMR.retrieve());
28 ATH_CHECK(m_hostCopyTool.retrieve());
29 ATH_CHECK(m_deviceCopyTool.retrieve());
30 ATH_CHECK(m_streamTool.retrieve());
31
32 // Print some information about the configuration:
33 ATH_MSG_INFO("Input container key: " << m_inputKey);
34 ATH_MSG_INFO("Output container key: " << m_outputKey);
35
36 // Return gracefully.
37 return StatusCode::SUCCESS;
38}
39
41 const EventContext& ctx) const {
42
43 // Retrieve the input container.
44 auto inputHandle = SG::makeHandle(m_inputKey, ctx);
45 const xAOD::TrackParticleContainer* input = inputHandle.cptr();
46 if (input == nullptr) {
47 ATH_MSG_ERROR("Failed to retrieve input container from: " << m_inputKey);
48 return StatusCode::FAILURE;
49 }
50
51 // The number of input/output tracks.
52 const std::size_t nTracks = input->size();
53
54 // If the input container is empty, then create an empty output container, and
55 // be done with it.
56 if (nTracks == 0) {
57 auto output = std::make_unique<xAOD::TrackParticleContainer>();
58 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
59 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
60 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
61 return StatusCode::SUCCESS;
62 }
63
64 // Get the CUDA stream to use.
65 cudaStream_t stream = m_streamTool->stream(ctx);
66
67 // The object managing host memory copies.
68 auto hostCopy = m_hostCopyTool->copy(ctx);
69 // The object managing device memory copies.
70 auto deviceCopy = m_deviceCopyTool->copy(ctx);
71
72 // Construct input buffer(s).
73 traccc::edm::track_collection<traccc::default_algebra>::buffer
74 inputHostBuffer(std::vector<unsigned int>(input->size(), 0u),
75 m_hostMR->mr());
76 traccc::edm::track_collection<traccc::default_algebra>::buffer
77 inputDeviceBuffer(std::vector<unsigned int>(input->size(), 0u),
78 m_deviceMR->mr(), &(m_hostMR->mr()));
79 hostCopy->setup(inputHostBuffer)->wait();
80 deviceCopy->setup(inputDeviceBuffer)->ignore();
81
82 // Copy the relevant data into the input buffer.
83 traccc::edm::track_collection<traccc::default_algebra>::device inputHost{
84 inputHostBuffer};
85 for (unsigned int i = 0; i < input->size(); ++i) {
86 inputHost[i].params().set_theta(input->at(i)->theta());
87 inputHost[i].params().set_phi(input->at(i)->phi());
88 inputHost[i].params().set_qop(input->at(i)->qOverP());
89 }
90
91 // Copy the input buffer to the device.
92 (*deviceCopy)(inputHostBuffer, inputDeviceBuffer)->ignore();
93
94 // Construct output buffer(s).
95 traccc::edm::track_collection<traccc::default_algebra>::buffer
96 outputDeviceBuffer(std::vector<unsigned int>(input->size(), 0u),
97 m_deviceMR->mr(), &(m_hostMR->mr()));
98 deviceCopy->setup(outputDeviceBuffer)->ignore();
99 traccc::edm::track_collection<traccc::default_algebra>::host
100 outputHostCollection(m_hostMR->mr());
101
102 // Run the kernel.
103 ATH_CHECK(calibrateOnGPU(stream, inputDeviceBuffer, outputDeviceBuffer));
104
105 // Get the output back to the host.
106 (*deviceCopy)(outputDeviceBuffer, outputHostCollection)->wait();
107
108 // Construct the output container.
109 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
110 SG::copyAuxStoreThinned(*(input->getConstStore()), *outputAux, nullptr);
111 auto output = std::make_unique<xAOD::TrackParticleContainer>();
112 for (std::size_t i = 0; i < nTracks; ++i) {
113 output->push_back(std::make_unique<xAOD::TrackParticle>());
114 }
115 output->setStore(outputAux.get());
116 for (std::size_t i = 0; i < nTracks; ++i) {
117 xAOD::TrackParticle* track = output->at(i);
118 track->setDefiningParameters(input->at(i)->d0(), input->at(i)->z0(),
119 outputHostCollection[i].params().phi(),
120 outputHostCollection[i].params().theta(),
121 outputHostCollection[i].params().qop());
122 }
123
124 // Record the output container.
125 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
126 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
127
128 // Return gracefully.
129 return StatusCode::SUCCESS;
130}
131
132} // namespace AthCUDAExamples
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
ToolHandle< AthDevice::IMemoryResourceTool > m_deviceMR
Device memory resource tool to use.
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
ToolHandle< AthDevice::ICopyTool > m_deviceCopyTool
Device copy tool to use.
ToolHandle< AthDevice::IMemoryResourceTool > m_hostMR
Host memory resource tool to use.
ToolHandle< AthCUDA::IStreamTool > m_streamTool
Stream tool to use.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inputKey
The input container.
ToolHandle< AthDevice::ICopyTool > m_hostCopyTool
Host copy tool to use.
SG::WriteHandleKey< xAOD::TrackParticleContainer > m_outputKey
The output container.
virtual StatusCode initialize() override
Function initialising the algorithm.
Helper to copy an aux store while applying thinning.
StatusCode calibrateOnGPU(cudaStream_t stream, const traccc::edm::track_collection< traccc::default_algebra >::const_view &input, traccc::edm::track_collection< traccc::default_algebra >::view &output)
Perform the transformation on an NVIDIA GPU.
void copyAuxStoreThinned(const SG::IConstAuxStore &orig, SG::IAuxStore &copy, const SG::ThinningInfo *info)
Helper to copy an aux store while applying thinning.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".