ATLAS Offline Software
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 
11 #include "IntegrationBase.h"
12 #include <fstream>
13 
15 {
16  // Perform the usual OpenCL setup here
17  // Find device
18  std::vector<cl::Platform> platforms;
19  cl_int err = cl::Platform::get(&platforms);
20 
21  if (err == CL_SUCCESS)
22  {
23  ATH_MSG_INFO("Detected OpenCL platforms: " << platforms.size());
24  }
25  else
26  {
27  ATH_MSG_ERROR("Error calling cl::Platform::get. Error code: " << err);
28  return StatusCode::FAILURE;
29  }
30 
31  std::vector<cl::Device> allDevices;
32 
33  // Print platform information
34  for (cl::Platform pf : platforms)
35  {
36  ATH_MSG_INFO("In initialize()");
37  ATH_MSG_INFO("---");
38  ATH_MSG_INFO("Platform name: " << pf.getInfo<CL_PLATFORM_NAME>());
39  ATH_MSG_INFO("Platform profile: " << pf.getInfo<CL_PLATFORM_PROFILE>());
40  ATH_MSG_INFO("Platform vendor: " << pf.getInfo<CL_PLATFORM_VENDOR>());
41 
42  pf.getDevices(CL_DEVICE_TYPE_ALL, &allDevices);
43  ATH_MSG_INFO("There are " << allDevices.size() << " devices in this platform.");
44 
45  // Loop over all devices in the platform and see if there is an accelerator card
46  // If there is an accelerator card, use the first one
47  bool foundAccelerator = false;
48  for(auto device : allDevices)
49  {
50  if(device.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_ACCELERATOR)
51  {
52  ATH_MSG_INFO("Found an FPGA accelerator card in this platform");
53  m_accelerator = device;
54  foundAccelerator = true;
55  break;
56  }
57  }
58 
59  // If there is no accelerator card, print error and return
60  if(!foundAccelerator)
61  {
62  ATH_MSG_ERROR("Couldn't find an FPGA accelerator card in this platform");
63  return StatusCode::FAILURE;
64  }
65  }
66 
67  ATH_MSG_INFO("Using FPGA accelerator card: " << m_accelerator.getInfo<CL_DEVICE_NAME>());
68 
69  // Create context
70  m_context = cl::Context({m_accelerator});
71 
72  return StatusCode::SUCCESS;
73 }
74 
76 {
77  ATH_MSG_DEBUG("In execute()");
78 
79  return StatusCode::SUCCESS;
80 }
81 
82 StatusCode IntegrationBase::loadProgram(const std::string& xclbin)
83 {
84  // Open binary object in binary mode
85  std::ifstream bin_file(xclbin, std::ios_base::binary);
86  if (!bin_file)
87  {
88  ATH_MSG_ERROR("Couldn't find the xclbin file.");
89  return StatusCode::FAILURE;
90  }
91  // Get the size of the binary file
92  bin_file.seekg(0, bin_file.end);
93  unsigned bin_size = bin_file.tellg();
94  // Reset the reference point back to the beginning
95  bin_file.seekg(0, bin_file.beg);
96  // Create a new pointer for the binary buffer and get the set a pointer to the binary buffer
97  std::vector<char> buf(bin_size);
98  bin_file.read(buf.data(), bin_size);
99 
100  // Create binary object and program object
101  cl_int err = 0;
102  std::vector<cl_int> binaryStatus;
103  cl::Program::Binaries bins{{buf.data(), bin_size}};
104  m_program = cl::Program(m_context, {m_accelerator}, bins, &binaryStatus, &err);
105 
106  bin_file.close();
107 
108  if (err == CL_SUCCESS && binaryStatus.at(0) == CL_SUCCESS)
109  {
110  ATH_MSG_INFO("Successfully loaded xclbin file into " << m_accelerator.getInfo<CL_DEVICE_NAME>());
111  }
112  else
113  {
114  ATH_MSG_ERROR("Error loading xclbin file. Error code: " << err);
115  return StatusCode::FAILURE;
116  }
117 
118  return StatusCode::SUCCESS;
119 }
120 
121 StatusCode IntegrationBase::precheck(std::vector<Gaudi::Property<std::string>> inputs) const
122 {
123  for(auto item : inputs)
124  {
125  if(item.empty())
126  {
127  ATH_MSG_FATAL(item.documentation()<<" is empty. Please set it to a valid value");
128  return StatusCode::FAILURE;
129  }
130  }
131 
132  return StatusCode::SUCCESS;
133 }
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:14
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
python.App.bins
bins
Definition: App.py:410
IntegrationBase::m_context
cl::Context m_context
Context object for the application.
Definition: IntegrationBase.h:67
IntegrationBase.h
The base class for EFTracking 2nd demonstrator integration.
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:193
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::loadProgram
StatusCode loadProgram(const std::string &xclbin)
Find the xclbin file and load it into the OpenCL program object.
Definition: IntegrationBase.cxx:82
IntegrationBase::precheck
StatusCode precheck(std::vector< Gaudi::Property< std::string >> inputs) const
Check if the the desired Gaudi properties are set.
Definition: IntegrationBase.cxx:121
item
Definition: ItemListSvc.h:43
python.output.AtlRunQueryRoot.pf
pf
Definition: AtlRunQueryRoot.py:988
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
IntegrationBase::execute
virtual StatusCode execute()
Should be overriden by derived classes to perform meaningful work.
Definition: IntegrationBase.cxx:75
IntegrationBase::m_program
cl::Program m_program
Program object containing the kernel.
Definition: IntegrationBase.h:68