Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-27 23:16:39

0001 #include "CondFormats/DataRecord/interface/EcalMappingElectronicsRcd.h"
0002 #include "CondFormats/EcalObjects/interface/alpaka/EcalElectronicsMappingDevice.h"
0003 #include "DataFormats/EcalDigi/interface/alpaka/EcalDigiDeviceCollection.h"
0004 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0005 #include "EventFilter/EcalRawToDigi/interface/DCCRawDataDefinitions.h"
0006 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0009 #include "FWCore/Utilities/interface/ESGetToken.h"
0010 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDGetToken.h"
0011 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDPutToken.h"
0012 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/Event.h"
0013 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EventSetup.h"
0014 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/stream/EDProducer.h"
0015 
0016 #include <alpaka/alpaka.hpp>
0017 
0018 #include "DeclsForKernels.h"
0019 #include "UnpackPortable.h"
0020 
0021 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0022 
0023   class EcalRawToDigiPortable : public stream::EDProducer<> {
0024   public:
0025     explicit EcalRawToDigiPortable(edm::ParameterSet const& ps);
0026     ~EcalRawToDigiPortable() override = default;
0027     static void fillDescriptions(edm::ConfigurationDescriptions&);
0028 
0029     void produce(device::Event&, device::EventSetup const&) override;
0030 
0031   private:
0032     edm::EDGetTokenT<FEDRawDataCollection> rawDataToken_;
0033     using OutputProduct = EcalDigiDeviceCollection;
0034     device::EDPutToken<OutputProduct> digisDevEBToken_;
0035     device::EDPutToken<OutputProduct> digisDevEEToken_;
0036     device::ESGetToken<EcalElectronicsMappingDevice, EcalMappingElectronicsRcd> eMappingToken_;
0037 
0038     std::vector<int> fedsToUnpack_;
0039 
0040     ecal::raw::ConfigurationParameters config_;
0041   };
0042 
0043   void EcalRawToDigiPortable::fillDescriptions(edm::ConfigurationDescriptions& confDesc) {
0044     edm::ParameterSetDescription desc;
0045 
0046     desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
0047     std::vector<int> feds(54);
0048     for (uint32_t i = 0; i < 54; ++i)
0049       feds[i] = i + 601;
0050     desc.add<std::vector<int>>("FEDs", feds);
0051     desc.add<uint32_t>("maxChannelsEB", 61200);
0052     desc.add<uint32_t>("maxChannelsEE", 14648);
0053     desc.add<std::string>("digisLabelEB", "ebDigis");
0054     desc.add<std::string>("digisLabelEE", "eeDigis");
0055 
0056     confDesc.addWithDefaultLabel(desc);
0057   }
0058 
0059   EcalRawToDigiPortable::EcalRawToDigiPortable(const edm::ParameterSet& ps)
0060       : rawDataToken_{consumes<FEDRawDataCollection>(ps.getParameter<edm::InputTag>("InputLabel"))},
0061         digisDevEBToken_{produces(ps.getParameter<std::string>("digisLabelEB"))},
0062         digisDevEEToken_{produces(ps.getParameter<std::string>("digisLabelEE"))},
0063         eMappingToken_{esConsumes()},
0064         fedsToUnpack_{ps.getParameter<std::vector<int>>("FEDs")} {
0065     config_.maxChannelsEB = ps.getParameter<uint32_t>("maxChannelsEB");
0066     config_.maxChannelsEE = ps.getParameter<uint32_t>("maxChannelsEE");
0067   }
0068 
0069   void EcalRawToDigiPortable::produce(device::Event& event, device::EventSetup const& setup) {
0070     // conditions
0071     auto const& eMappingProduct = setup.getData(eMappingToken_);
0072 
0073     // event data
0074     const auto rawDataHandle = event.getHandle(rawDataToken_);
0075 
0076     // make a first iteration over the FEDs to compute the total buffer size
0077     uint32_t size = 0;
0078     uint32_t feds = 0;
0079     for (auto const& fed : fedsToUnpack_) {
0080       auto const& data = rawDataHandle->FEDData(fed);
0081       auto const nbytes = data.size();
0082 
0083       // skip empty FEDs
0084       if (nbytes < globalFieds::EMPTYEVENTSIZE)
0085         continue;
0086 
0087       size += nbytes;
0088       ++feds;
0089     }
0090 
0091     auto& queue = event.queue();
0092 
0093     // input host buffers
0094     ecal::raw::InputDataHost inputHost(queue, size, feds);
0095 
0096     // output device collections
0097     OutputProduct digisDevEB{static_cast<int32_t>(config_.maxChannelsEB), queue};
0098     OutputProduct digisDevEE{static_cast<int32_t>(config_.maxChannelsEE), queue};
0099     // reset the size scalar of the SoA
0100     // memset takes an alpaka view that is created from the scalar in a view to the device collection
0101     auto digiViewEB = cms::alpakatools::make_device_view<uint32_t>(queue, digisDevEB.view().size());
0102     auto digiViewEE = cms::alpakatools::make_device_view<uint32_t>(queue, digisDevEE.view().size());
0103     alpaka::memset(queue, digiViewEB, 0);
0104     alpaka::memset(queue, digiViewEE, 0);
0105 
0106     // iterate over FEDs to fill the host buffer
0107     uint32_t currentCummOffset = 0;
0108     uint32_t fedCounter = 0;
0109     for (auto const& fed : fedsToUnpack_) {
0110       auto const& data = rawDataHandle->FEDData(fed);
0111       auto const nbytes = data.size();
0112 
0113       // skip empty FEDs
0114       if (nbytes < globalFieds::EMPTYEVENTSIZE)
0115         continue;
0116 
0117       // copy raw data into host buffer
0118       std::memcpy(inputHost.data.data() + currentCummOffset, data.data(), nbytes);
0119       // set the offset in bytes from the start
0120       inputHost.offsets[fedCounter] = currentCummOffset;
0121       inputHost.feds[fedCounter] = fed;
0122 
0123       // this is the current offset into the buffer
0124       currentCummOffset += nbytes;
0125       ++fedCounter;
0126     }
0127     assert(currentCummOffset == size);
0128     assert(fedCounter == feds);
0129 
0130     // unpack if at least one FED has data
0131     if (fedCounter > 0) {
0132       ecal::raw::unpackRaw(queue, inputHost, digisDevEB, digisDevEE, eMappingProduct, fedCounter, currentCummOffset);
0133     }
0134 
0135     event.emplace(digisDevEBToken_, std::move(digisDevEB));
0136     event.emplace(digisDevEEToken_, std::move(digisDevEE));
0137   }
0138 
0139 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0140 
0141 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0142 DEFINE_FWK_ALPAKA_MODULE(EcalRawToDigiPortable);