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_mrTool.retrieve());
27 ATH_CHECK(m_hostCopyTool.retrieve());
28 ATH_CHECK(m_deviceCopyTool.retrieve());
29 ATH_CHECK(m_streamTool.retrieve());
30
31 // Print some information about the configuration:
32 ATH_MSG_INFO("Input container key: " << m_inputKey);
33 ATH_MSG_INFO("Output container key: " << m_outputKey);
34
35 // Return gracefully.
36 return StatusCode::SUCCESS;
37}
38
40 const EventContext& ctx) const {
41
42 // Retrieve the input container.
43 auto inputHandle = SG::makeHandle(m_inputKey, ctx);
44 const xAOD::TrackParticleContainer* input = inputHandle.cptr();
45 if (input == nullptr) {
46 ATH_MSG_ERROR("Failed to retrieve input container from: " << m_inputKey);
47 return StatusCode::FAILURE;
48 }
49
50 // The number of input/output tracks.
51 const std::size_t nTracks = input->size();
52
53 // If the input container is empty, then create an empty output container, and
54 // be done with it.
55 if (nTracks == 0) {
56 auto output = std::make_unique<xAOD::TrackParticleContainer>();
57 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
58 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
59 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
60 return StatusCode::SUCCESS;
61 }
62
63 // Get the CUDA stream to use.
64 cudaStream_t stream = m_streamTool->stream(ctx);
65
66 // The object managing host memory copies.
67 auto hostCopy = m_hostCopyTool->copy(ctx);
68 // The object managing device memory copies.
69 auto deviceCopy = m_deviceCopyTool->copy(ctx);
70
71 // Buffer size(s) to create.
72 const std::vector<unsigned int> bufferSize(input->size(), 0u);
73
74 // Construct input buffer(s).
75 auto inputHostBuffer =
76 m_mrTool->hostMR()
77 ? traccc::edm::track_collection<
78 traccc::default_algebra>::buffer{bufferSize,
79 *(m_mrTool->hostMR())}
80 : traccc::edm::track_collection<traccc::default_algebra>::buffer{
81 bufferSize, m_mrTool->mainMR()};
82 auto inputDeviceBuffer =
83 m_mrTool->hostMR()
84 ? traccc::edm::track_collection<
85 traccc::default_algebra>::buffer{bufferSize, m_mrTool->mainMR(),
86 m_mrTool->hostMR()}
87 : traccc::edm::track_collection<traccc::default_algebra>::buffer{};
88 hostCopy->setup(inputHostBuffer)->wait();
89 deviceCopy->setup(inputDeviceBuffer)->ignore();
90
91 // Copy the relevant data into the input buffer.
92 traccc::edm::track_collection<traccc::default_algebra>::device inputHost{
93 inputHostBuffer};
94 for (unsigned int i = 0; i < input->size(); ++i) {
95 inputHost[i].params().set_theta(input->at(i)->theta());
96 inputHost[i].params().set_phi(input->at(i)->phi());
97 inputHost[i].params().set_qop(input->at(i)->qOverP());
98 }
99
100 // Copy the input buffer to the device.
101 if (m_mrTool->hostMR()) {
102 (*deviceCopy)(inputHostBuffer, inputDeviceBuffer)->ignore();
103 }
104
105 // Construct output buffer(s).
106 traccc::edm::track_collection<traccc::default_algebra>::buffer
107 outputDeviceBuffer(bufferSize, m_mrTool->mainMR(), m_mrTool->hostMR());
108 if (m_mrTool->hostMR()) {
109 deviceCopy->setup(outputDeviceBuffer)->ignore();
110 } else {
111 hostCopy->setup(outputDeviceBuffer)->ignore();
112 }
113 traccc::edm::track_collection<traccc::default_algebra>::host
114 outputHostCollection(m_mrTool->hostMR() ? *(m_mrTool->hostMR())
115 : m_mrTool->mainMR());
116
117 // Run the kernel.
119 stream, m_mrTool->hostMR() ? inputDeviceBuffer : inputHostBuffer,
120 outputDeviceBuffer));
121
122 // Get the output back to the host.
123 if (m_mrTool->hostMR()) {
124 (*deviceCopy)(outputDeviceBuffer, outputHostCollection)->wait();
125 } else {
126 (*hostCopy)(outputDeviceBuffer, outputHostCollection)->wait();
127 }
128
129 // Construct the output container.
130 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
131 SG::copyAuxStoreThinned(*(input->getConstStore()), *outputAux, nullptr);
132 auto output = std::make_unique<xAOD::TrackParticleContainer>();
133 for (std::size_t i = 0; i < nTracks; ++i) {
134 output->push_back(std::make_unique<xAOD::TrackParticle>());
135 }
136 output->setStore(outputAux.get());
137 for (std::size_t i = 0; i < nTracks; ++i) {
138 xAOD::TrackParticle* track = output->at(i);
139 track->setDefiningParameters(input->at(i)->d0(), input->at(i)->z0(),
140 outputHostCollection[i].params().phi(),
141 outputHostCollection[i].params().theta(),
142 outputHostCollection[i].params().qop());
143 }
144
145 // Record the output container.
146 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
147 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
148
149 // Return gracefully.
150 return StatusCode::SUCCESS;
151}
152
153} // 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.
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
ToolHandle< AthDevice::ICopyTool > m_deviceCopyTool
Device copy tool to use.
ToolHandle< AthCUDA::IStreamTool > m_streamTool
Stream tool to use.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inputKey
The input container.
ToolHandle< AthDevice::IMemoryResourcesTool > m_mrTool
Host memory resource tool to use.
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".