ATLAS Offline Software
Loading...
Searching...
No Matches
EventDataCopyExampleAlg.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3// Local include(s).
5
6// System include(s).
7#include <cassert>
8#include <memory>
9
10namespace AthExDevice {
11
13
14 // Initialize the StoreGate keys.
15 ATH_CHECK(m_inputKey.initialize());
16 ATH_CHECK(m_outputKey.initialize());
17
18 // Retrieve the tools.
19 ATH_CHECK(m_copies.retrieve());
20 ATH_CHECK(m_mrs.retrieve());
21
22 // Return gracefully.
23 return StatusCode::SUCCESS;
24}
25
26StatusCode EventDataCopyExampleAlg::execute(const EventContext& ctx) const {
27
28 // Create the StoreGate handles.
29 auto inputHandle = SG::makeHandle(m_inputKey, ctx);
30 auto outputHandle = SG::makeHandle(m_outputKey, ctx);
31
32 // Helper lambda for copying the payload from the input (host) container into
33 // a host-accessible output buffer.
34 auto copyToBuffer = [](const xAOD::TrackParticleContainer& input,
35 DeviceObjectCollection::buffer& output) {
36 assert(input.size() == output.capacity());
37 DeviceObjectCollection::device helper{output};
38 for (size_t i = 0; i < input.size(); ++i) {
39 const xAOD::TrackParticle* inputObj = input.at(i);
40 DeviceObject outputObj = helper.at(i);
41 outputObj.eta() = inputObj->eta();
42 outputObj.phi() = inputObj->phi();
43 outputObj.indices().at(0u) = 1u;
44 outputObj.indices().at(1u) = 2u;
45 }
46 };
47
48 // Decide whether an explicit host->device copy will be necessary or not.
49 if (m_mrs->hostMR() == nullptr) {
50
51 // In this case we have just one "main" memory resource. This should mean
52 // that the memory provided by that one memory resource, is accessible
53 // from both the host and the device.
54
55 // Tell the user what we're doing.
56 ATH_MSG_DEBUG("Using a single memory resource for both host and device");
57
58 // Create the final/output buffer.
59 auto output = std::make_unique<DeviceObjectCollection::buffer>(
60 std::vector<unsigned int>(inputHandle->size(), 2), m_mrs->mainMR());
61 m_copies->hostCopy(ctx)->setup(*output)->wait();
62
63 // Fill it with data directly.
64 copyToBuffer(*inputHandle, *output);
65
66 // Record the output buffer in StoreGate.
67 ATH_CHECK(outputHandle.record(std::move(output)));
68
69 } else {
70
71 // In this case we have separate "device"/"main" and "host" memory
72 // resources. In this case we first need to set up a buffer on the host, and
73 // copy data into that. Then we can set up a buffer on the device, and
74 // perform the host->device copy between the two buffers.
75
76 // Tell the user what we're doing.
77 ATH_MSG_DEBUG("Using separate memory resources for host and device");
78
79 // Create the host buffer.
80 DeviceObjectCollection::buffer hostBuffer(
81 std::vector<unsigned int>(inputHandle->size(), 2), *(m_mrs->hostMR()));
82 m_copies->hostCopy(ctx)->setup(hostBuffer)->wait();
83
84 // Fill it with data.
85 copyToBuffer(*inputHandle, hostBuffer);
86
87 // Get the "device" copy object.
88 auto deviceCopy = m_copies->deviceCopy(ctx);
89
90 // Create the device buffer.
91 auto output = std::make_unique<DeviceObjectCollection::buffer>(
92 std::vector<unsigned int>(inputHandle->size(), 2), m_mrs->mainMR(),
93 m_mrs->hostMR());
94 deviceCopy->setup(*output)->ignore();
95
96 // Perform the host->device copy.
97 (*deviceCopy)(hostBuffer, *output)->wait();
98
99 // Record the output buffer in StoreGate.
100 ATH_CHECK(outputHandle.record(std::move(output)));
101 }
102
103 // Return gracefully.
104 return StatusCode::SUCCESS;
105}
106
107} // namespace AthExDevice
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x)
Interface to the example SoA collection.
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inputKey
The input container.
ToolHandle< AthDevice::IMemoryResourcesTool > m_mrs
The memory resources tool to use for allocating memory on the device.
ToolHandle< AthDevice::ICopiesTool > m_copies
The copies tool to use for copying data to/from the device.
virtual StatusCode initialize() override
Function initialising the algorithm.
SG::WriteHandleKey< DeviceObjectCollection::buffer > m_outputKey
The output container.
virtual double phi() const override final
The azimuthal angle ( ) of the particle (has range to .).
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
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".