ATLAS Offline Software
PixelClustering.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 "PixelClustering.h"
12 #include <fstream>
13 #include <CL/cl_ext.h>
14 
16 {
20 
21  return StatusCode::SUCCESS;
22 }
23 
24 StatusCode PixelClustering::execute(const EventContext &ctx) const
25 {
26  ATH_MSG_DEBUG("In execute(), event slot: " << ctx.slot());
27 
28  // open the input testvector file
29  std::ifstream ifs;
30  ATH_MSG_DEBUG("Reading in TV");
31 
32  // Open the testvector to ifstream and check if it is open
33  ifs.open(m_inputTV);
34  if (!ifs.is_open())
35  {
36  ATH_MSG_ERROR("Error reading testvector file");
37  return StatusCode::FAILURE;
38  }
39 
40  uint32_t *inputTV;
41  uint32_t *outputTV;
42 
43  // do 4k alignment
44  posix_memalign((void **)&inputTV, 4096, sizeof(uint32_t) * 4096);
45  posix_memalign((void **)&outputTV, 4096, sizeof(uint32_t) * 4096);
46 
47  // read in the file 4 bytes at a time and store it in a pointer of uint32_t
48  uint32_t cache;
49  int counter = 0;
50  while (ifs >> std::hex >> cache)
51  {
52  inputTV[counter] = cache;
53  counter++;
54 
55  if (counter == 4096)
56  {
57  break;
58  }
59  }
60  // print the first 10 elements of the input vector
61  for (int i = 0; i < 10; i++)
62  {
63  ATH_MSG_DEBUG("inputTV[" << std::dec << i << "] = " << std::hex << inputTV[i]);
64  }
65 
66  // Close the file
67  ifs.close();
68 
69  // Work with the accelerator
70  cl_int err = 0;
71 
72  // Allocate buffers on accelerator
73  cl::Buffer acc_inbuff(m_context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 4096, NULL, &err);
74  cl::Buffer acc_outbuff(m_context, CL_MEM_READ_WRITE, sizeof(uint32_t) * 4096, NULL, &err);
75 
76  // Prepare kernel
77  cl::Kernel acc_kernel(m_program, m_kernelName.value().data(), &err);
78  acc_kernel.setArg<uint>(0, 0);
79  acc_kernel.setArg<cl::Buffer>(1, acc_inbuff);
80  acc_kernel.setArg<cl::Buffer>(2, acc_outbuff);
81 
82  // Make queue of commands
83  cl::CommandQueue acc_queue(m_context, m_accelerator);
84 
85  acc_queue.enqueueWriteBuffer(acc_inbuff, CL_TRUE, 0, sizeof(uint32_t) * 4096, inputTV, NULL, NULL);
86 
87  err = acc_queue.enqueueTask(acc_kernel);
88 
89  acc_queue.enqueueReadBuffer(acc_outbuff, CL_TRUE, 0, sizeof(uint32_t) * 4096, outputTV, NULL, NULL);
90 
91  acc_queue.finish();
92 
93  // Quick validation
94  // Print the fist 10 elements of output vector
95  for (int i = 0; i < 10; i++)
96  {
97  ATH_MSG_DEBUG("outputTV[" << std::dec << i << "] = " << std::hex << outputTV[i]);
98  }
99 
100  free(inputTV);
101  free(outputTV);
102 
103  return StatusCode::SUCCESS;
104 }
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
PixelClustering::execute
StatusCode execute(const EventContext &ctx) const override
Should be overriden by derived classes to perform meaningful work.
Definition: PixelClustering.cxx:24
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
IntegrationBase::m_context
cl::Context m_context
Context object for the application.
Definition: IntegrationBase.h:67
PixelClustering::m_refTV
Gaudi::Property< std::string > m_refTV
Reference TestVector.
Definition: PixelClustering.h:32
PixelClustering.h
Class for the pixel clustering kernel.
PixelClustering::initialize
StatusCode initialize() override
Detect the OpenCL devices and prepare OpenCL context.
Definition: PixelClustering.cxx:15
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
PixelClustering::m_xclbin
Gaudi::Property< std::string > m_xclbin
Path and name of the xclbin file.
Definition: PixelClustering.h:29
PixelClustering::m_kernelName
Gaudi::Property< std::string > m_kernelName
Kernel name.
Definition: PixelClustering.h:30
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:183
lumiFormat.i
int i
Definition: lumiFormat.py:85
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
PixelClustering::m_inputTV
Gaudi::Property< std::string > m_inputTV
Input TestVector.
Definition: PixelClustering.h:31
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
test_pyathena.counter
counter
Definition: test_pyathena.py:15
IntegrationBase::m_program
cl::Program m_program
Program object containing the kernel.
Definition: IntegrationBase.h:68