CMS 3D CMS Logo

HcalRawToDigiGPU.cc
Go to the documentation of this file.
1 #include <iostream>
2 
16 
17 #include "DeclsForKernels.h"
18 #include "DecodeGPU.h"
19 #include "ElectronicsMappingGPU.h"
20 
21 class HcalRawToDigiGPU : public edm::stream::EDProducer<edm::ExternalWork> {
22 public:
23  explicit HcalRawToDigiGPU(edm::ParameterSet const& ps);
24  ~HcalRawToDigiGPU() override;
26 
27 private:
29  void produce(edm::Event&, edm::EventSetup const&) override;
30 
31 private:
40 
42 
43  std::vector<int> fedsToUnpack_;
44 
48 };
49 
52 
53  desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
55  std::vector<int> feds(nFeds);
56  for (int i = 0; i < nFeds; ++i)
58  desc.add<std::vector<int>>("FEDs", feds);
59  desc.add<uint32_t>("maxChannelsF01HE", 10000u);
60  desc.add<uint32_t>("maxChannelsF5HB", 10000u);
61  desc.add<uint32_t>("maxChannelsF3HB", 10000u);
62  desc.add<uint32_t>("nsamplesF01HE", 8);
63  desc.add<uint32_t>("nsamplesF5HB", 8);
64  desc.add<uint32_t>("nsamplesF3HB", 8);
65  desc.add<std::string>("digisLabelF5HB", "f5HBDigisGPU");
66  desc.add<std::string>("digisLabelF01HE", "f01HEDigisGPU");
67  desc.add<std::string>("digisLabelF3HB", "f3HBDigisGPU");
68 
69  std::string label = "hcalRawToDigiGPU";
70  confDesc.add(label, desc);
71 }
72 
74  : eMappingToken_{esConsumes()},
75  rawDataToken_{consumes<FEDRawDataCollection>(ps.getParameter<edm::InputTag>("InputLabel"))},
76  digisF01HEToken_{produces<ProductTypef01>(ps.getParameter<std::string>("digisLabelF01HE"))},
77  digisF5HBToken_{produces<ProductTypef5>(ps.getParameter<std::string>("digisLabelF5HB"))},
78  digisF3HBToken_{produces<ProductTypef3>(ps.getParameter<std::string>("digisLabelF3HB"))},
79  fedsToUnpack_{ps.getParameter<std::vector<int>>("FEDs")} {
80  config_.maxChannelsF01HE = ps.getParameter<uint32_t>("maxChannelsF01HE");
81  config_.maxChannelsF5HB = ps.getParameter<uint32_t>("maxChannelsF5HB");
82  config_.maxChannelsF3HB = ps.getParameter<uint32_t>("maxChannelsF3HB");
83  config_.nsamplesF01HE = ps.getParameter<uint32_t>("nsamplesF01HE");
84  config_.nsamplesF5HB = ps.getParameter<uint32_t>("nsamplesF5HB");
85  config_.nsamplesF3HB = ps.getParameter<uint32_t>("nsamplesF3HB");
86 }
87 
89 
91  edm::EventSetup const& setup,
93  // raii
94  cms::cuda::ScopedContextAcquire ctx{event.streamID(), std::move(holder), cudaState_};
95 
96  // conditions
97  auto const& eMappingProduct = setup.getData(eMappingToken_).getProduct(ctx.stream());
98 
99  // bundle up conditions
100  hcal::raw::ConditionsProducts conditions{eMappingProduct};
101 
102  // event data
103  edm::Handle<FEDRawDataCollection> rawDataHandle;
104  event.getByToken(rawDataToken_, rawDataHandle);
105 
106  // scratch
107  hcal::raw::ScratchDataGPU scratchGPU = {
108  cms::cuda::make_device_unique<uint32_t[]>(hcal::raw::numOutputCollections, ctx.stream())};
109 
110  // input cpu data
111  hcal::raw::InputDataCPU inputCPU = {cms::cuda::make_host_unique<unsigned char[]>(
113  cms::cuda::make_host_unique<uint32_t[]>(hcal::raw::utca_nfeds_max, ctx.stream()),
115 
116  // input data gpu
117  hcal::raw::InputDataGPU inputGPU = {
118  cms::cuda::make_device_unique<unsigned char[]>(hcal::raw::utca_nfeds_max * hcal::raw::nbytes_per_fed_max,
119  ctx.stream()),
120  cms::cuda::make_device_unique<uint32_t[]>(hcal::raw::utca_nfeds_max, ctx.stream()),
122 
123  // output cpu
124  outputCPU_ = {cms::cuda::make_host_unique<uint32_t[]>(hcal::raw::numOutputCollections, ctx.stream())};
125 
126  // output gpu
127  outputGPU_.allocate(config_, ctx.stream());
128 
129  // iterate over feds
130  // TODO: another idea
131  // - loop over all feds to unpack and enqueue cuda memcpy
132  // - accumulate the sizes
133  // - after the loop launch cuda memcpy for sizes
134  // - enqueue the kernel
135  uint32_t currentCummOffset = 0;
136  uint32_t counter = 0;
137  for (auto const& fed : fedsToUnpack_) {
138  auto const& data = rawDataHandle->FEDData(fed);
139  auto const nbytes = data.size();
140 
141  // skip empty feds
142  if (nbytes < hcal::raw::empty_event_size)
143  continue;
144 
145 #ifdef HCAL_RAWDECODE_CPUDEBUG
146  printf("fed = %d nbytes = %lu\n", fed, nbytes);
147 #endif
148 
149  // copy raw data into plain buffer
150  std::memcpy(inputCPU.data.get() + currentCummOffset, data.data(), nbytes);
151  // set the offset in bytes from the start
152  inputCPU.offsets[counter] = currentCummOffset;
153  inputCPU.feds[counter] = fed;
154 
155  // this is the current offset into the vector
156  currentCummOffset += nbytes;
157  ++counter;
158  }
159 
160  hcal::raw::entryPoint(inputCPU,
161  inputGPU,
162  outputGPU_,
163  scratchGPU,
164  outputCPU_,
165  conditions,
166  config_,
167  ctx.stream(),
168  counter,
169  currentCummOffset);
170 }
171 
174 
175 #ifdef HCAL_RAWDECODE_CPUDEBUG
176  printf("f01he channels = %u f5hb channesl = %u\n",
179 #endif
180 
181  // FIXME: use sizes of views directly for cuda mem cpy?
182  auto const nchannelsF01HE = outputCPU_.nchannels[hcal::raw::OutputF01HE];
183  auto const nchannelsF5HB = outputCPU_.nchannels[hcal::raw::OutputF5HB];
184  auto const nchannelsF3HB = outputCPU_.nchannels[hcal::raw::OutputF3HB];
185  outputGPU_.digisF01HE.size = nchannelsF01HE;
186  outputGPU_.digisF5HB.size = nchannelsF5HB;
187  outputGPU_.digisF3HB.size = nchannelsF3HB;
188  outputGPU_.digisF01HE.stride = hcal::compute_stride<hcal::Flavor1>(config_.nsamplesF01HE);
189  outputGPU_.digisF5HB.stride = hcal::compute_stride<hcal::Flavor5>(config_.nsamplesF5HB);
190  outputGPU_.digisF3HB.stride = hcal::compute_stride<hcal::Flavor3>(config_.nsamplesF3HB);
191 
195 
196  // reset ptrs that are carried as members
197  outputCPU_.nchannels.reset();
198 }
199 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
hcal::raw::OutputDataGPU outputGPU_
~HcalRawToDigiGPU() override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
DigiCollection< Flavor5, ::calo::common::DevStoragePolicy > digisF5HB
constexpr uint32_t nbytes_per_fed_max
constexpr uint8_t OutputF5HB
char const * label
cms::cuda::ContextState cudaState_
constexpr int32_t empty_event_size
cms::cuda::host::unique_ptr< unsigned char[]> data
edm::ESGetToken< hcal::raw::ElectronicsMappingGPU, HcalElectronicsMapRcd > eMappingToken_
void produce(edm::Event &, edm::EventSetup const &) override
edm::EDPutTokenT< ProductTypef5 > digisF5HBToken_
constexpr uint8_t OutputF3HB
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
edm::EDGetTokenT< FEDRawDataCollection > rawDataToken_
DigiCollection< Flavor1, ::calo::common::DevStoragePolicy > digisF01HE
edm::EDPutTokenT< ProductTypef01 > digisF01HEToken_
void entryPoint(InputDataCPU const &, InputDataGPU &, OutputDataGPU &, ScratchDataGPU &, OutputDataCPU &, ConditionsProducts const &, ConfigurationParameters const &, cudaStream_t cudaStream, uint32_t const, uint32_t const)
hcal::raw::OutputDataCPU outputCPU_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &)
cms::cuda::host::unique_ptr< uint32_t[]> nchannels
constexpr uint32_t numOutputCollections
hcal::raw::ConfigurationParameters config_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
edm::EDPutTokenT< ProductTypef3 > digisF3HBToken_
std::vector< int > fedsToUnpack_
static std::atomic< unsigned int > counter
device::impl::make_device_unique_selector< T >::non_array make_device_unique(cudaStream_t stream)
void acquire(edm::Event const &, edm::EventSetup const &, edm::WaitingTaskWithArenaHolder) override
cms::cuda::host::unique_ptr< int[]> feds
host::impl::make_host_unique_selector< T >::non_array make_host_unique(cudaStream_t stream)
DigiCollection< Flavor3, ::calo::common::DevStoragePolicy > digisF3HB
cms::cuda::host::unique_ptr< uint32_t[]> offsets
void allocate(ConfigurationParameters const &config, cudaStream_t cudaStream)
def move(src, dest)
Definition: eostools.py:511
constexpr uint8_t OutputF01HE
Definition: event.py:1
constexpr uint32_t utca_nfeds_max
HcalRawToDigiGPU(edm::ParameterSet const &ps)