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
9
10// Framework include(s).
15
16// VecMem include(s).
17#include <vecmem/memory/cuda/device_memory_resource.hpp>
18#include <vecmem/memory/host_memory_resource.hpp>
19#include <vecmem/utils/cuda/copy.hpp>
20
21// System include(s).
22#include <cstring>
23
24namespace AthCUDAExamples {
25
27
28 // Initialize the keys.
29 ATH_CHECK(m_inputKey.initialize());
30 ATH_CHECK(m_outputKey.initialize());
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
65 vecmem::host_memory_resource hostMR;
67 vecmem::cuda::device_memory_resource deviceMR;
68 // The object managing CUDA memory copies.
69 vecmem::cuda::copy copy;
70
71 // Construct input buffer(s).
72 TrackParticleContainer::buffer inputHostBuffer(input->size(), hostMR);
73 TrackParticleContainer::buffer inputDeviceBuffer(input->size(), deviceMR);
74
75 // Copy the relevant data into the input buffer.
76 static const SG::AuxElement::ConstAccessor<float> thetaAcc("theta");
77 static const SG::AuxElement::ConstAccessor<float> phiAcc("phi");
78 static const SG::AuxElement::ConstAccessor<float> qOverPAcc("qOverP");
79 std::memcpy(inputHostBuffer.get<0>().ptr(), thetaAcc.getDataArray(*input),
80 nTracks * sizeof(float));
81 std::memcpy(inputHostBuffer.get<1>().ptr(), phiAcc.getDataArray(*input),
82 nTracks * sizeof(float));
83 std::memcpy(inputHostBuffer.get<2>().ptr(), qOverPAcc.getDataArray(*input),
84 nTracks * sizeof(float));
85
86 // Copy the input buffer to the device.
87 copy(inputHostBuffer, inputDeviceBuffer)->wait();
88
89 // Construct output buffer(s).
90 TrackParticleContainer::buffer outputDeviceBuffer(input->size(), deviceMR);
91 TrackParticleContainer::buffer outputHostBuffer(input->size(), hostMR);
92
93 // Run the kernel.
94 ATH_CHECK(calibrateOnGPU(inputDeviceBuffer, outputDeviceBuffer));
95
96 // Get the output back to the host.
97 copy(outputDeviceBuffer, outputHostBuffer)->wait();
98
99 // Construct the output container.
100 auto outputAux = std::make_unique<xAOD::AuxContainerBase>();
101 SG::copyAuxStoreThinned(*(input->getConstStore()), *outputAux, nullptr);
102 std::memcpy(outputAux->getData(thetaAcc.auxid(), nTracks, nTracks),
103 outputHostBuffer.get<0>().ptr(), nTracks * sizeof(float));
104 std::memcpy(outputAux->getData(phiAcc.auxid(), nTracks, nTracks),
105 outputHostBuffer.get<1>().ptr(), nTracks * sizeof(float));
106 std::memcpy(outputAux->getData(qOverPAcc.auxid(), nTracks, nTracks),
107 outputHostBuffer.get<2>().ptr(), nTracks * sizeof(float));
108 auto output = std::make_unique<xAOD::TrackParticleContainer>();
109 for (std::size_t i = 0; i < nTracks; ++i) {
110 output->push_back(new xAOD::TrackParticle());
111 }
112 output->setStore(outputAux.get());
113
114 // Record the output container.
115 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
116 ATH_CHECK(outputHandle.record(std::move(output), std::move(outputAux)));
117
118 // Return gracefully.
119 return StatusCode::SUCCESS;
120}
121
122} // 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.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inputKey
The input container.
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(const TrackParticleContainer::const_view &input, TrackParticleContainer::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".