Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
IntegrationBase.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
12 #include <fstream>
13 #include <CL/cl_ext_xilinx.h>
14 
15 
17 {
18  // Perform the usual OpenCL setup here
19  // Find device
20  std::vector<cl::Platform> platforms;
21  cl_int err = cl::Platform::get(&platforms);
22 
23  if (err == CL_SUCCESS)
24  {
25  ATH_MSG_INFO("Detected OpenCL platforms: " << platforms.size());
26  }
27  else
28  {
29  ATH_MSG_ERROR("Error calling cl::Platform::get. Error code: " << err);
30  return StatusCode::FAILURE;
31  }
32 
33  std::vector<cl::Device> allDevices;
34  int device_id = 0;
35 
36  // Print platform information
37  for (cl::Platform pf : platforms)
38  {
39  ATH_MSG_INFO("In initialize()");
40  ATH_MSG_INFO("---");
41  ATH_MSG_INFO("Platform name: " << pf.getInfo<CL_PLATFORM_NAME>());
42  ATH_MSG_INFO("Platform profile: " << pf.getInfo<CL_PLATFORM_PROFILE>());
43  ATH_MSG_INFO("Platform vendor: " << pf.getInfo<CL_PLATFORM_VENDOR>());
44 
45  pf.getDevices(CL_DEVICE_TYPE_ALL, &allDevices);
46  ATH_MSG_INFO("There are " << allDevices.size() << " devices in this platform.");
47 
48  // Loop over all devices in the platform and see if there is an accelerator card
49  // If there is an accelerator card, use the first one
50  bool foundAccelerator = false;
51 
52  for(auto device : allDevices)
53  {
54  // If emulation is being used, use the first device
55  if(m_doEmulation.value())
56  {
57  m_accelerator = device;
58  foundAccelerator = true;
59  break;
60  }
61 
62  if (device.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_ACCELERATOR)
63  {
64  std::string deviceBDF = "";
65  device.getInfo(CL_DEVICE_PCIE_BDF, &deviceBDF);
66  // check if the user specify the BDF of the device
67  if (!m_deviceBDF.empty())
68  {
69  if (deviceBDF == m_deviceBDF)
70  {
71  ATH_MSG_INFO("Found the device with BDF: " << m_deviceBDF.value());
72  m_accelerator = device;
73  foundAccelerator = true;
74  break;
75  }
76  }
77  else
78  {
79  ATH_MSG_INFO("Using the first found accelerator card: " << device.getInfo<CL_DEVICE_NAME>());
80  m_accelerator = device;
81  foundAccelerator = true;
82  break;
83  }
84  }
85  device_id++;
86  }
87 
88  // If there is no accelerator card, print error and return
89  if(!foundAccelerator)
90  {
91  ATH_MSG_ERROR("Couldn't find an FPGA accelerator card in this platform");
92  return StatusCode::FAILURE;
93  }
94  }
95 
96  ATH_MSG_INFO("Using device number " << device_id << " which is a FPGA accelerator card: " << m_accelerator.getInfo<CL_DEVICE_NAME>());
97 
98  // Create context
99  m_context = cl::Context({m_accelerator});
100 
101  return StatusCode::SUCCESS;
102 }
103 
104 StatusCode IntegrationBase::execute(const EventContext &ctx) const
105 {
106  ATH_MSG_DEBUG("In execute(), event slot: "<<ctx.slot());
107 
108  return StatusCode::SUCCESS;
109 }
110 
111 StatusCode IntegrationBase::loadProgram(const std::string& xclbin)
112 {
113  // Open binary object in binary mode
114  std::ifstream bin_file(xclbin, std::ios_base::binary);
115  if (!bin_file)
116  {
117  ATH_MSG_ERROR("Couldn't find the xclbin file: " << xclbin);
118  return StatusCode::FAILURE;
119  }
120  // Get the size of the binary file
121  bin_file.seekg(0, bin_file.end);
122  unsigned bin_size = bin_file.tellg();
123  // Reset the reference point back to the beginning
124  bin_file.seekg(0, bin_file.beg);
125  // Create a new pointer for the binary buffer and get the set a pointer to the binary buffer
126  std::vector<char> buf(bin_size);
127  bin_file.read(buf.data(), bin_size);
128 
129  // Create binary object and program object
130  cl_int err = 0;
131  std::vector<cl_int> binaryStatus;
132  cl::Program::Binaries bins{{buf.data(), bin_size}};
133  m_program = cl::Program(m_context, {m_accelerator}, bins, &binaryStatus, &err);
134 
135  bin_file.close();
136 
137  if (err == CL_SUCCESS && binaryStatus.at(0) == CL_SUCCESS)
138  {
139  ATH_MSG_INFO("Successfully loaded xclbin file into " << m_accelerator.getInfo<CL_DEVICE_NAME>());
140  }
141  else
142  {
143  ATH_MSG_ERROR("Error loading xclbin file (" << xclbin << ") into " << m_accelerator.getInfo<CL_DEVICE_NAME>() <<". Error code: " << err);
144  return StatusCode::FAILURE;
145  }
146 
147  return StatusCode::SUCCESS;
148 }
149 
150 StatusCode IntegrationBase::precheck(const std::vector<Gaudi::Property<std::string>>& inputs) const
151 {
152  for(const auto &item : inputs)
153  {
154  if(item.empty())
155  {
156  ATH_MSG_FATAL(item.documentation()<<" is empty. Please set it to a valid value");
157  return StatusCode::FAILURE;
158  }
159  }
160 
161  // Always check if bdf is set
162  if (m_deviceBDF.empty())
163  {
164  ATH_MSG_WARNING("Device BDF is not set. Using the first found accelerator card. Set property 'bdfID' to specify the BDF of the device.");
165  }
166 
167  return StatusCode::SUCCESS;
168 }
IntegrationBase::m_accelerator
cl::Device m_accelerator
Device object for the accelerator card.
Definition: IntegrationBase.h:66
IntegrationBase::initialize
virtual StatusCode initialize() override
Detect the OpenCL devices and prepare OpenCL context.
Definition: IntegrationBase.cxx:16
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
IntegrationBase::m_context
cl::Context m_context
Context object for the application.
Definition: IntegrationBase.h:67
IntegrationBase.h
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
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
IntegrationBase::m_deviceBDF
Gaudi::Property< std::string > m_deviceBDF
BDF ID of the accelerator card.
Definition: IntegrationBase.h:69
IntegrationBase::execute
virtual StatusCode execute(const EventContext &ctx) const
Should be overriden by derived classes to perform meaningful work.
Definition: IntegrationBase.cxx:104
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
IntegrationBase::precheck
StatusCode precheck(const std::vector< Gaudi::Property< std::string >> &inputs) const
Check if the the desired Gaudi properties are set.
Definition: IntegrationBase.cxx:150
IntegrationBase::loadProgram
StatusCode loadProgram(const std::string &xclbin)
Find the xclbin file and load it into the OpenCL program object.
Definition: IntegrationBase.cxx:111
IntegrationBase::m_doEmulation
Gaudi::Property< bool > m_doEmulation
Definition: IntegrationBase.h:70
item
Definition: ItemListSvc.h:43
python.output.AtlRunQueryRoot.pf
pf
Definition: AtlRunQueryRoot.py:988
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
IntegrationBase::m_program
cl::Program m_program
Program object containing the kernel.
Definition: IntegrationBase.h:68