ATLAS Offline Software
Loading...
Searching...
No Matches
ITkPixelDecodingTracccCellsCUDAAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
8#include "RD53CDecodingCUDA.h"
9
10#include <GaudiKernel/StatusCode.h>
11#include <cuda_runtime_api.h>
12#include <cstdint>
13#include <functional>
14#include <memory>
15#include <stdexcept>
16#include <utility>
17#include <vecmem/containers/data/jagged_vector_buffer.hpp>
18#include <vecmem/containers/data/vector_buffer.hpp>
19#include <vecmem/containers/vector.hpp>
20#include <vecmem/utils/copy.hpp>
21#include <vector>
22
24{
25 ATH_CHECK(m_decodingTool.retrieve());
27 ATH_CHECK(m_tracccCellsKey.initialize());
28
29 ATH_CHECK(m_MRs.retrieve());
30 ATH_CHECK(m_copies.retrieve());
31 ATH_CHECK(m_streamTool.retrieve());
32 return StatusCode::SUCCESS;
33}
34
35StatusCode ITkPixelDecodingTracccCellsCUDAAlg::execute(const EventContext& ctx) const
36{
37 auto& hostMR = *m_MRs->hostMR();
38 std::vector<uint64_t> bytestream_data;
39 ITkPixelDecodingCUDA::StreamInfoContainer::host streams_info(hostMR);
40 try {
41 ATH_CHECK(m_decodingTool->aggregateData(ctx, bytestream_data, streams_info,
42 std::function<StatusCode (uint32_t, uint32_t&)>{
43 [this](uint32_t module_id, uint32_t& detray_index) {
44 return moduleIdToDetrayIndex(module_id, detray_index);
45 }}));
46 } catch (std::runtime_error const & exc) {
47 ATH_MSG_ERROR("aggregateData error: " << exc.what());
48 return StatusCode::FAILURE;
49 }
50
51 if (bytestream_data.empty() || streams_info.size() == 0) {
52 ATH_MSG_ERROR("input data is empty");
53 return StatusCode::FAILURE;
54 }
55
56 // Usable bits per 64-bit frame, matching the CPU DecCore configuration.
57 // In the CPU implementation, DecCore calculates the frame size internally
58 // when core.decode(payload64) creates the MemStreamProvider.
59 // Here each CUDA thread decodes its assigned stream using decode_stream(),
60 // so we construct MemStreamProvider explicitly and use the same frame-size
61 // calculation as DecCore.
62 std::unique_ptr<traccc::edm::silicon_cell_collection::buffer> traccc_cells;
63
64 auto device_copy = m_copies->deviceCopy(ctx);
65 auto host_copy = m_copies->hostCopy(ctx);
66 auto& deviceMR = m_MRs->mainMR();
67 cudaStream_t stream = static_cast<cudaStream_t>(m_streamTool->stream(ctx));
68
69 // Copy input data into GPU: copy to pinned memory first, then copy from
70 // pinned memory to GPU to allows async copy
71 auto input_data_size = static_cast<vecmem::edm::details::size_type>(
72 bytestream_data.size());
73 vecmem::data::vector_buffer<uint64_t> input_data_buffer_host{
74 input_data_size, *m_MRs->hostMR()};
75 host_copy->setup(input_data_buffer_host)->ignore();
76 (*host_copy)(vecmem::get_data(bytestream_data), input_data_buffer_host)->wait();
77
78 vecmem::data::vector_buffer<uint64_t> input_data_buffer{
79 input_data_size, m_MRs->mainMR()};
80 device_copy->setup(input_data_buffer)->ignore();
81 (*device_copy)(vecmem::ignore_event, input_data_buffer_host, input_data_buffer);
82
83 // Copy streams data to GPU
84 ITkPixelDecodingCUDA::StreamInfoContainer::buffer streams_info_buffer(
85 streams_info.size(), m_MRs->mainMR());
86 device_copy->setup(streams_info_buffer)->ignore();
87 (*device_copy)(vecmem::get_data(streams_info), streams_info_buffer)->ignore();
88
89 // Decode all prepared RD53C inputs as one GPU batch.
90 // The GPU allocates the output container, performs the parallel decoding,
91 // and creates the cells in device memory.
92 try {
93 ATH_CHECK(ITkPixelDecodingCUDA::decodeRD53C(stream, deviceMR, hostMR,
94 *device_copy, input_data_buffer, streams_info_buffer, m_frameSize,
95 s_decodingOptions, traccc_cells));
96 } catch(std::runtime_error const & exc) {
97 ATH_MSG_ERROR(exc.what());
98 return StatusCode::FAILURE;
99 }
100
101 // Register device buffer containing decoded traccc cells in SG
102 const unsigned int nb_cells = traccc_cells->capacity();
103 auto traccc_cells_handles = SG::makeHandle(m_tracccCellsKey, ctx);
104 ATH_CHECK(traccc_cells_handles.record(std::move(traccc_cells)));
105
106 ATH_MSG_DEBUG("nb cells created: " << nb_cells
107 << ", input data size [B]: " << bytestream_data.size() * sizeof(bytestream_data[0]));
108 return StatusCode::SUCCESS;
109}
110
112 uint32_t module_id, uint32_t & detray_index) const
113{
114 const std::optional<size_t> module_index_opt =
115 m_geoIdMapping->athenaToDetDescIndex(Identifier(module_id));
116 if (!module_index_opt.has_value()) {
117 ATH_MSG_ERROR("no module index for " << Identifier(module_id));
118 return StatusCode::FAILURE;
119 }
120 detray_index = module_index_opt.value();
121 return StatusCode::SUCCESS;
122}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
Handle class for recording to StoreGate.
const ServiceHandle< StoreGateSvc > & detStore() const
ToolHandle< AthCUDA::IStreamTool > m_streamTool
StatusCode execute(const EventContext &ctx) const override
ToolHandle< AthDevice::IMemoryResourcesTool > m_MRs
Gaudi::Property< std::string > m_geoIdMappingObjectName
const ActsTrk::GeometryIdMapping * m_geoIdMapping
ToolHandle< AthDevice::ICopiesTool > m_copies
SG::WriteHandleKey< traccc::edm::silicon_cell_collection::buffer > m_tracccCellsKey
static constexpr itksw::pix::endec::intf::Options s_decodingOptions
StatusCode moduleIdToDetrayIndex(uint32_t module_id, uint32_t &detray_index) const
ToolHandle< ITkPixelDecodingTool > m_decodingTool
StatusCode decodeRD53C(cudaStream_t stream, vecmem::memory_resource &mr, vecmem::memory_resource &host_mr, vecmem::copy const &copy, vecmem::data::vector_view< uint64_t > const &input_data, StreamInfoContainer::const_view const &stream_info_view, size_t frame_size, const itksw::pix::endec::intf::Options &options, std::unique_ptr< traccc::edm::silicon_cell_collection::buffer > &output_cells)
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())