Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-10 01:03:12

0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/Event.h"
0003 #include "FWCore/Framework/interface/EventSetup.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 
0006 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0007 #include "HeterogeneousCore/CUDAUtilities/interface/host_unique_ptr.h"
0008 #include "CUDADataFormats/EcalDigi/interface/DigisCollection.h"
0009 
0010 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0011 #include "DataFormats/EcalDigi/interface/EcalDataFrame_Ph2.h"
0012 
0013 #include "DeclsForKernelsPhase2.h"
0014 
0015 class EcalPhase2DigiToGPUProducer : public edm::stream::EDProducer<> {
0016 public:
0017   explicit EcalPhase2DigiToGPUProducer(const edm::ParameterSet& ps);
0018   ~EcalPhase2DigiToGPUProducer() override = default;
0019   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0020 
0021   void produce(edm::Event& evt, edm::EventSetup const& setup) override;
0022 
0023 private:
0024   const edm::EDGetTokenT<EBDigiCollectionPh2> digiCollectionToken_;
0025   const edm::EDPutTokenT<cms::cuda::Product<ecal::DigisCollection<calo::common::DevStoragePolicy>>>
0026       digisCollectionToken_;
0027 };
0028 
0029 void EcalPhase2DigiToGPUProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0030   edm::ParameterSetDescription desc;
0031 
0032   desc.add<edm::InputTag>("BarrelDigis", edm::InputTag("simEcalUnsuppressedDigis", ""));
0033   desc.add<std::string>("digisLabelEB", "ebDigis");
0034 
0035   descriptions.addWithDefaultLabel(desc);
0036 }
0037 
0038 EcalPhase2DigiToGPUProducer::EcalPhase2DigiToGPUProducer(const edm::ParameterSet& ps)
0039     : digiCollectionToken_(consumes<EBDigiCollectionPh2>(ps.getParameter<edm::InputTag>("BarrelDigis"))),
0040       digisCollectionToken_(produces<cms::cuda::Product<ecal::DigisCollection<calo::common::DevStoragePolicy>>>(
0041           ps.getParameter<std::string>("digisLabelEB"))) {}
0042 
0043 void EcalPhase2DigiToGPUProducer::produce(edm::Event& event, edm::EventSetup const& setup) {
0044   cms::cuda::ScopedContextProduce ctx{event.streamID()};
0045 
0046   //input data from event
0047   const auto& pdigis = event.get(digiCollectionToken_);
0048 
0049   const uint32_t size = pdigis.size();
0050 
0051   ecal::DigisCollection<::calo::common::DevStoragePolicy> digis;
0052   digis.size = size;
0053 
0054   //allocate device pointers for output
0055   digis.ids = cms::cuda::make_device_unique<uint32_t[]>(size, ctx.stream());
0056   digis.data = cms::cuda::make_device_unique<uint16_t[]>(size * EcalDataFrame_Ph2::MAXSAMPLES, ctx.stream());
0057 
0058   //allocate host pointers for holding product data and id vectors
0059   auto idstmp = cms::cuda::make_host_unique<uint32_t[]>(size, ctx.stream());
0060   auto datatmp = cms::cuda::make_host_unique<uint16_t[]>(size * EcalDataFrame_Ph2::MAXSAMPLES, ctx.stream());
0061 
0062   //iterate over digis
0063   uint32_t i = 0;
0064   for (const auto& pdigi : pdigis) {
0065     const int nSamples = pdigi.size();
0066     //assign id to output vector
0067     idstmp.get()[i] = pdigi.id();
0068     //iterate over sample in digi
0069     for (int sample = 0; sample < nSamples; ++sample) {
0070       //get samples from input digi
0071       EcalLiteDTUSample thisSample = pdigi[sample];
0072       //assign adc data to output
0073       datatmp.get()[i * nSamples + sample] = thisSample.raw();
0074     }
0075     ++i;
0076   }
0077 
0078   //copy output vectors into member variable device pointers for the output struct
0079   cudaCheck(
0080       cudaMemcpyAsync(digis.ids.get(), idstmp.get(), size * sizeof(uint32_t), cudaMemcpyHostToDevice, ctx.stream()));
0081   cudaCheck(cudaMemcpyAsync(digis.data.get(),
0082                             datatmp.get(),
0083                             size * EcalDataFrame_Ph2::MAXSAMPLES * sizeof(uint16_t),
0084                             cudaMemcpyHostToDevice,
0085                             ctx.stream()));
0086 
0087   //emplace output in the context
0088   ctx.emplace(event, digisCollectionToken_, std::move(digis));
0089 }
0090 
0091 DEFINE_FWK_MODULE(EcalPhase2DigiToGPUProducer);