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 = static_cast<cudaStream_t>(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(vecmem::ignore_event, inputDeviceBuffer);
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)(vecmem::ignore_event, inputHostBuffer, inputDeviceBuffer,
103 vecmem::copy::type::copy_type::host_to_device);
104 }
105
106 // Construct output buffer(s).
107 traccc::edm::track_collection<traccc::default_algebra>::buffer
108 outputDeviceBuffer(bufferSize, m_mrTool->mainMR(), m_mrTool->hostMR());
109 if (m_mrTool->hostMR()) {
110 deviceCopy->setup(vecmem::ignore_event, outputDeviceBuffer);
111 } else {
112 hostCopy->setup(vecmem::ignore_event, outputDeviceBuffer);
113 }
114 traccc::edm::track_collection<traccc::default_algebra>::host
115 outputHostCollection(m_mrTool->hostMR() ? *(m_mrTool->hostMR())
116 : m_mrTool->mainMR());
117
118 // Run the kernel.
120 stream, m_mrTool->hostMR() ? inputDeviceBuffer : inputHostBuffer,
121 outputDeviceBuffer));
122
123 // Get the output back to the host.
124 if (m_mrTool->hostMR()) {
125 (*deviceCopy)(outputDeviceBuffer, outputHostCollection,
126 vecmem::copy::type::copy_type::device_to_host)
127 ->wait();
128 } else {
129 (*hostCopy)(outputDeviceBuffer, outputHostCollection,
130 vecmem::copy::type::copy_type::host_to_host)
131 ->wait();
132 }
133
134 // Construct the output container.
135 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
136 SG::copyAuxStoreThinned(*(input->getConstStore()), *outputAux, nullptr);
137 auto output = std::make_unique<xAOD::TrackParticleContainer>();
138 for (std::size_t i = 0; i < nTracks; ++i) {
139 output->push_back(std::make_unique<xAOD::TrackParticle>());
140 }
141 output->setStore(outputAux.get());
142 for (std::size_t i = 0; i < nTracks; ++i) {
143 xAOD::TrackParticle* track = output->at(i);
144 track->setDefiningParameters(input->at(i)->d0(), input->at(i)->z0(),
145 outputHostCollection[i].params().phi(),
146 outputHostCollection[i].params().theta(),
147 outputHostCollection[i].params().qop());
148 }
149
150 // Record the output container.
151 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
152 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
153
154 // Return gracefully.
155 return StatusCode::SUCCESS;
156}
157
158} // 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.
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".