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 
6 
8  const std::string& name,
9  ISvcLocator* pSvcLocator
10 ) : AthReentrantAlgorithm(name, pSvcLocator)
11 {}
12 
14  ATH_MSG_INFO("Initializing " << name());
15 
16  for (const auto& [kernelName, kernelDefinition] : m_kernelDefinitions) {
17  for (const auto& interfaceDefinition : kernelDefinition) {
18  const std::string& storeGateKey = interfaceDefinition.at("storeGateKey");
19 
20  // Todo: Reference the enum in the python to avoid painful string
21  // comparisons here.
22  if (interfaceDefinition.at("interfaceMode") == "INPUT") {
23  m_inputDataStreamKeys.push_back({storeGateKey});
24  ATH_CHECK(m_inputDataStreamKeys.back().initialize());
25  }
26  else if (interfaceDefinition.at("interfaceMode") == "OUTPUT") {
27  m_outputDataStreamKeys.push_back({storeGateKey});
28  ATH_CHECK(m_outputDataStreamKeys.back().initialize());
29  }
30  else {
31  ATH_MSG_ERROR("Failed to map kernel definitions to xrt objects.");
32 
33  return StatusCode::FAILURE;
34  }
35  }
36  }
37 
38  return StatusCode::SUCCESS;
39 }
40 
41 StatusCode EFTrackingXrtAlgorithm::execute(const EventContext& ctx) const
42 {
43  // Keep xrt variables in execute scope for now as there are non-const
44  // methods that are needed.
45  xrt::device device = xrt::device(0);
46  xrt::uuid uuid = device.load_xclbin(m_xclbinPath);
47 
48  std::vector<xrt::run> runs{};
49  std::vector<xrt::kernel> kernels{};
50  std::vector<xrt::bo> inputBuffers{};
51  std::vector<xrt::bo> outputBuffers{};
52 
53  for (const auto& [kernelName, kernelDefinition] : m_kernelDefinitions) {
54  kernels.emplace_back(device, uuid, kernelName, xrt::kernel::cu_access_mode::exclusive);
55  runs.emplace_back(kernels.back());
56 
57  for (const auto& interfaceDefinition : kernelDefinition) {
58  const int argumentIndex = std::stoi(interfaceDefinition.at("argumentIndex"));
59 
60  // Todo: Reference an enum in the python to avoid painful string
61  // comparisons here.
62  if (interfaceDefinition.at("interfaceMode") == "INPUT") {
63  ATH_MSG_INFO("Mapping: " << kernelName << ",\tINPUT,\t" << argumentIndex);
64 
65  inputBuffers.emplace_back(device,
66  sizeof(unsigned long) * m_bufferSize,
67  kernels.back().group_id(argumentIndex));
68 
69  runs.back().set_arg(argumentIndex, inputBuffers.back());
70  }
71  else if (interfaceDefinition.at("interfaceMode") == "OUTPUT") {
72  ATH_MSG_INFO("Mapping: " << kernelName << ",\tOUTPUT,\t" << argumentIndex);
73 
74  outputBuffers.emplace_back(device,
75  sizeof(unsigned long) * m_bufferSize,
76  kernels.back().group_id(argumentIndex));
77 
78  runs.back().set_arg(argumentIndex, outputBuffers.back());
79  }
80  else {
81  ATH_MSG_ERROR("Failed to map kernel definitions to xrt objects.");
82 
83  return StatusCode::FAILURE;
84  }
85  }
86  }
87 
88  ATH_MSG_DEBUG("Writing Inputs");
89  std::size_t handleIndex = 0;
90  for (
91  const SG::ReadHandleKey<std::vector<unsigned long>>& inputDataStreamKey :
93  ) {
95  inputBuffers[handleIndex].write(inputDataStream->data(),
96  sizeof(unsigned long) * m_bufferSize,
97  0);
98 
99  inputBuffers[handleIndex].sync(XCL_BO_SYNC_BO_TO_DEVICE);
100  handleIndex++;
101  }
102 
103  ATH_MSG_DEBUG("Starting Kernels");
104  for (xrt::run& run : runs) {
105  run.start();
106  }
107 
108  ATH_MSG_DEBUG("Waiting for Kernels");
109  for (xrt::run& run : runs) {
110  run.wait();
111  }
112 
113  ATH_MSG_DEBUG("Reading Outputs");
114  handleIndex = 0;
115  for (
116  const SG::WriteHandleKey<std::vector<unsigned long>>& outputDataStreamKey :
118  ) {
120  ATH_CHECK(outputDataStream.record(std::make_unique<std::vector<unsigned long>>(m_bufferSize)));
121 
122  outputBuffers[handleIndex].sync(XCL_BO_SYNC_BO_FROM_DEVICE);
123  outputBuffers[handleIndex].read(outputDataStream->data());
124 
125  handleIndex++;
126  }
127 
128  return StatusCode::SUCCESS;
129 }
130 
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
find_tgc_unfilled_channelids.runs
int runs
Definition: find_tgc_unfilled_channelids.py:10
run
int run(int argc, char *argv[])
Definition: ttree2hdf5.cxx:28
EFTrackingXrtAlgorithm::EFTrackingXrtAlgorithm
EFTrackingXrtAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: EFTrackingXrtAlgorithm.cxx:7
EFTrackingXrtAlgorithm::m_inputDataStreamKeys
std::vector< SG::ReadHandleKey< std::vector< unsigned long > > > m_inputDataStreamKeys
Keys to access encoded 64bit words following the EFTracking specification.
Definition: EFTrackingXrtAlgorithm.h:35
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:83
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::WriteHandleKey
Property holding a SG store/key/clid from which a WriteHandle is made.
Definition: StoreGate/StoreGate/WriteHandleKey.h:40
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::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: EFTrackingXrtAlgorithm.cxx:41
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
EFTrackingXrtAlgorithmConfig.outputDataStream
outputDataStream
Definition: EFTrackingXrtAlgorithmConfig.py:66
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
EFTrackingXrtAlgorithmConfig.inputDataStream
inputDataStream
Definition: EFTrackingXrtAlgorithmConfig.py:55
EFTrackingXrtAlgorithm::initialize
StatusCode initialize() override final
Definition: EFTrackingXrtAlgorithm.cxx:13
EFTrackingXrtAlgorithm::m_xclbinPath
Gaudi::Property< std::string > m_xclbinPath
Definition: EFTrackingXrtAlgorithm.h:38
EFTrackingXrtAlgorithm.h
EFTrackingXrtAlgorithm::m_outputDataStreamKeys
std::vector< SG::WriteHandleKey< std::vector< unsigned long > > > m_outputDataStreamKeys
Definition: EFTrackingXrtAlgorithm.h:36
EFTrackingXrtAlgorithm::m_bufferSize
Gaudi::Property< std::size_t > m_bufferSize
Definition: EFTrackingXrtAlgorithm.h:54
EFTrackingXrtAlgorithm::m_kernelDefinitions
Gaudi::Property< std::map< std::string, std::vector< std::map< std::string, std::string > > > > m_kernelDefinitions
Definition: EFTrackingXrtAlgorithm.h:47