ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaExamples
AthExDevice
src
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).
4
#include "
EventDataCopyExampleAlg.h
"
5
6
// System include(s).
7
#include <cassert>
8
#include <memory>
9
10
namespace
AthExDevice
{
11
12
StatusCode
EventDataCopyExampleAlg::initialize
() {
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
26
StatusCode
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
ATH_CHECK
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition
AthCheckMacros.h:40
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition
AthMsgStreamMacros.h:29
EventDataCopyExampleAlg.h
AthExDevice::DeviceObject
Interface to the example SoA collection.
Definition
DeviceObjectCollection.h:15
AthExDevice::DeviceObject::phi
auto & phi()
Definition
DeviceObjectCollection.h:36
AthExDevice::DeviceObject::eta
auto & eta()
Definition
DeviceObjectCollection.h:33
AthExDevice::DeviceObject::indices
auto & indices()
Definition
DeviceObjectCollection.h:39
AthExDevice::EventDataCopyExampleAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
Definition
EventDataCopyExampleAlg.cxx:26
AthExDevice::EventDataCopyExampleAlg::m_inputKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_inputKey
The input container.
Definition
EventDataCopyExampleAlg.h:46
AthExDevice::EventDataCopyExampleAlg::m_mrs
ToolHandle< AthDevice::IMemoryResourcesTool > m_mrs
The memory resources tool to use for allocating memory on the device.
Definition
EventDataCopyExampleAlg.h:59
AthExDevice::EventDataCopyExampleAlg::m_copies
ToolHandle< AthDevice::ICopiesTool > m_copies
The copies tool to use for copying data to/from the device.
Definition
EventDataCopyExampleAlg.h:55
AthExDevice::EventDataCopyExampleAlg::initialize
virtual StatusCode initialize() override
Function initialising the algorithm.
Definition
EventDataCopyExampleAlg.cxx:12
AthExDevice::EventDataCopyExampleAlg::m_outputKey
SG::WriteHandleKey< DeviceObjectCollection::buffer > m_outputKey
The output container.
Definition
EventDataCopyExampleAlg.h:50
xAOD::TrackParticle_v1::phi
virtual double phi() const override final
The azimuthal angle ( ) of the particle (has range to .).
xAOD::TrackParticle_v1::eta
virtual double eta() const override final
The pseudorapidity ( ) of the particle.
Definition
TrackParticle_v1.cxx:79
AthExDevice
Definition
DeviceObjectCollection.h:11
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition
ReadCondHandle.h:269
xAOD::TrackParticle
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Definition
Event/xAOD/xAODTracking/xAODTracking/TrackParticle.h:13
xAOD::TrackParticleContainer
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Definition
Event/xAOD/xAODTracking/xAODTracking/TrackParticleContainer.h:14
Generated on
for ATLAS Offline Software by
1.17.0