Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-11 23:27:52

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     // Workaround until the ProductID problem in issue https://github.com/cms-sw/cmssw/issues/44643 is fixed
0069 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
0070     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::EcalDigiDeviceCollection>",
0071                         ps.getParameter<std::string>("digisLabelEB"));
0072     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::EcalDigiDeviceCollection>",
0073                         ps.getParameter<std::string>("digisLabelEE"));
0074 #endif
0075   }
0076 
0077   void EcalRawToDigiPortable::produce(device::Event& event, device::EventSetup const& setup) {
0078     // conditions
0079     auto const& eMappingProduct = setup.getData(eMappingToken_);
0080 
0081     // event data
0082     const auto rawDataHandle = event.getHandle(rawDataToken_);
0083 
0084     // make a first iteration over the FEDs to compute the total buffer size
0085     uint32_t size = 0;
0086     uint32_t feds = 0;
0087     for (auto const& fed : fedsToUnpack_) {
0088       auto const& data = rawDataHandle->FEDData(fed);
0089       auto const nbytes = data.size();
0090 
0091       // skip empty FEDs
0092       if (nbytes < globalFieds::EMPTYEVENTSIZE)
0093         continue;
0094 
0095       size += nbytes;
0096       ++feds;
0097     }
0098 
0099     auto& queue = event.queue();
0100 
0101     // input host buffers
0102     ecal::raw::InputDataHost inputHost(queue, size, feds);
0103 
0104     // output device collections
0105     OutputProduct digisDevEB{static_cast<int32_t>(config_.maxChannelsEB), queue};
0106     OutputProduct digisDevEE{static_cast<int32_t>(config_.maxChannelsEE), queue};
0107     // reset the size scalar of the SoA
0108     // memset takes an alpaka view that is created from the scalar in a view to the device collection
0109     auto digiViewEB = cms::alpakatools::make_device_view<uint32_t>(alpaka::getDev(queue), digisDevEB.view().size());
0110     auto digiViewEE = cms::alpakatools::make_device_view<uint32_t>(alpaka::getDev(queue), digisDevEE.view().size());
0111     alpaka::memset(queue, digiViewEB, 0);
0112     alpaka::memset(queue, digiViewEE, 0);
0113 
0114     // iterate over FEDs to fill the host buffer
0115     uint32_t currentCummOffset = 0;
0116     uint32_t fedCounter = 0;
0117     for (auto const& fed : fedsToUnpack_) {
0118       auto const& data = rawDataHandle->FEDData(fed);
0119       auto const nbytes = data.size();
0120 
0121       // skip empty FEDs
0122       if (nbytes < globalFieds::EMPTYEVENTSIZE)
0123         continue;
0124 
0125       // copy raw data into host buffer
0126       std::memcpy(inputHost.data.data() + currentCummOffset, data.data(), nbytes);
0127       // set the offset in bytes from the start
0128       inputHost.offsets[fedCounter] = currentCummOffset;
0129       inputHost.feds[fedCounter] = fed;
0130 
0131       // this is the current offset into the buffer
0132       currentCummOffset += nbytes;
0133       ++fedCounter;
0134     }
0135     assert(currentCummOffset == size);
0136     assert(fedCounter == feds);
0137 
0138     // unpack if at least one FED has data
0139     if (fedCounter > 0) {
0140       ecal::raw::unpackRaw(queue, inputHost, digisDevEB, digisDevEE, eMappingProduct, fedCounter, currentCummOffset);
0141     }
0142 
0143     event.emplace(digisDevEBToken_, std::move(digisDevEB));
0144     event.emplace(digisDevEEToken_, std::move(digisDevEE));
0145   }
0146 
0147 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0148 
0149 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0150 DEFINE_FWK_ALPAKA_MODULE(EcalRawToDigiPortable);