ATLAS Offline Software
EFTrackingXrtAlgorithm.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3  */
4 
5 #include "AthenaKernel/Chrono.h"
6 
8 
10  const std::string& name,
11  ISvcLocator* pSvcLocator
12 ) : AthReentrantAlgorithm(name, pSvcLocator)
13 {}
14 
16  ATH_MSG_INFO("Initializing " << name());
17 
18  ATH_CHECK(m_DeviceMgmtSvc.retrieve());
19  ATH_CHECK(m_chronoSvc.retrieve());
20  ATH_CHECK(m_inputDataStreamKeys.initialize());
21  ATH_CHECK(m_vSizeDataStreamKeys.initialize());
22  ATH_CHECK(m_outputDataStreamKeys.initialize());
23 
24  for (const auto& [kernelName, storeGateKey, argumentIndex] : m_inputInterfaces) {
25  const std::vector<std::shared_ptr<xrt::device>> devices =
26  m_DeviceMgmtSvc->get_xrt_devices_by_kernel_name(kernelName);
27 
28  ATH_CHECK(devices.size() != 0);
29 
30  if (!m_kernels.contains(kernelName)) {
31  m_kernels[kernelName] = std::make_unique<xrt::kernel>(
32  *(devices[0]),
33  (devices[0])->get_xclbin_uuid(),
34  kernelName,
35  xrt::kernel::cu_access_mode::exclusive
36  );
37  }
38 
39  ATH_CHECK(m_kernels[kernelName].get() != nullptr);
40  m_inputBuffers.emplace_back(
41  *(devices[0]),
42  sizeof(unsigned long) * m_bufferSize,
43  xrt::bo::flags::normal,
44  m_kernels[kernelName]->group_id(argumentIndex)
45  );
46 
47  if (!m_runs.contains(kernelName)) {
48  m_runs[kernelName] = std::make_unique<xrt::run>(*m_kernels[kernelName]);
49  }
50 
51  ATH_CHECK(m_runs[kernelName].get() != nullptr);
52  m_runs[kernelName]->set_arg(argumentIndex, m_inputBuffers.back());
53  }
54 
55  for (const auto& [kernelName, storeGateKey, argumentIndex] : m_vSizeInterfaces) {
56  const std::vector<std::shared_ptr<xrt::device>> devices =
57  m_DeviceMgmtSvc->get_xrt_devices_by_kernel_name(kernelName);
58 
59  ATH_CHECK(devices.size() != 0);
60 
61  if (!m_kernels.contains(kernelName)) {
62  m_kernels[kernelName] = std::make_unique<xrt::kernel>(
63  *(devices[0]),
64  (devices[0])->get_xclbin_uuid(),
65  kernelName,
66  xrt::kernel::cu_access_mode::exclusive
67  );
68  }
69 
70  ATH_CHECK(m_kernels[kernelName].get() != nullptr);
71 
72  if (!m_runs.contains(kernelName)) {
73  m_runs[kernelName] = std::make_unique<xrt::run>(*m_kernels[kernelName]);
74  }
75 
76  ATH_CHECK(m_runs[kernelName].get() != nullptr);
77  }
78 
79  for (const auto& [kernelName, storeGateKey, argumentIndex] : m_outputInterfaces) {
80  const std::vector<std::shared_ptr<xrt::device>> devices =
81  m_DeviceMgmtSvc->get_xrt_devices_by_kernel_name(kernelName);
82 
83  ATH_CHECK(devices.size() != 0);
84 
85  if (!m_kernels.contains(kernelName)) {
86  m_kernels[kernelName] = std::make_unique<xrt::kernel>(
87  *(devices[0]),
88  devices[0]->get_xclbin_uuid(),
89  kernelName,
90  xrt::kernel::cu_access_mode::exclusive
91  );
92  }
93 
94  m_outputBuffers.emplace_back(
95  *(devices[0]),
96  sizeof(unsigned long) * m_bufferSize,
97  xrt::bo::flags::normal,
98  m_kernels[kernelName]->group_id(argumentIndex)
99  );
100 
101  if (!m_runs.contains(kernelName)) {
102  m_runs[kernelName] = std::make_unique<xrt::run>(*m_kernels[kernelName]);
103  }
104 
105  ATH_CHECK(m_runs[kernelName].get() != nullptr);
106  m_runs[kernelName]->set_arg(argumentIndex, m_outputBuffers.back());
107  }
108 
109  return StatusCode::SUCCESS;
110 }
111 
112 StatusCode EFTrackingXrtAlgorithm::execute(const EventContext& ctx) const
113 {
114  ATH_CHECK(m_inputDataStreamKeys.size() == m_inputBuffers.size());
115  std::size_t inputHandleIndex = 0;
116  for (
117  const SG::ReadHandleKey<std::vector<unsigned long>>& inputDataStreamKey :
119  ) {
121  ATH_MSG_DEBUG("Writing: " << inputDataStream.name());
122  unsigned long* inputMap = m_inputBuffers.at(inputHandleIndex).map<unsigned long*>();
123 
125 
126  ATH_MSG_DEBUG("Copy " + inputDataStream.name() + " from storegate to host side map");
127  {
128  Athena::Chrono chrono(
129  "Copy " + inputDataStream.name() + " from storegate to host side map",
130  m_chronoSvc.get()
131  );
132 
133  for (std::size_t index = 0; index < inputDataStream->size(); index++) {
134  inputMap[index] = inputDataStream->at(index);
135  }
136  }
137 
138  ATH_MSG_DEBUG("Copy " + inputDataStream.name() + " from host side map to device");
139  {
140  Athena::Chrono chrono(
141  "Copy " + inputDataStream.name() + " from host side map to device",
142  m_chronoSvc.get()
143  );
144 
145  m_inputBuffers.at(inputHandleIndex).sync(XCL_BO_SYNC_BO_TO_DEVICE);
146  }
147 
148  inputHandleIndex++;
149  }
150 
152  std::size_t vSizeHandleIndex = 0;
153  for (
154  const SG::ReadHandleKey<std::vector<unsigned long>>& vSizeDataStreamKey :
156  ) {
157  SG::ReadHandle<std::vector<unsigned long>> vSizeDataStream(vSizeDataStreamKey, ctx);
158  ATH_MSG_DEBUG("Setting VSize: " << vSizeDataStream.name());
159  const auto& [kernelName, storeGateKey, argumentIndex] = m_vSizeInterfaces[vSizeHandleIndex];
160 
161  m_runs.at(kernelName)->set_arg(argumentIndex, vSizeDataStream->size());
162  }
163 
164  ATH_MSG_DEBUG("Run kernels");
165  {
166  Athena::Chrono chrono("Run kernels", m_chronoSvc.get());
167 
168  for (const auto& [key, run] : m_runs) {
169  run->start();
170  }
171 
172  for (const auto& [key, run] : m_runs) {
173  run->wait();
174  }
175  }
176 
177  std::size_t outputHandleIndex = 0;
178  for (
179  const SG::WriteHandleKey<std::vector<unsigned long>>& outputDataStreamKey :
181  ) {
183  ATH_CHECK(outputDataStream.record(std::make_unique<std::vector<unsigned long>>(m_bufferSize)));
184 
185  ATH_MSG_DEBUG("Copy " + outputDataStream.name() + " from device to host side map");
186  {
187  Athena::Chrono chrono(
188  "Copy " + outputDataStream.name() + " from device to host side map",
189  m_chronoSvc.get()
190  );
191 
192  m_outputBuffers.at(outputHandleIndex).sync(XCL_BO_SYNC_BO_FROM_DEVICE);
193  }
194 
195  const unsigned long* outputMap = m_outputBuffers.at(outputHandleIndex).map<unsigned long*>();
196  ATH_MSG_DEBUG("Copy " + outputDataStream.name() + " from host side map to storegate");
197  {
198  Athena::Chrono chrono(
199  "Copy " + outputDataStream.name() + " from host side map to storegate",
200  m_chronoSvc.get()
201  );
202 
203  for (std::size_t index = 0; index < outputDataStream->size(); index++) {
204  outputDataStream->at(index) = outputMap[index];
205  }
206  }
207 
208  outputHandleIndex++;
209  }
210 
211  return StatusCode::SUCCESS;
212 }
213 
EFTrackingXrtAlgorithm::m_outputInterfaces
Gaudi::Property< std::vector< std::tuple< std::string, std::string, int > > > m_outputInterfaces
Definition: EFTrackingXrtAlgorithm.h:72
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PathfinderHlsConfig.inputDataStream
inputDataStream
Definition: PathfinderHlsConfig.py:66
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
index
Definition: index.py:1
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
EFTrackingXrtAlgorithm::EFTrackingXrtAlgorithm
EFTrackingXrtAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: EFTrackingXrtAlgorithm.cxx:9
Chrono.h
Exception-safe IChronoSvc caller.
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
EFTrackingXrtAlgorithm::m_vSizeInterfaces
Gaudi::Property< std::vector< std::tuple< std::string, std::string, int > > > m_vSizeInterfaces
Definition: EFTrackingXrtAlgorithm.h:65
EFTrackingXrtAlgorithm::m_kernels
std::map< std::string, std::unique_ptr< xrt::kernel > > m_kernels
Definition: EFTrackingXrtAlgorithm.h:86
SG::WriteHandleKey
Property holding a SG store/key/clid from which a WriteHandle is made.
Definition: StoreGate/StoreGate/WriteHandleKey.h:40
Athena::Chrono
Exception-safe IChronoSvc caller.
Definition: Chrono.h:50
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
run
Definition: run.py:1
EFTrackingXrtAlgorithm::m_inputDataStreamKeys
SG::ReadHandleKeyArray< std::vector< unsigned long > > m_inputDataStreamKeys
Keys to access encoded 64bit words following the EFTracking specification.
Definition: EFTrackingXrtAlgorithm.h:40
EFTrackingXrtAlgorithm::m_DeviceMgmtSvc
ServiceHandle< AthXRT::IDeviceMgmtSvc > m_DeviceMgmtSvc
Definition: EFTrackingXrtAlgorithm.h:44
EFTrackingXrtAlgorithm::m_inputInterfaces
Gaudi::Property< std::vector< std::tuple< std::string, std::string, int > > > m_inputInterfaces
Definition: EFTrackingXrtAlgorithm.h:58
EFTrackingXrtAlgorithm::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: EFTrackingXrtAlgorithm.cxx:112
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
EFTrackingXrtAlgorithm::m_vSizeDataStreamKeys
SG::ReadHandleKeyArray< std::vector< unsigned long > > m_vSizeDataStreamKeys
Definition: EFTrackingXrtAlgorithm.h:41
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
DeMoScan.index
string index
Definition: DeMoScan.py:362
EFTrackingXrtAlgorithm::initialize
StatusCode initialize() override final
Definition: EFTrackingXrtAlgorithm.cxx:15
PathfinderHlsConfig.outputDataStream
outputDataStream
Definition: PathfinderHlsConfig.py:93
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
EFTrackingXrtAlgorithm.h
EFTrackingXrtAlgorithm::m_bufferSize
Gaudi::Property< std::size_t > m_bufferSize
Definition: EFTrackingXrtAlgorithm.h:79
EFTrackingXrtAlgorithm::m_outputDataStreamKeys
SG::WriteHandleKeyArray< std::vector< unsigned long > > m_outputDataStreamKeys
Definition: EFTrackingXrtAlgorithm.h:42
EFTrackingXrtAlgorithm::m_runs
std::map< std::string, std::unique_ptr< xrt::run > > m_runs
Definition: EFTrackingXrtAlgorithm.h:87
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
EFTrackingXrtAlgorithm::m_chronoSvc
ServiceHandle< IChronoSvc > m_chronoSvc
Definition: EFTrackingXrtAlgorithm.h:51